-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
59 lines (50 loc) · 1.71 KB
/
app.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
/**
* APP Script
* Observer for telegram events
* @module APP
*/
const cluster = require('cluster');
require("./src/bootstrap");
if(!cluster.isWorker) {
const spawn = function(forkId) {
const worker = cluster.fork({
forkId
});
worker.forkId = forkId;
worker.on('exit', function (code, signal) {
setTimeout(function () {
spawn(forkId);
}, 500);
});
}
spawn(0);// For worker
spawn(1);// For application
return;
}
const logSystem = "app";
const TimeAgo = require('javascript-time-ago');
TimeAgo.addDefaultLocale(require('javascript-time-ago/locale/en'));
if(parseInt(process.env.forkId) === 0) {
return require('./src/worker.js');
}
const Telegraf = require('telegraf');
const bot = new Telegraf.Telegraf(global.config.bot.token);
// We can get bot nickname from bot informations. This is particularly useful for groups.
bot.telegram.getMe().then(bot_informations => {
// bot.options.username = bot_informations.username;
global.config.bot.name = "@" + bot_informations.username;
global.log('info', logSystem, "Server initialized");
global.log('info', logSystem, "Bot Nick: %s", global.config.bot.name);
for(let m of ['middleware', 'action', 'command']) {
global.log('info', logSystem, "Loading Registry : " + m);
require('./src/registries/' + m)(bot);
}
});
// bot.action(/^address-([A-Za-z]+)$/, (ctx) => {
// return ctx.answerCbQuery(`Param: ${ctx.match[1]}! 👍`)
// })
bot.catch(e => global.log('error', logSystem, e));
bot.launch();
// // Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));