-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
69 lines (61 loc) · 1.7 KB
/
bot.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
const { Telegraf } = require('telegraf');
const fs = require('fs');
/**
* Info
* =====================
* Get data from /package.json
*/
const info = require('./package.json');
/**
* Environments
* =====================
* Get environment variables from .env file
*/
require('dotenv').config()
/**
* Bot
* =====================
* Initialize bot instance.
*/
const bot = new Telegraf(process.env.TELEGRAM_BOT_API_TOKEN, {username: process.env.TELEGRAM_BOT_USERNAME});
/**
* Auth
* =====================
* Authentication tool
*/
const auth = function(ctx){
// Get allowed users.
const allowedUsersStr = process.env.TELEGRAM_ALLOWED_USERS.trim();
const allowedUsers = allowedUsersStr ? allowedUsersStr.split(',') : [];
// If permissions are not specified, all are enabled.
if(!allowedUsers || allowedUsers.length == 0) return true;
// Else check if current user is allowed.
if(allowedUsers.includes('' + ctx.message.chat.id)) return true;
// Otherwise, return false.
return false;
}
/**
* Router
* =====================
* Commands and hears (reply message). Core of bot.
*/
require(__dirname + '/routes/hears')(bot, info, process.env, auth);
require(__dirname + '/routes/commands')(bot, info, process.env, auth);
require(__dirname + '/routes/callbacks')(bot, info, process.env, auth);
require(__dirname + '/routes/inline_query')(bot, info, process.env, auth);
/**
* Router Extra
* =====================
* Extra private contents
*/
fs.stat('private/extra.js', function(err, stat) {
if(err == null) {
require(__dirname + '/private/extra')(bot, info, process.env, auth);
}
});
/**
* Polling
* =====================
* Telegraf Socket.
*/
bot.startPolling();