forked from Telebite/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
77 lines (60 loc) · 2.07 KB
/
bot.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var config = require('./config');
var pluginsL = config.activePlugins;
var token = config.telegramToken;
var TelegramBot = require('node-telegram-bot-api');
var bot = new TelegramBot(token, {polling: true});
console.log("The bot is starting...");
console.log(pluginsL.length + " plugins enabled");
/*loading the plugins*/
var pluginsM = [];
for(i=0;i<pluginsL.length;i++){
var plugin = require("./plugins/" + pluginsL[i])
pluginsM.push(new plugin());
}
console.log("Plugins loaded");
/*init method*/
for(i=0;i<pluginsM.length;i++){
pluginsM[i].init();
}
console.log("Plugins initialized");
bot.on('message', function (msg) {
if(msg.text){ //right now we handle only commands coming as text messages
var chatId = msg.chat.id;
for(pl=0;pl<pluginsM.length;pl++)
{
pluginsM[pl].doMessage(msg, function(reply){ //this does the job, I don't know if it is the best way, but is good for async functions done by plugins
if(reply.type == "text")
{
bot.sendMessage(chatId, reply.text);
}
if(reply.type == "audio")
{
bot.sendAudio(chatId, reply.audio);
}
if(reply.type == "photo")
{
bot.sendPhoto(chatId, reply.photo);
}
if(reply.type == "status")
{
bot.sendChatAction(chatId, reply.status);
}
return;
});
}
}
});
process.on('SIGINT', shutDown);
process.on('uncaughtException', shutDown); //remove this is you are developing plugins
function shutDown(){
console.log("The bot is shutting down...");
for(i=0;i<pluginsM.length;i++){
pluginsM[i].doStop();
}
/*need a better way to implement this:
wait until all plugins have stopped before exiting, something with callbacks (which I don't know much about)
waiting 1,5 sec is NOT good, but works*/
setTimeout(function() {
process.exit();
}, 1500);
}