-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
571 lines (504 loc) · 19 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
const TelegramBot = require("node-telegram-bot-api");
const { MongoClient } = require("mongodb");
const axios = require('axios');
const http = require('http');
// Bot Connection
const token = process.env.BOT_TOKEN;
const ownerId = parseInt(process.env.OWNER_ID, 10)
const Force_Join_Channel = process.env.FORCE_JOIN_CHANNEL;
const bot = new TelegramBot(token, { polling: true });
console.log("Connected To Bot");
// Db Connection
let db;
let usersCollection;
let broadcastHistoryCollection;
MongoClient.connect(process.env.MONGO_URI)
.then(client => {
db = client.db("tg-bot");
usersCollection = db.collection("Users");
broadcastHistoryCollection = db.collection("Broadcast_History");
console.log("Connected to MongoDB");
})
.catch(error => console.error(error));
getUserIdsFromDB = async () => {
try {
const users = await usersCollection.find({}).toArray();
return users.map(user => user.userId);
} catch (error) {
console.error("Error reading user IDs from MongoDB:", error);
return [];
}
};
// Helper function to add user ID to MongoDB
const addUserIdToDB = async (userId, firstName) => {
try {
await usersCollection.updateOne(
{ userId: userId },
{ $set: { userId: userId, firstName: firstName } },
{ upsert: true }
);
} catch (error) {
console.error("Error adding user ID to MongoDB:", error);
}
};
// Helper function to get user information
const getUserInfo = async (userId) => {
try {
const user = await bot.getChat(userId);
return user;
} catch (error) {
console.error("Error getting user info:", error);
return null;
}
};
// Helper function to store broadcast history in MongoDB
const storeBroadcastHistory = async (message, photo, caption, photo_link) => {
let newBroadcastEntry;
if (photo !== null) {
newBroadcastEntry = {
type: 'Photo',
caption,
photo_id: photo,
photo_link: photo_link,
date_time: new Date().toLocaleString()
};
} else {
newBroadcastEntry = {
type: 'Message',
message,
date_time: new Date().toLocaleString()
};
}
try {
await broadcastHistoryCollection.insertOne(newBroadcastEntry);
} catch (error) {
console.error('Error writing broadcast history to MongoDB:', error);
}
};
// Helper function to get the file path of a photo
const getFileLink = async (fileId) => {
try {
const response = await bot.getFile(fileId);
const filePath = response.file_path;
return `https://api.telegram.org/file/bot${token}/${filePath}`;
} catch (error) {
console.error('Error getting file path:', error);
return null;
}
};
const getBroadcastHistoryFromDB = async () => {
try {
const broadcastHistory = await broadcastHistoryCollection.find({}).toArray();
return broadcastHistory;
} catch (error) {
console.error("Error reading broadcast history from MongoDB:", error);
return [];
}
};
const setWebhook = async (botToken, webhookUrl) => {
try {
const response = await axios.post(`https://api.telegram.org/bot${botToken}/setWebhook`, {
url: webhookUrl
});
return response.data;
} catch (error) {
console.error('Error setting webhook:', error);
return null;
}
};
bot.onText(/\/start/, async (msg) => {
const chatId = msg.chat.id;
const userId = msg.from.id;
const firstname = msg.from.first_name;
bot.sendChatAction(chatId, "upload_photo");
const users = await getUserIdsFromDB()
if (!users.includes(userId.toString())) {
await addUserIdToDB(userId, firstname);
}
bot.sendPhoto(chatId, "https://t.me/FLIXPLPLOGOS/318", {
caption: `<b>Hey, <a href='tg://user?id=${userId}'>${firstname}</a>\n\ntype /cmds to check commands</b>`,
parse_mode: "HTML",
reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({
inline_keyboard: [[{ text: "𝗢𝗪𝗡𝗘𝗥", url: `tg://user?id=${ownerId}` }]],
resize_keyboard: true,
}),
});
});
// Handling the /cmds command
bot.onText(/\/cmds/, (msg) => {
const chatId = msg.chat.id;
const userId = msg.from.id;
bot.sendChatAction(chatId, "typing");
bot.getChatMember(Force_Join_Channel, userId).then((response) => {
const status = response.status;
if (status !== "member" && status !== "creator") {
bot.sendMessage(
chatId,
"<b>Please join our channel to access this feature.</b>",
{
parse_mode: "HTML",
reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗝𝗢𝗜𝗡 𝗖𝗛𝗔𝗡𝗡𝗘𝗟", url: "https://t.me/+Gh5Cq7m-V003ZjY1" }],
[{ text: "𝗖𝗵𝗲𝗰𝗸 𝗦𝘁𝗮𝘁𝘂𝘀", callback_data: "force_join" }],
],
resize_keyboard: true,
}),
}
);
} else {
bot.sendMessage(chatId, "<b>What You Want to Check</b>", {
parse_mode: "HTML",
reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗖𝗛𝗘𝗖𝗞 𝗖𝗛𝗔𝗧 𝗜𝗗", callback_data: "id" }],
[{ text: "𝗢𝗪𝗡𝗘𝗥 𝗖𝗠𝗗𝗦", callback_data: "Ownercmds" }],
],
resize_keyboard: true,
}),
});
}
});
});
// Handling callback queries
bot.on("callback_query", async (callbackQuery) => {
const data = callbackQuery.data;
const callbackChatId = callbackQuery.message.chat.id;
const callbackFirstName = callbackQuery.from.first_name;
const callbackMessageId = callbackQuery.message.message_id;
const callbackUserId = callbackQuery.from.id;
const callbackQueryId = callbackQuery.id;
if (data === "force_join") {
const response = await bot.getChatMember(Force_Join_Channel, callbackUserId);
const status = response.status;
if (status !== "member" && status !== "creator") {
bot.editMessageText("<b>First Join Then Check</b>", {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗝𝗢𝗜𝗡 𝗖𝗛𝗔𝗡𝗡𝗘𝗟", url: "https://t.me/+Gh5Cq7m-V003ZjY1" }],
[{ text: "𝗖𝗵𝗲𝗰𝗸 𝗦𝘁𝗮𝘁𝘂𝘀", callback_data: "force_join" }],
],
resize_keyboard: true,
}),
});
} else {
bot.editMessageText("<b>What You Want to Check</b>", {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗖𝗛𝗘𝗖𝗞 𝗖𝗛𝗔𝗧 𝗜𝗗", callback_data: "id" }],
[{ text: "𝗢𝗪𝗡𝗘𝗥 𝗖𝗠𝗗𝗦", callback_data: "Ownercmds" }],
],
resize_keyboard: true,
}),
});
}
}
if (data === "id") {
bot.editMessageText(
`<b>ID Lookup ⚡️</b>\n\n✮ 𝗨𝘀𝗲𝗿 𝗜𝗗 ➔ <code>${callbackUserId}</code>\n✮ 𝗚𝗿𝗼𝘂𝗽 𝗜𝗗 ➔ <code>${callbackChatId}</code>\n✮ 𝗨𝘀𝗲𝗿 𝗣𝗿𝗼𝗳𝗶𝗹𝗲 ➔ <a href='tg://user?id=${callbackUserId}'>${callbackFirstName}</a>`,
{
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [[{ text: "Return", callback_data: "listback" }]],
resize_keyboard: true,
}),
}
);
}
if (data === "Ownercmds") {
if (callbackUserId === ownerId) {
bot.editMessageText("<b>What You Want to Check</b>", {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗨𝗦𝗘𝗥𝗦", callback_data: "Userlist" }],
[
{ text: "𝗕𝗥𝗢𝗔𝗗𝗖𝗔𝗦𝗧", callback_data: "broadcast" },
{ text: "𝗛𝗜𝗦𝗧𝗢𝗥𝗬 ", callback_data: "broadcast_history" },
],
[{ text: "Return", callback_data: "listback" }],
],
resize_keyboard: true,
}),
});
} else {
bot.answerCallbackQuery(callbackQueryId, {
text: "𝗡𝗼𝘁 𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘀𝗲𝗱",
show_alert: true,
});
}
}
if (data === "listback") {
bot.editMessageText("<b>What You Want to Check</b>", {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗖𝗛𝗘𝗖𝗞 𝗖𝗛𝗔𝗧 𝗜𝗗", callback_data: "id" }],
[{ text: "𝗢𝗪𝗡𝗘𝗥 𝗖𝗠𝗗𝗦", callback_data: "Ownercmds" }],
],
resize_keyboard: true,
}),
});
}
// Userlist callback
if (data === "Userlist") {
if (callbackUserId === ownerId) {
const userIds = await getUserIdsFromDB();
const userCount = userIds.length;
if (userCount === 0) {
bot.editMessageText("<b>NO USERS FOUND 😏</b>", {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [[{ text: "Return", callback_data: "Ownercmds" }]],
resize_keyboard: true,
}),
});
} else {
const userChatInfoPromises = userIds.map(async (userId, index) => {
const user = await getUserInfo(userId);
if (user && user.username && user.first_name) {
return `<b>${index + 1}</b> 𝗨𝘀𝗲𝗿 ➔ ${
user.first_name
}\n𝗨𝗦𝗘𝗥 𝗜𝗗 ➔ [<code>${userId}</code>]\n𝗨𝗦𝗘𝗥 𝗣𝗥𝗢𝗙𝗜𝗟𝗘 ➔ @${
user.username
}\n`;
}
return "";
});
const userChatInfo = await Promise.all(userChatInfoPromises);
const userInfoText = `<b>${userCount}</b> 𝗨𝗦𝗘𝗥𝗦 𝗟𝗜𝗦𝗧 ✅\n\n${userChatInfo
.filter(Boolean)
.join("\n\n")}`;
bot.editMessageText(userInfoText, {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: "HTML",
reply_markup: JSON.stringify({
inline_keyboard: [[{ text: "Return", callback_data: "Ownercmds" }]],
resize_keyboard: true,
}),
});
}
} else {
bot.answerCallbackQuery(callbackQueryId, {
text: "𝗡𝗼𝘁 𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘀𝗲𝗱",
show_alert: true,
});
}
}
if (data === 'broadcast') {
// Check if the user is authorized
if (callbackUserId === ownerId) {
bot.sendChatAction(callbackChatId, 'typing');
bot.sendMessage(callbackChatId, '𝗣𝗿𝗼𝘃𝗶𝗱𝗲 𝗔 𝗠𝗮𝘀𝘀𝗮𝗴𝗲 𝗧𝗼 𝗕𝗿𝗼𝗮𝗱𝗰𝗮𝘀𝘁', {
reply_markup: {
force_reply: true
}
}).then(sentMessage => {
const replyListenerId = bot.onReplyToMessage(
sentMessage.chat.id,
sentMessage.message_id,
async (replyMessage) => {
const broadcastUserIds = await getUserIdsFromDB();
if (replyMessage.photo) {
const photo = replyMessage.photo.pop().file_id;
const caption = replyMessage.caption || '';
for (const userId of broadcastUserIds) {
if (userId !== ownerId) {
bot.sendPhoto(userId, photo, { caption, parse_mode: 'html' });
}
}
const fileLink = await getFileLink(photo);
await storeBroadcastHistory(null, photo, caption, fileLink);
} else if (replyMessage.text) {
const text = replyMessage.text;
for (const userId of broadcastUserIds) {
if (userId !== ownerId) {
bot.sendMessage(userId, text, { parse_mode: 'html' });
}
}
storeBroadcastHistory(text, null, null, null);
}
bot.sendMessage(callbackChatId, '✅ 𝗕𝗿𝗼𝗮𝗱𝗰𝗮𝘀𝘁 𝗦𝘂𝗰𝗰𝗲𝘀𝘀𝗳𝘂𝗹 𝗧𝗼 𝗔𝗹𝗹 𝗨𝘀𝗲𝗿𝘀');
bot.removeReplyListener(replyListenerId); // Remove the reply listener to avoid spam
}
);
});
} else {
bot.answerCallbackQuery(callbackQuery.id, {
text: '𝗡𝗼𝘁 𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘀𝗲𝗱',
show_alert: true
});
}
}
if (data === "broadcast_history") {
const broadcastHistory = await getBroadcastHistoryFromDB();
let historyText = "";
if (broadcastHistory.length > 0) {
broadcastHistory.forEach((entry, index) => {
historyText += `<b>${index + 1} Broadcast:</b>\n`;
if (entry.type === 'Message') {
historyText += `<b>Type ➔ Message\nMessage ➔</b> ${entry.message}\n`;
} else if (entry.type === 'Photo') {
historyText += `<b>Type ➔ Photo\nPhoto Link ➔</b> <a href='${entry.photo_link}'>View Photo</a>\n<b>Photo Caption ➔</b> ${entry.caption || ''}\n`;
}
historyText += `𝗗𝗮𝘁𝗲 & 𝗧𝗶𝗺𝗲 ↯ ${new Date(entry.date_time).toLocaleString()}\n\n`;
});
bot.editMessageText(`<b>Broadcast History:</b>\n\n${historyText}`, {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: 'HTML',
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "𝗗𝗲𝗹𝗲𝘁𝗲 𝗔𝗹𝗹 𝗛𝗶𝘀𝘁𝗼𝗿𝘆", callback_data: "delete_broadcast_history" }],
[{ text: "Return", callback_data: "Ownercmds" }],
],
resize_keyboard: true,
}),
});
} else {
// If Broadcast History Empty
bot.editMessageText(`<b>No broadcast history found.</b>`, {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: 'HTML',
reply_markup: JSON.stringify({
inline_keyboard: [[{ text: "Return", callback_data: "Ownercmds" }]],
resize_keyboard: true,
}),
});
}
}
if (data === "delete_broadcast_history") {
// Check if the user is authorized to delete broadcast history (e.g., ownerId)
if (callbackUserId === ownerId) {
try {
await broadcastHistoryCollection.deleteMany({});
bot.editMessageText(`<b>All Broadcast History Deleted Successfully</b>`, {
chat_id: callbackChatId,
message_id: callbackMessageId,
parse_mode: 'HTML',
reply_markup: JSON.stringify({
inline_keyboard: [[{ text: "Return", callback_data: "Ownercmds" }]],
resize_keyboard: true,
}),
});
} catch (error) {
console.error("Error deleting all broadcast history:", error);
bot.sendMessage(callbackChatId, "An error occurred while deleting broadcast history");
}
} else {
bot.answerCallbackQuery(callbackQueryId, {
text: "𝗡𝗼𝘁 𝗔𝘂𝘁𝗵𝗼𝗿𝗶𝘀𝗲𝗱",
show_alert: true,
});
}
}
});
bot.onText(/\/(info|id|me)/, (msg) => {
const chatId = msg.chat.id;
const userId = msg.from.id;
const firstname = msg.from.first_name;
bot.sendChatAction(chatId, "typing");
bot.sendMessage(
chatId,
`<b>ID Lookup ⚡️</b>\n\n✮ 𝗨𝘀𝗲𝗿 𝗜𝗗 ➔ <code>${userId}</code>\n✮ 𝗚𝗿𝗼𝘂𝗽 𝗜𝗗 ➔ <code>${chatId}</code>\n✮ 𝗨𝘀𝗲𝗿 𝗣𝗿𝗼𝗳𝗶𝗹𝗲 ➔ <a href='tg://user?id=${userId}'>${firstname}</a>`,
{
parse_mode: "HTML",
disable_web_page_preview: true,
reply_to_message_id: msg.message_id,
}
);
});
bot.on('message', (msg) => {
if (msg.forward_from) {
const chatId = msg.chat.id;
const forwardedFromId = msg.forward_from.id;
const forwardedFromName = msg.forward_from.first_name;
const forwardedFromUsername = msg.forward_from.username;
bot.sendMessage(
chatId,
`𝗙𝗼𝗿𝘄𝗮𝗿𝗱𝗲𝗱 𝗠𝗲𝘀𝘀𝗮𝗴𝗲 𝗜𝗻𝗳𝗼\n\n✮ 𝗨𝘀𝗲𝗿 𝗜𝗗 ➔ <code>${forwardedFromId}</code>\n✮ 𝗨𝘀𝗲𝗿𝗻𝗮𝗺𝗲 ➔ @${forwardedFromUsername}\n✮ 𝗨𝘀𝗲𝗿 𝗣𝗿𝗼𝗳𝗶𝗹𝗲 ➔ <a href='tg://user?id=${forwardedFromId}'>${forwardedFromName}</a>`,
{
parse_mode: "HTML",
disable_web_page_preview: true,
reply_to_message_id: msg.message_id,
}
);
}
});
// Handling the /ping command
bot.onText(/\/ping/, (msg) => {
const chatId = msg.chat.id;
const userId = msg.from.id;
const messageId = msg.message_id;
const startTime = new Date().getTime();
bot.sendChatAction(chatId, 'typing').then(() => {
const endTime = new Date().getTime();
const pingTime = endTime - startTime;
bot.sendMessage(chatId, `Pong! 🏓\nPing Time: ${pingTime} ms`, {
reply_to_message_id: messageId
});
});
});
bot.onText(/\/setwebhook/, async (msg) => {
const chatId = msg.chat.id;
const messageId = msg.message_id;
const messageText = msg.text;
const parts = messageText.split(" ");
if (parts.length === 3) {
const initialMessage = await bot.sendMessage(chatId, "𝗦𝗲𝘁𝘁𝗶𝗻𝗴 𝗪𝗲𝗯𝗵𝗼𝗼𝗸... ⏳", {
reply_to_message_id: messageId
});
if (initialMessage) {
const response = await setWebhook(parts[1], parts[2]);
let message;
if (response.ok && response.result) {
if (response.description === 'Webhook was set') {
message = '𝗪𝗲𝗯𝗵𝗼𝗼𝗸 𝗦𝗲𝘁𝘂𝗽 𝗜𝘀 𝗗𝗼𝗻𝗲 ⚡';
} else if (response.description === 'Webhook is already set') {
message = '𝗪𝗲𝗯𝗵𝗼𝗼𝗸 𝗜𝘀 𝗔𝗹𝗿𝗲𝗮𝗱𝘆 𝗦𝗲𝘁 ✅';
}
} else if (response.error_code === 401) {
message = '𝗜𝗡𝗩𝗔𝗟𝗜𝗗 𝗕𝗢𝗧 𝗧𝗢𝗞𝗘𝗡 ❌';
} else if (response.error_code === 400) {
message = '𝗜𝗻𝘃𝗮𝗹𝗶𝗱 𝗪𝗲𝗯𝗵𝗼𝗼𝗸 𝗨𝗥𝗟, 𝗠𝘂𝘀𝘁 𝗕𝗲 𝗛𝗧𝗧𝗣𝗦 𝗨𝗥𝗟 ❌';
} else {
message = '𝗨𝗻𝗸𝗻𝗼𝘄𝗻 𝗲𝗿𝗿𝗼𝗿 𝗼𝗰𝗰𝘂𝗿𝗿𝗲𝗱.';
}
bot.editMessageText(message, {
chat_id: initialMessage.chat.id,
message_id: initialMessage.message_id
});
}
} else {
bot.sendMessage(chatId, "𝗜𝗻𝘃𝗮𝗹𝗶𝗱 𝗙𝗼𝗿𝗺𝗮𝘁 ❌.\n𝗨𝘀𝗲 /setwebhook 𝗕𝗼𝘁𝗧𝗼𝗸𝗲𝗻 𝗪𝗲𝗯𝗵𝗼𝗼𝗸𝗨𝗿𝗹", {
reply_to_message_id: messageId
});
}
});
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Bot Alive');
});
const PORT = process.env.PORT || 8000;
server.listen(PORT, () => {
console.log(`Bot started on port ${PORT}`);
});