-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
67 lines (57 loc) · 1.95 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
59
60
61
62
63
64
65
66
67
'use strict';
const TelegramBot = require('node-telegram-bot-api');
const mongoose = require('mongoose');
const path = require('path');
const config = require('./config');
const REPOSITORY_CONNECTION_STRING = config.get('repository:connectionString');
const TELEGRAM_BOT_TOKEN = config.get('app:token');
const IS_PROD = config.get('app:env') === 'production';
const APP_IP = config.get('app:ip');
const APP_PORT = config.get('app:port');
const WEB_HOOK_URL = APP_IP + ':' + APP_PORT + '/bot' + config.get('app:token');
const CERT_KEY_PATH = path.join(__dirname, 'certificates', 'cert.key');
const CERT_PATH = path.join(__dirname, 'certificates', 'cert.pem');
mongoose.Promise = global.Promise;
mongoose.connect(REPOSITORY_CONNECTION_STRING).then(() => {
console.info('Mongo connection success!');
initBot();
});
function initBot() {
if (!IS_PROD) return initBotPolling();
return initBotWebHook();
};
function initBotWebHook() {
let bot = new TelegramBot(TELEGRAM_BOT_TOKEN, {
webHook: {
port: APP_PORT,
key: CERT_KEY_PATH,
cert: CERT_PATH
}
});
return bot.setWebHook(WEB_HOOK_URL, CERT_PATH).then(res => {
return getMe(bot);
}).then(me => {
console.log('Bot started in webhook mode.');
require('./routes/menu')(bot);
require('./routes/admin')(bot);
require('./routes/game')(bot);
require('./routes/leaderboards')(bot);
});
};
function initBotPolling() {
let bot = new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true });
return getMe(bot).then(me => {
console.log('Bot started in polling mode.');
require('./routes/menu')(bot);
require('./routes/admin')(bot);
require('./routes/game')(bot);
require('./routes/leaderboards')(bot);
});
};
function getMe(bot) {
if (bot.me) return Promise.resolve(bot.me);
return bot.getMe().then(user => {
bot.me = user;
return user;
});
};