-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
154 lines (138 loc) · 4.32 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
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
const dotenv = require('dotenv');
const fs = require('fs');
const telegramBot = require('node-telegram-bot-api');
dotenv.config();
const bot1 = new telegramBot(process.env.TELEGRAM_API_KEY1, { polling: true });
const bot2 = new telegramBot(process.env.TELEGRAM_API_KEY2, { polling: true });
const bot3 = new telegramBot(process.env.TELEGRAM_API_KEY3, { polling: true });
const users = {
"bot1": [],
"bot2": [],
"bot3": []
};
/** Tg bot handlers */
bot1.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
if (!users.bot1.includes(chatId)) {
console.warn(`New user started: ${chatId}`)
users.bot1.push(chatId);
try {
// saveUsersToFile();
} catch { }
}
});
bot2.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
if (!users.bot2.includes(chatId)) {
console.warn(`New user started: ${chatId}`)
users.bot2.push(chatId);
}
});
bot3.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
if (!users.bot3.includes(chatId)) {
console.warn(`New user started: ${chatId}`)
users.bot3.push(chatId);
}
});
/** Broadcast msg to users */
const sendTgMsgToUsers = async (msg, botName) => {
// users.forEach(async (chatId) => {
// bot.sendMessage(chatId, msg, {
// parse_mode: "HTML",
// });
// });
if (botName == "bot1"){
users.bot1.forEach(async (chatId) => {
while (!isSent) {
try {
await bot1.sendMessage(chatId, msg, {
parse_mode: "HTML",
});
isSent = true; // message successfully sent
} catch (error) {
if (error.response && error.response.statusCode === 429) {
const retryAfter = error.response.body.parameters.retry_after;
console.log(`Rate limit exceeded. Retrying after ${retryAfter} seconds.`);
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
} else {
console.error('Unexpected error:', error);
break; // exit the loop on non-rate limit errors
}
}
}
// bot1.sendMessage(chatId, msg, {
// parse_mode: "HTML",
// });
});
}
// if (botName == "bot2"){
// users.bot2.forEach(async (chatId) => {
// bot2.sendMessage(chatId, msg, {
// parse_mode: "HTML",
// });
// });
// }
// if (botName == "bot3"){
// users.bot3.forEach(async (chatId) => {
// bot3.sendMessage(chatId, msg, {
// parse_mode: "HTML",
// });
// });
// }
if(botName == "bot") {
users.bot1.forEach(async (chatId) => {
bot1.sendMessage(chatId, msg, {
parse_mode: "HTML",
});
});
// users.bot2.forEach(async (chatId) => {
// bot2.sendMessage(chatId, msg, {
// parse_mode: "HTML",
// });
// });
// users.bot3.forEach(async (chatId) => {
// bot3.sendMessage(chatId, msg, {
// parse_mode: "HTML",
// });
// });
}
};
const saveUsersToFile = () => {
const filePath = "users.json";
if (fs.existsSync(filePath)) {
fs.unlink(filePath, (err) => {
if (err) {
console.error("Error deleting the file:", err.message);
return;
} else {
console.log(`${filePath} has been deleted.`);
}
});
}
// Create a new file
fs.writeFile(filePath, JSON.stringify(users, null, 2), "utf8", (err) => {
if (err) {
console.error("Error creating the file:", err.message);
} else {
console.log(`${filePath} has been created with new data.`);
}
});
};
const loadUsersFromFile = () => {
try {
const data = fs.readFileSync("users.json", "utf8");
users.push(...JSON.parse(data));
console.log(`Loaded ${users.length} users.`);
} catch (err) {
console.error("Error loading users from file:", err);
}
};
module.exports = {
bot1,
bot2,
bot3,
sendTgMsgToUsers,
saveUsersToFile,
loadUsersFromFile,
}