-
Notifications
You must be signed in to change notification settings - Fork 29
/
setupBot.js
169 lines (158 loc) · 5.62 KB
/
setupBot.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint no-console: 0 */
const fs = require("fs");
const inquirer = require("inquirer");
const Log = require("./src/Log");
const log = Log("Bot", null, "debug");
const path = require("path");
const TelegramBot = require("node-telegram-bot-api");
const walk = require("walk-sync");
// Version reporting, useful for bug reports
let commit = "";
if (fs.existsSync(path.join(__dirname, "./.git")))
commit = fs.readFileSync(path.join(__dirname, "./.git/refs/heads/es6"), "utf8").substr(0, 7);
log.info(`Nikoro version ${require('./package.json').version}` + (commit ? `, commit ${commit}` : ""));
let token;
let bot;
let admin;
let adminChatId;
const enabledPlugins = new Set();
const configPath = `${__dirname}/config.json`;
inquirer
.prompt({
type: "confirm",
name: "force",
message: "A configuration file already exists. Would you like to overwrite it?",
default: false,
when: function() {
try {
JSON.parse(fs.readFileSync(configPath, "utf8"));
return true;
} catch (e) {
return false;
}
}
})
.then(({force = true}) => {
if (!force) process.exit(0);
return inquirer.prompt({
type: "input",
name: "TELEGRAM_TOKEN",
message: "What's the bot token? (You can get one from @BotFather.)",
validate: token => /\d+:\w+/.test(token) ? true : "Please insert a valid token."
});
})
.then(({TELEGRAM_TOKEN}) => {
token = TELEGRAM_TOKEN;
bot = new TelegramBot(TELEGRAM_TOKEN, {polling: true});
log.info("The bot is online!");
log.info("Send a message to your bot to get started.");
return new Promise(resolve => bot.once("message", resolve));
})
.then(msg => {
admin = msg.from;
adminChatId = msg.chat.id;
log.info("Done! Check your Telegram chat.");
return bot.sendMessage(adminChatId, `Hello, ${admin.first_name}!`);
})
.then(() => {
let msg = "Now, we'll choose the plugins. Here is the list.\n\n";
const plugins = walk(`${__dirname}/src/plugins`)
// Use only .js files.
.filter(item => /\.js$/i.test(item))
.map(path => {
// Remove the extension.
path = path.replace(/\.js$/i, "");
let plugin;
try {
plugin = require(`./src/plugins/${path}`, false).plugin;
} catch (e) {
return {
name: `${path}.js`,
disabled: true,
message: e.message
};
}
return {
name: plugin.name,
disabled: false,
message: plugin.description,
path
};
});
msg += plugins
.map(plugin => {
if (plugin.disabled)
return `_${plugin.name}_ - Disabled because: ${plugin.message}`;
return `*${plugin.name}*: ${plugin.message}`;
})
.join("\n");
const buttons = plugins
.filter(p => !p.disabled)
.map(plugin => ({
text: plugin.name,
callback_data: plugin.path
}));
// We want to put the buttons in two columns.
const rows = buttons.reduce((rows, button) => {
const lastRow = rows[rows.length === 0 ? 0 : (rows.length - 1)];
// If there is space, add a new button
if (lastRow.length < 2)
lastRow.push(button);
// Otherwise, push a new row
else
rows.push([button]);
return rows;
}, [[]]);
return bot.sendMessage(adminChatId, msg, {
parse_mode: "markdown",
reply_markup: {
inline_keyboard: rows
}
});
})
.then(() => bot.sendMessage(adminChatId, "Toggle plugins by clicking on them. When you're done, send /done to proceed."))
.then(() => {
bot.on("callback_query", msg => {
const plugin = msg.data;
let text;
if (enabledPlugins.has(plugin)) {
text = `${plugin} disabled.`;
enabledPlugins.delete(plugin);
} else {
text = `${plugin} enabled.`;
enabledPlugins.add(plugin);
}
bot.answerCallbackQuery({
callback_query_id: msg.id,
text
});
});
return new Promise(resolve => bot.on("text", message => {
if (message.text === "/done")
resolve();
}));
})
.then(() => {
const config = {
TELEGRAM_TOKEN: token,
owners: [admin.id],
activePlugins: Array.from(enabledPlugins)
};
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
const message = "You configured the bot successfully! The setup procedure will now end.";
log.info(message);
log.info("You can now run 'npm run bot' to launch the bot.");
return bot.sendMessage(adminChatId, message);
})
.then(() => new Promise(resolve => setTimeout(resolve, 1000)))
.then(() => process.exit(0))
.catch(err => {
if (err) {
log.error(err);
log.error("A fatal error occurred. Terminating.");
}
process.exit(1);
});
process.on('unhandledRejection', (reason, p) => {
log.error("Unhandled rejection at Promise ", p, " with reason ", reason);
});