-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.26 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import TelegramBot from 'node-telegram-bot-api';
import QbApi from './lib/qb_api.js';
import { WELCOME, add_torrent, help, rm_torrent, see_torrents } from './lib/tg_message_handler.js';
import { checkUsers } from './lib/checkUsers.js';
import { gracefulShutdown } from './lib/gracefulShutdown.js';
const BOT_TOKEN = process.env.BOT_TOKEN;
const qb_api = new QbApi(process.env.QB_USERNAME, process.env.QB_PASSWORD);
const listeners = [
{
command: /\/start/,
callback: ({ msg, bot }) => bot.sendMessage(msg.chat.id, WELCOME)
},
{
command: /\/add/,
callback: add_torrent
},
{
command: /\/rm/,
callback: rm_torrent
},
{
command: /\/seeAll/,
callback: see_torrents
},
{
command: /\/help/,
callback: help
}
];
async function main() {
await qb_api.authenticate();
const bot = new TelegramBot(BOT_TOKEN, { polling: true, });
console.log('Telegram bot started');
listeners.forEach(({ command, callback }) => bot.onText(command, (msg) => {
return checkUsers(msg, bot, () => callback({ msg, qb_api, bot }))
}));
process.on('SIGTERM', () => gracefulShutdown({ qb_api }));
process.on('SIGINT', () => gracefulShutdown({ qb_api }));
}
main();