-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
446 lines (393 loc) · 14 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"use strict";
const TelegramBot = require("node-telegram-bot-api");
const fs = require("fs");
// Load bot settings
// botmode: 0 (allow everything) 1 (audio msgs only) 2 (text/images msgs only)
var botSettings = JSON.parse(fs.readFileSync("bot-settings.json"));
const bot = new TelegramBot(botSettings.token, { polling: true });
var botUsername;
// set up commands suggestions in Telegram when typing /
// and get the bot's username
initialSetUp();
// array of messages that have the settings keyboard open
let openSettingsMessages = [];
// Listen to messages and commands
// this function is a "validator": if a message is valid, one of the tests performed in it will return and stop the function.
// if a message isn't valid, the tests will not return and so the message will be deleted at the end of the function
bot.on("message", async (msg) => {
// Check if it is the correct group
if (msg.chat.id != botSettings.groupid) {
// if it isn't the correct group, the only message it will answer to is chat id
if (msg.text && msg.text.match(new RegExp(`^\/chatid($|@${botUsername}$)`)))
cmdChatID(msg);
// in any case, it doesn't do anything after
return;
}
// Check if the message is from an admin
let userStatus = (await bot.getChatMember(botSettings.groupid, msg.from.id)).status;
if (userStatus == "creator" || userStatus == "administrator" || msg.from.username == "GroupAnonymousBot") {
console.log(`msg from admin ${msg.from.username} (${userStatus})`);
// check if the admin wrote a command
if (msg.text) {
let text = msg.text;
if (text.match(new RegExp(`^\/help($|@${botUsername}$)`))) {
cmdHelp(msg);
} else if (text.match(new RegExp(`^\/chatid($|@${botUsername}$)`))) {
cmdChatID(msg);
} else if (text.match(new RegExp(`^\/audiobot($|@${botUsername}$)`))) {
cmdSettings(msg);
}
}
// this stops the function and so it prevents the message from being deleted
return;
}
// if it isn't from an admin, check if some user tried to do some commands
// if he tried to, it immediately deletes the message
if (msg.text) {
let text = msg.text;
if (isACommand(text)) {
deleteMessage(msg.message_id, `Hey ${msg.from.first_name}, non puoi usare i comandi!`);
return;
}
}
// if every msgs are allowed, it doesn't do anything
if (botSettings.botmode == 0)
return;
// if only audio msgs are allowed, check if it is a valid audio msg
if (botSettings.botmode == 1 && msg.voice) {
// check if it is under the limit
if (msg.voice.duration <= botSettings.maxaudiosecs)
return;
}
// if audio msgs are forbidden, check if the message isn't an audio one
if (botSettings.botmode == 2 && !msg.voice)
return;
// delete the message: if it reached this point, every test before were negative, so the message has to be deleted
// this "if" will prevent msgs like "msg pinned", "new user" ecc. to be eliminated
if (msg.voice || msg.text || msg.audio || msg.document || msg.photo || msg.sticker || msg.video || msg.video_note || msg.contact || msg.poll || msg.location) {
if (botSettings.botmode == 1)
deleteMessage(msg.message_id, `Hey ${msg.from.first_name}, invia solo audio inferiori a ${botSettings.maxaudiosecs} secondi!`);
else if (botSettings.botmode == 2)
deleteMessage(msg.message_id, `Hey ${msg.from.first_name}, non puoi inviare audio!`);
}
});
// Listen to inline keyboard callbacks (the settings buttons)
bot.on("callback_query", async (pressedButton) => {
// if it comes from another chat, ignore the action
if (pressedButton.message.chat.id != botSettings.groupid)
return;
// check if the user isn't an admin
let userStatus = (await bot.getChatMember(botSettings.groupid, pressedButton.from.id)).status;
if (userStatus != "creator" && userStatus != "administrator" && pressedButton.from.username != "GroupAnonymousBot") {
// A normal user tried to edit the settings: the bot will show an alert to the user
const answerOptions = {
text: "❌ Non hai i permessi per modificare le impostazioni!",
show_alert: true
};
bot.answerCallbackQuery(pressedButton.id, answerOptions);
return;
}
console.log(pressedButton.data + " --- " + pressedButton.message.message_id + " --- " + pressedButton.message.chat.id);
// check if the settings msg used is one of the valid ones:
// if the settings message used for tapping the button isn't in the open settings messages array,
// it just closes it (it removes the keyboard)
if (openSettingsMessages.indexOf(parseInt(pressedButton.message.message_id)) == -1) {
closeOpenSettingsMessages(pressedButton.message.message_id);
return;
}
// if is valid, check what action was performed
let editMessage = false; // if a setting will change, this will become true
switch (pressedButton.data) {
// the button to allow every message
case "but_allow_everything":
if (botSettings.botmode != 0)
editMessage = true;
botSettings.botmode = 0;
// after pressing a button, all the open settings messages are closed (except for "change audio duration" btn)
closeOpenSettingsMessages();
break;
// the button to allow only audio messages
case "but_audio_only":
if (botSettings.botmode != 1)
editMessage = true;
botSettings.botmode = 1;
closeOpenSettingsMessages();
break;
// the button to allow only text/media messages
case "but_chat_only":
if (botSettings.botmode != 2)
editMessage = true;
botSettings.botmode = 2;
closeOpenSettingsMessages();
break;
// the button to show the deletion messages in chat
case "but_confirm_delete":
if (botSettings.senddelmsg != true)
editMessage = true;
botSettings.senddelmsg = true;
closeOpenSettingsMessages();
break;
// the button to hide the deletion messages in chat
case "but_hide_delete":
if (botSettings.senddelmsg != false)
editMessage = true;
botSettings.senddelmsg = false;
closeOpenSettingsMessages();
break;
// the button to show the keyboard to change the max audio duration allowed
case "but_change_duration":
switchToChangeAudioDuration(pressedButton);
return;
}
if (pressedButton.data.startsWith("but_duration_")) {
// if the button pressed is one with the new duration of audio msgs, it will substring and take the last 2 chars
let newDuration = parseInt(pressedButton.data.substring(13, pressedButton.data.length));
if (botSettings.maxaudiosecs != newDuration) {
editMessage = true;
}
botSettings.maxaudiosecs = newDuration;
closeOpenSettingsMessages();
}
// update the settings json file
updateSettingsFile();
// send a popup to the user (top of the screen)
const answerOptions = {
text: "✅ Impostazioni aggiornate"
};
bot.answerCallbackQuery(pressedButton.id, answerOptions);
// if the settings weren't changed, it won't update the msg text with the new settings
if (!editMessage)
return;
// updates the msg text to show the new settings
const options = {
parse_mode: "MarkdownV2",
chat_id: botSettings.groupid,
message_id: pressedButton.message.message_id
};
// Wait a bit before editing the msgs because sometimes it returns an error from the API (content not edited)
setTimeout(() => {
bot.editMessageText(generateSettingText(), options);
}, 200);
});
// /help
async function cmdHelp(msg) {
// it dynamically generates the help text from GodFather's command list
let helpText = "";
for (let cmd of await bot.getMyCommands()) {
helpText += `/${cmd.command} - ${cmd.description}\n\n`;
}
bot.sendMessage(msg.chat.id, helpText);
}
// /chatid
function cmdChatID(msg) {
console.log(`asked chatid in chat ${msg.chat.id}, from user ${msg.from.id} (${msg.from.username})`);
bot.sendMessage(msg.chat.id, msg.chat.id);
}
// /audiobot
async function cmdSettings(msg) {
// closes old settings msg still open (open: inline keyboard shown, close: inline keyboard removed)
closeOpenSettingsMessages();
const options = {
parse_mode: "MarkdownV2",
reply_markup: {
inline_keyboard: [[
{
text: "✅ Tutto",
callback_data: "but_allow_everything"
},
{
text: "🎙 Solo vocali",
callback_data: "but_audio_only"
},
{
text: "💬 Solo testo",
callback_data: "but_chat_only"
}
], [
{
text: "⏱ Cambia durata massima audio",
callback_data: "but_change_duration"
}
], [
{
text: "🔔 Comunica l'eliminazione dei messaggi",
callback_data: "but_confirm_delete"
}
], [
{
text: "🔕 Nascondi l'eliminazione dei messaggi",
callback_data: "but_hide_delete"
}
]]
}
};
let message = await bot.sendMessage(msg.chat.id, generateSettingText(), options);
// sends the message with the current settings and the inline keyboard to edit them
openSettingsMessages.push(message.message_id);
}
// it replaces the normal settings keyboard with the one that shows the audio durations
function switchToChangeAudioDuration(pressedButton) {
const reply_markup = {
inline_keyboard: [[
{
text: "10 sec",
callback_data: "but_duration_10"
},
{
text: "15 sec",
callback_data: "but_duration_15"
},
{
text: "20 sec",
callback_data: "but_duration_20"
}
], [
{
text: "25 sec",
callback_data: "but_duration_25"
},
{
text: "30 sec",
callback_data: "but_duration_30"
},
{
text: "35 sec",
callback_data: "but_duration_35"
}
], [
{
text: "40 sec",
callback_data: "but_duration_40"
},
{
text: "45 sec",
callback_data: "but_duration_45"
},
{
text: "50 sec",
callback_data: "but_duration_50"
}
], [
{
text: "55 sec",
callback_data: "but_duration_55"
},
{
text: "60 sec",
callback_data: "but_duration_60"
},
{
text: "90 sec",
callback_data: "but_duration_90"
}
]]
};
const options = {
chat_id: botSettings.groupid,
message_id: pressedButton.message.message_id
};
// it edits the keyboard (with the new one with the avaliable audio durations)
bot.editMessageReplyMarkup(reply_markup, options);
return;
}
// ====================
// delete a message. If deletion confirmations are shown, it sends the deletion reason and after x seconds it deletes it as well
async function deleteMessage(msgID, reason) {
let deleteConfirmationID = null;
if (botSettings.senddelmsg)
deleteConfirmationID = (await bot.sendMessage(botSettings.groupid, reason, { reply_to_message_id: msgID })).message_id;
// this timeout is to give some time to properly reply to the message before deleting it, so that the user receives a notification
setTimeout(() => {
bot.deleteMessage(botSettings.groupid, msgID);
}, 500);
// if the delete confirmation message was sent, it waits x seconds and then it deletes the confirmation
if (deleteConfirmationID) {
setTimeout(() => {
bot.deleteMessage(botSettings.groupid, deleteConfirmationID);
}, botSettings.delconfirmationexpireseconds * 1000);
}
return;
}
// send the commands (and descriptions) to BotFather and gets the bot's username
async function initialSetUp() {
let commands = [
{
command: "help",
description: "spiega i comandi disponibili del bot ADFarenz Audio Bot"
},
{
command: "chatid",
description: "id della chat"
},
{
command: "audiobot",
description: "apre le impostazioni del bot ADFarenz Audio Bot"
}
];
bot.setMyCommands(commands);
botUsername = (await bot.getMe()).username;
console.log(botUsername);
}
// check if the passed text is a command
function isACommand(text) {
return text.match(new RegExp(`^\/(help|chatid|audiobot)($|@${botUsername}$)`));
}
// generate the text to be displayed in the configuration message
function generateSettingText() {
let textModalita;
switch (botSettings.botmode) {
case 0:
textModalita = "Tutti i messaggi sono ammessi";
break;
case 1:
textModalita = "Solo vocali ammessi";
break;
case 2:
textModalita = "Solo testo/immagini ammesse";
break;
}
const textConfermaElim = botSettings.senddelmsg ? "Sì" : "No";
const settingsText = `*Impostazioni del bot:*
Modalità attuale: __*${textModalita}*__\n
Durata massima dei vocali: __*${botSettings.maxaudiosecs} secondi*__\n
Comunica l'eliminazione dei messaggi: __*${textConfermaElim}*__`;
return settingsText;
}
// close every settings msgs still open in the chat
// optional: if messageID is passed, it will only close the specified message
function closeOpenSettingsMessages(messageID) {
// if messageID is specified, it closes that message
if (messageID) {
const reply_markup = {
inline_keyboard: [[]]
};
const options = {
chat_id: botSettings.groupid,
message_id: messageID
};
// it edits the keyboard (with an empty one)
bot.editMessageReplyMarkup(reply_markup, options);
return;
}
// else, it closes every message in the openSettingMessages array
for (let k = 0; k < openSettingsMessages.length; k++) {
let openSettingMsg = openSettingsMessages[k];
const reply_markup = {
inline_keyboard: [[]]
};
const options = {
chat_id: botSettings.groupid,
message_id: openSettingMsg
};
// it edits the keyboard (with an empty one) and removes the message from the open settings messages array
bot.editMessageReplyMarkup(reply_markup, options);
openSettingsMessages.splice(k, 1);
}
}
// save the settings to the json file
function updateSettingsFile() {
fs.writeFile("bot-settings.json", JSON.stringify(botSettings, null, 2), (err) => {
if (err)
console.log(err);
});
}