-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (28 loc) · 921 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
var TelegramBot = require('node-telegram-bot-api');
var fs = require("fs");
var router = require("./router.js");
var config = JSON.parse(fs.readFileSync("config.json"));
// Setup telegram polling
var bot = new TelegramBot(config["Telegram_token"], { polling: true });
//add additional contexts to be passed to controllers here
var contexts = {
bot: bot,
config: config
};
//incoming telegram command
bot.onText(/\/(.+)/, function (msg, match) {
var route = router.matched(match[1]);
//now messy autoloader
try {
var controller = require("./controllers/" + route.command + ".js");
controller.execute(route, msg, contexts);
} catch (ex) {
var fromId = msg.chat.id || msg.from.id;
bot.sendMessage(fromId, "Unsupported command.");
}
});
// Any kind of message
// bot.on('message', function (msg) {
// var fromid = msg.chat.id || msg.from.id;;
// console.log("Received message from " + fromid, msg);
// });