-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync.js
66 lines (53 loc) · 1.45 KB
/
sync.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
'use strict';
/**
* Module dependencies.
*/
const
path = require('path'),
mongoose = require('mongoose'),
autoIncrement = require('mongoose-auto-increment'),
debug = require('debug'),
dotenv = require('dotenv'),
requireDir = require('require-dir'),
_ = require('underscore'),
TelegramBot = require('node-telegram-bot-api');
/**
* Application specific configurations.
*/
const
log = debug('telegrambot-reanderman:server'),
env = process.env,
EXECUTION_TIMEOUT = 1000 * 60 * 10; // 10 minutes
dotenv.load({
path: path.join(__dirname, '.env')
});
_.defaults(env, {
NODE_ENV: 'development',
PORT: 9000
});
/**
* Creates an Application.
*/
const db = mongoose.connect(process.env.MONGO_URL, { options: { db: { safe: true } } }, (e) => {
if (e) throw e;
log('Connected to mongodb.');
mongoose.set('debug', process.env.NODE_ENV === "development");
autoIncrement.initialize(db);
// Bootstrap models
requireDir('./models');
log('Bootstrapped models.');
// Create push-only bot
const
bot = new TelegramBot(env.TELEGRAM_BOT_TOKEN, {
webHook: false,
polling: false
});
log('Created bot. Starting sync...');
// Register execution timeout
setTimeout(() => {
log('Reached timeout. Exiting now!');
process.exit(1);
}, EXECUTION_TIMEOUT);
// Bootstrap scheduler
require('./workers/sync')(bot);
});