/
index.js
32 lines (27 loc) · 978 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const TelegramBot = require('node-telegram-bot-api');
const redis = require('redis');
const EmojiData = require('emoji-data');
const config = require('./config');
const client = redis.createClient(config.redis_url);
client.on("error", function (err) {
console.log("Error " + err);
});
const bot = new TelegramBot(config.bot_token, {polling: true});
bot.onText(/\/emojistats/, (msg, match) => {
var chatID = msg.chat.id;
var chatName = msg.chat.title || chatID;
client.zrevrangebyscore(`chat:${chatID}`, '+inf', 0, 'WITHSCORES', 'LIMIT', 0, 10, (err, res) => {
if (err) throw err;
var respString = `Top emojis in chat ${chatName}: \n`;
for (var i = 0; i < res.length; i+=2) {
respString += `${EmojiData.from_unified(res[i]).render()} - ${res[i+1]}\n`
}
bot.sendMessage(chatID, respString);
});
});
bot.on('message', msg => {
const chatID = msg.chat.id;
EmojiData.scan(msg.text).forEach(e => {
client.zincrby(`chat:${chatID}`, 1, e.unified);
});
});