-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_bot.js
76 lines (56 loc) · 1.81 KB
/
telegram_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
70
71
72
73
74
75
76
console.log("START TELEGRAM_BOT.JS");
const TelegramBot = require('node-telegram-bot-api');
const {checkApi} = require("./hmstr_logic");
const fs = require("fs");
const token = process.env.TELEGRAM_TOKEN;
const groupId = '-4268517821';
const bot = new TelegramBot(token, {polling: true});
const commandHandlers = {
'/status': handleStatusCommand,
'/restart': handleRestartCommand,
'/tokens': handleTokenList,
'/check': checkApi,
};
bot.on('polling_error', (error) => {
console.error('Polling error:', error);
sendLogMessage('Polling error: ' + error.message);
});
bot.on('webhook_error', (error) => {
console.error('Webhook error:', error);
sendLogMessage('Webhook error: ' + error.message);
});
const getTokensFromFile = () => {
try {
return fs.readFileSync(process.env.TOKENS_FILE_PATH, 'utf8').trim().split('\n');
} catch (error) {
console.error("Error reading tokens from file: ", error);
sendLogMessage("Error reading tokens from file: " + error.message);
process.exit(1);
}
};
function handleStatusCommand(msg) {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Статус бота: працює');
}
function handleRestartCommand(msg) {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'Команда для перезапуска!');
}
function handleTokenList(msg) {
let tokens = getTokensFromFile();
//console.log(tokens);
let tokenstext = '';
tokens.forEach((token) => {
tokenstext += token + '\n' + '\n';
});
sendLogMessage(tokenstext);
}
for (const [command, handler] of Object.entries(commandHandlers)) {
bot.onText(new RegExp(`^${command}$`), handler);
}
const sendLogMessage = (message) => {
bot.sendMessage(groupId, message);
};
module.exports = {
sendLogMessage
};