-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
350 lines (289 loc) · 10.2 KB
/
index.js
File metadata and controls
350 lines (289 loc) · 10.2 KB
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
import {
handleStart,
handleAllAccounts,
handleAddAccount,
handleAddAccountAdmin,
handleAddAccountNonAdmin,
handleRefreshGroups,
handleSetReportChannel,
handleRestartMonitoring,
getUserSession,
handlePhoneNumber,
handleVerificationCode,
handlePassword,
handleChannelUsername,
} from "./helpers/botHandlers.js";
import dotenv from "dotenv";
import { Telegraf } from "telegraf";
import { connectDB } from "./models/db.js";
import { Account, AdminWithoutSessions, FinderMessage, System } from "./models/db.js";
import { startPreaching, stopPreaching } from "./helpers/preaching.js";
import { startMessageMonitoring, stopMessageMonitoring } from "./helpers/messageMonitor.js";
import launchBot from "./helpers/launchbot.js";
import express from "express";
dotenv.config();
const app = express();
const bot = new Telegraf(process.env.BOT_TOKEN);
global.bot = bot;
async function isAdminUserId(userId) {
const id = userId?.toString();
if (!id) return false;
// Auth strictly via DB: admin Accounts or AdminWithoutSessions
const accountAdmin = await Account.findOne({ admin: true, adminUserId: id });
if (accountAdmin) return true;
const adminNoSession = await AdminWithoutSessions.findOne({ userId: id });
return !!adminNoSession;
}
async function ensureSystemDoc() {
let doc = await System.findOne({});
if (!doc) {
doc = new System({});
await doc.save();
}
return doc;
}
app.get("/ping", (req, res) => {
res.send("Pong");
});
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log("Server is running on port ", PORT);
});
bot.start(handleStart)
bot.action("all_accounts", handleAllAccounts);
bot.action("add_account", handleAddAccount);
bot.action("add_account_admin", handleAddAccountAdmin);
bot.action("add_account_nonadmin", handleAddAccountNonAdmin);
bot.action("refresh_groups", handleRefreshGroups);
bot.action("set_report_channel", handleSetReportChannel);
bot.action("start_preaching", startPreaching)
bot.action("stop_preaching", stopPreaching)
bot.action("restart_monitoring", handleRestartMonitoring)
// Back button navigation
bot.action("back_to_main", async (ctx) => {
await handleStart(ctx);
});
// Callback query handlers
bot.action("all_accounts", handleAllAccounts);
bot.action(/^back_to_/, async (ctx) => {
// Handle back button navigation
const targetMenu = ctx.match[0].split("back_to_")[1];
if (targetMenu === "main") {
await handleStart(ctx);
} else if (targetMenu === "accounts") {
await handleAllAccounts(ctx);
}
});
bot.action("add_account", handleAddAccount);
bot.action("refresh_groups", handleRefreshGroups);
bot.action("set_report_channel", handleSetReportChannel);
// Admin commands
bot.command("whoami", async (ctx) => {
const id = ctx.from.id.toString();
const username = ctx.from.username ? `@${ctx.from.username}` : 'none';
await ctx.reply(`id: ${id}\nusername: ${username}`);
});
bot.command("groupid", async (ctx) => {
// Only meaningful in groups/supergroups/channels
const chatType = ctx.chat?.type;
if (!chatType || (chatType !== "group" && chatType !== "supergroup" && chatType !== "channel")) {
return ctx.reply("Use this command inside a group.");
}
const chatId = ctx.chat?.id?.toString?.() || "";
return ctx.reply(chatId ? `Group ID: ${chatId}` : "Could not read group id.");
});
// Add this function at the top
// function ensureMinus100(groupId) {
// const idStr = groupId.toString().trim().replace(/\s+/g, '');
// // If already starts with -100, return as is
// if (idStr.startsWith('-100')) return idStr;
// // Remove any existing minus sign
// const cleanId = idStr.replace(/^-/, '');
// // Always prepend -100 for storage
// return `-100${cleanId}`;
// }
// Update your set_dump command
// bot.command("set_dump", async (ctx) => {
// if (!(await isAdminUserId(ctx.from.id))) return ctx.reply("Not allowed.");
// const parts = (ctx.message.text || "").trim().split(/\s+/);
// const groupId = parts[1];
// if (!groupId) return ctx.reply("Usage: /set_dump {groupId}");
// const normalizedId = ensureMinus100(groupId);
// const doc = await ensureSystemDoc();
// doc.dumpGroupId = normalizedId;
// await doc.save();
// await ctx.reply(`✅ Dump group set to: ${normalizedId}`);
// });
// SIMPLE FIX: Remove the ensureMinus100 function completely
bot.command("set_dump", async (ctx) => {
if (!(await isAdminUserId(ctx.from.id))) return ctx.reply("Not allowed.");
const parts = (ctx.message.text || "").trim().split(/\s+/);
const groupId = parts[1];
if (!groupId) return ctx.reply("Usage: /set_dump {groupId}");
// Store EXACTLY what was provided
const doc = await ensureSystemDoc();
doc.dumpGroupId = groupId.toString().trim();
await doc.save();
await ctx.reply(`✅ Dump group set to: ${doc.dumpGroupId}`);
});
bot.command("set_admin", async (ctx) => {
if (!(await isAdminUserId(ctx.from.id))) {
return;
}
const parts = (ctx.message.text || "").trim().split(/\s+/);
const arg = parts[1];
if (!arg) {
return ctx.reply("Usage: /set_admin {username|userId}");
}
const isNumeric = /^\d+$/.test(arg);
let username = null;
let userId = null;
if (isNumeric) {
userId = arg;
} else {
username = arg.replace("@", "").trim();
// Try resolving userId via Bot API
try {
const chat = await bot.telegram.getChat(`@${username}`);
userId = chat?.id?.toString?.() || null;
} catch {
// leave userId null if cannot resolve
}
}
// If an Account exists with that username, mark it admin and store adminUserId if we have it
let updated = false;
if (username) {
const acc = await Account.findOne({ username });
if (acc) {
await Account.updateOne(
{ _id: acc._id },
{ $set: { admin: true, ...(userId ? { adminUserId: userId } : {}) } }
);
updated = true;
}
}
if (isNumeric) {
const accByAdminUserId = await Account.findOne({ adminUserId: userId });
if (accByAdminUserId) {
await Account.updateOne({ _id: accByAdminUserId._id }, { $set: { admin: true } });
updated = true;
}
}
if (updated) {
return ctx.reply(`✅ Admin set for ${username ? `@${username}` : userId}${userId ? ` (userId: ${userId})` : ""}`);
}
// Otherwise store in AdminWithoutSessions (must have userId)
if (!userId) {
return ctx.reply("❌ Could not resolve userId for that username. Please pass a numeric userId instead.");
}
await AdminWithoutSessions.updateOne(
{ userId },
{ $set: { userId, username: username || null } },
{ upsert: true }
);
return ctx.reply(`✅ Admin added (no session): ${username ? `@${username}` : ""} userId=${userId}`);
});
// Finder messages "Completed" button
// Add this handler for the finder callback
bot.action(/^finder_done:(.+)/, async (ctx) => {
try {
const finderId = ctx.match[1];
// Find the finder message
const finder = await FinderMessage.findById(finderId);
if (!finder) {
await ctx.answerCbQuery("Finder message not found");
return;
}
// Delete the message from dump group
try {
await ctx.deleteMessage();
} catch (deleteError) {
console.log("Could not delete message:", deleteError.message);
// Try to edit message instead if delete fails
await ctx.editMessageText(`✅ Completed\n\n${finder.preview}`, {
reply_markup: { inline_keyboard: [] }
});
}
// Update finder as completed
finder.completed = true;
finder.completedAt = new Date();
await finder.save();
await ctx.answerCbQuery("✅ Marked as completed");
} catch (error) {
console.error("Error handling finder done:", error);
await ctx.answerCbQuery("Error processing request");
}
});
// For old messages with the "finder_done_pending" callback
bot.action('finder_done_pending', async (ctx) => {
await ctx.answerCbQuery("This action has expired");
});
// Text message handler for multi-step conversations
bot.on("text", async (ctx) => {
const userId = ctx.from.id;
const session = getUserSession(userId);
if (!session) return;
if (session.step === "awaiting_number") {
await handlePhoneNumber(ctx, session);
} else if (session.step === "awaiting_code") {
await handleVerificationCode(ctx, session);
} else if (session.step === "awaiting_password") {
await handlePassword(ctx, session);
} else if (session.step === "awaiting_channel") {
await handleChannelUsername(ctx, session);
}
});
// ============================================
// Initialize Bot
// ============================================
async function main() {
try {
// Connect to database
await connectDB();
// Launch bot
launchBot(bot);
bot.telegram.setMyCommands([
{ command: "start", description: "Start the bot" },
{ command: "groupid", description: "Show current group id (use in group)" },
{ command: "set_dump", description: "Set dump group (admin only)" },
{ command: "set_admin", description: "Register a bot admin (admin only)" },
{ command: "whoami", description: "Show your Telegram id and username" }
])
// Start message monitoring for DMs and replies
await startMessageMonitoring();
// Start preaching functionality
startPreaching();
// Graceful shutdown
process.once("SIGINT", async () => {
console.log("\n⏳ Shutting down bot...");
await stopPreaching();
await stopMessageMonitoring();
bot.stop("SIGINT");
process.exit(0);
});
process.once("SIGTERM", async () => {
console.log("\n⏳ Shutting down bot...");
await stopPreaching();
await stopMessageMonitoring();
bot.stop("SIGTERM");
process.exit(0);
});
} catch (error) {
console.error("❌ Failed to start bot:", error);
process.exit(1);
}
}
main();
// Silence telegram timeout errors only
process.on('unhandledRejection', (reason) => {
if (reason && (reason.message === 'TIMEOUT' || reason.toString().includes('TIMEOUT'))) {
return; // Silent
}
console.error('Unhandled Rejection:', reason);
});
process.on('uncaughtException', (error) => {
if (error && (error.message === 'TIMEOUT' || error.toString().includes('TIMEOUT'))) {
return; // Silent
}
console.error('Uncaught Exception:', error);
});