forked from N3TC4T/xrp-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (71 loc) · 2.17 KB
/
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
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
77
78
79
80
81
82
83
84
85
require('dotenv').config();
const path = require('path');
const chalk = require('chalk');
const glob = require('glob');
// Telegraf
const Telegraf = require('telegraf');
const Stage = require('telegraf/stage');
const commandParts = require('telegraf-command-parts');
const RedisSession = require('./lib/session');
// libs
const logger = require('./lib/loggin');
// constants
const v = require(path.join(__dirname, '/config/vars'));
const db = require('./models/index');
// defines
const userModel = new db.User();
const groupModel = new db.Group();
const DEV = process.env.NODE_ENV === 'development';
/** Init bot */
const bot = new Telegraf(DEV ? process.env.BOT_TOKEN_DEVELOPMENT : process.env.BOT_TOKEN);
// Set bot username
bot.telegram.getMe().then(botInfo => {
bot.options.username = botInfo.username;
});
console.log('Starting', chalk.magenta(`${v.BOT_NAME_HUMAN} v${v.BOT_VERSION} ${DEV ? '[Development]' : ''}`));
console.log('---', chalk.cyan(new Date()));
/** Using REDIS Instead of JS Memory */
const session = new RedisSession({
store: {
host: process.env.TELEGRAM_SESSION_HOST || '127.0.0.1',
port: process.env.TELEGRAM_SESSION_PORT || 6379,
},
getSessionKey: ctx => ctx.from.id,
});
/** Error handling */
bot.catch(err => {
logger.error(err);
});
/** Command lists here */
const handlersList = [];
glob.sync('./handlers/**/*.js').forEach(function(file) {
handlersList.push(require(path.resolve(file)));
});
/** Define Wizards */
const stage = new Stage();
handlersList.forEach(d => {
const h = new d(bot, db, stage);
if (typeof h.setWizard === 'function') {
h.setWizard();
}
});
/** Middleware */
bot.use(session.middleware());
bot.use(stage.middleware());
bot.use(commandParts());
bot.use((ctx, next) => userModel.updateUser(ctx, next));
bot.use((ctx, next) => groupModel.updateGroup(ctx, next));
/** set Handlers */
handlersList.forEach(d => {
const h = new d(bot, db, stage);
if (typeof h.setHandler === 'function') {
h.setHandler();
}
});
/** Jobs */
glob.sync('./listener/**/*.js').forEach(function(file) {
const l = require(path.resolve(file));
new l(bot, db);
});
/** Start the bot */
bot.launch();