aboutsummaryrefslogtreecommitdiff
path: root/vnext/server/durov.js
blob: acd8915874c2114584aaa4c18e4f0bfd1d6e6c32 (plain) (blame)
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
33
import TelegramBot from 'node-telegram-bot-api';
import debug from 'debug';
var log = debug('durov');
import config from 'config';


import { formatQuote, formatTitle } from './common/MessageUtils';
import { format } from '../src/utils/embed';

const sender = new TelegramBot(config.get('service.durov.token'), { polling: true });
const demouser = config.get('service.durov.demouser');

sender.on('message', msg => {
    log(`MESSAGE: ${JSON.stringify(msg)}`);
});

/**
 * @param {import('../src/api').Message} msg
 * @param {string[]} subscribers
 */
export const sendTelegramNotification = (msg, subscribers) => {
    log(`Telegram event: ${JSON.stringify(msg)}, ${subscribers} ${subscribers.length}`);
    if (!msg.service) {
        if (subscribers && subscribers.includes(demouser)) {
            const message = `${formatTitle(msg)}\n${formatQuote(msg)}\n${format(msg.body, msg.uid, false)}`;
            log(message);
            sender.sendMessage(demouser, message, {
                parse_mode: 'HTML',
                disable_web_page_preview: true
            }).then(log).catch(log);
        }
    }
};