forked from Ali-Sdg90/CS-Queue-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.js
491 lines (411 loc) · 15.5 KB
/
index2.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
require("dotenv").config();
const { Telegraf } = require("telegraf");
const express = require("express");
const axios = require("axios");
const ORGANIZATION = "cs-internship";
const PROJECT = "CS Internship Program";
const PARENT_ID = 30789;
const WORKITEM_ID = 31256;
const SPAM_THRESHOLD = 10;
const SPAM_TIME_WINDOW = 10 * 1000;
const COMMAND_COOLDOWN = 2 * 1000;
const GROUP_ID = "-1002368870938";
const ADMIN_GROUP_ID = process.env.Admin_Group_ID;
const PAT = process.env.PAT_TOKEN;
const BOT_VERSION = "v2.1";
const userCooldowns = new Map();
const userMessageCounts = new Map();
const blockedUsers = new Set();
const AUTH = `Basic ${Buffer.from(`:${PAT}`).toString("base64")}`;
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
const app = express();
app.use(express.json());
const createWorkItem = async (ctx, userData, isNewID) => {
if (!userData.username) {
ctx.reply(`کاربر یوزرنیم ندارد! 🤖`);
return;
}
try {
const res = await axios.get(
`https://dev.azure.com/${ORGANIZATION}/${PROJECT}/_apis/wit/workitems/${WORKITEM_ID}?api-version=7.1-preview.3`,
{ headers: { Authorization: AUTH } }
);
const originalWorkItem = res.data;
const fieldsToCopy = {
"System.Title": `Entrance Path: @${userData.username} - ${
(userData.first_name ? userData.first_name : "") +
" " +
(userData.last_name ? userData.last_name : "")
}`,
"System.Description": `<div style="text-align: right;">تاریخ ورود به گروه: ${new Date(
ctx.message.date * 1000
).toLocaleString("fa-IR", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: false,
})}<br/></br><a href="https://t.me/c/1191433472/${
ctx.message.message_id
}">https://t.me/c/1191433472/${ctx.message.message_id}</a></div>`,
"System.AreaPath": originalWorkItem.fields["System.AreaPath"],
"System.IterationPath":
originalWorkItem.fields["System.IterationPath"],
"Microsoft.VSTS.Common.Priority":
originalWorkItem.fields["Microsoft.VSTS.Common.Priority"] || 2,
};
const payload = Object.entries(fieldsToCopy).map(([key, value]) => ({
op: "add",
path: `/fields/${key}`,
value,
}));
payload.push({
op: "add",
path: "/relations/-",
value: {
rel: "System.LinkTypes.Hierarchy-Reverse",
url: `https://dev.azure.com/${ORGANIZATION}/${PROJECT}/_apis/wit/workItems/${PARENT_ID}`,
attributes: { isLocked: false, name: "Parent" },
},
});
await axios.post(
`https://dev.azure.com/${ORGANIZATION}/${PROJECT}/_apis/wit/workitems/$Product%20Backlog%20Item?api-version=7.1-preview.3`,
payload,
{
headers: {
"Content-Type": "application/json-patch+json",
Authorization: AUTH,
},
}
);
} catch (error) {
errorReply(ctx);
} finally {
if (isNewID) {
ctx.reply(`سلام @${userData.username}
یوزرنیم شما ثبت شد.
برای آشنایی با فرآیند مصاحبه، لطفاً پیامهای پینشده گروه را مطالعه کنید.
اگر درباره برنامه یا فرآیند مصاحبه سؤالی داشتید، میتوانید در گروه مطرح کنید؛ خوشحال میشویم راهنماییتان کنیم.
موفق باشید🌱`);
}
}
};
setInterval(() => {
const now = Date.now();
for (const [userId, data] of userMessageCounts.entries()) {
if (data.banned) continue;
if (Array.isArray(data)) {
userMessageCounts.set(
userId,
data.filter((t) => now - t < SPAM_TIME_WINDOW)
);
if (userMessageCounts.get(userId).length === 0) {
userMessageCounts.delete(userId);
}
}
}
for (const [userId, lastUsed] of userCooldowns.entries()) {
if (now - lastUsed > COMMAND_COOLDOWN * 5) {
userCooldowns.delete(userId);
}
}
}, 3 * 60 * 1000);
const sendIDKEmoji = async (ctx) => {
try {
await ctx.telegram.callApi("setMessageReaction", {
chat_id: ctx.chat.id,
message_id: ctx.message.message_id,
reaction: [{ type: "emoji", emoji: "🤷♂️" }],
});
} catch (error) {
errorReply(ctx);
}
};
const errorReply = (ctx) => {
ctx.reply("خطایی رخ داده است! 🤖");
};
const isAdminTalking = async (ctx) => {
try {
const member = await ctx.telegram.getChatMember(
ctx.chat.id,
ctx.from.id
);
if (member.status === "administrator" || member.status === "creator") {
return true;
} else {
return false;
}
} catch (error) {
errorReply(ctx);
return false;
}
};
bot.command("Version", async (ctx) => {
if (ctx.from.username === "Ali_Sdg90") {
ctx.reply(`🤖 Bot Version: ${BOT_VERSION}`);
} else {
try {
await ctx.telegram.callApi("setMessageReaction", {
chat_id: ctx.chat.id,
message_id: ctx.message.message_id,
reaction: [{ type: "emoji", emoji: "👀" }],
});
} catch (error) {
errorReply(ctx);
}
return;
}
});
bot.command("Ban", async (ctx) => {
const admin = ctx.from;
if (!ctx.message.reply_to_message) {
return ctx.reply(
"❗️ برای بلاک کردن، این دستور را روی پیام کاربر ریپلای کنید."
);
}
const repliedText = ctx.message.reply_to_message.text;
const idMatch = repliedText?.match(/🆔 (\d+)/);
if (!idMatch) {
return ctx.reply("❗️ آیدی کاربر در پیام ریپلایشده پیدا نشد.");
}
const targetUserId = parseInt(idMatch[1]);
if (isNaN(targetUserId)) {
return ctx.reply("❗️ آیدی معتبر نیست.");
}
if (blockedUsers.has(targetUserId)) {
return ctx.reply("ℹ️ این کاربر قبلاً بلاک شده است.");
}
blockedUsers.add(targetUserId);
try {
await ctx.telegram.sendMessage(
targetUserId,
"🚫 شما توسط ادمین بلاک شدید. دیگر پیامهایتان برای ادمین فرستاده نخواهد شد."
);
} catch (err) {
console.warn("❗️ ارسال پیام به کاربر ممکن نبود:", err.description);
}
await ctx.telegram.sendMessage(
ADMIN_GROUP_ID,
`🚫 کاربر با آیدی ${targetUserId} توسط @${admin.username} بلاک شد.\n\n#Ban`,
{
reply_markup: {
inline_keyboard: [
[
{
text: "🔓 رفع بلاک",
callback_data: `unban_${targetUserId}`,
},
],
],
},
}
);
await ctx.reply("🚫 کاربر با موفقیت بلاک شد.");
});
bot.on("callback_query", async (ctx) => {
const data = ctx.callbackQuery.data;
const admin = ctx.from;
if (data.startsWith("unban_")) {
const userId = parseInt(data.split("_")[1]);
if (blockedUsers.has(userId)) {
blockedUsers.delete(userId);
try {
await ctx.telegram.sendMessage(
userId,
"✅ شما از بلاک خارج شدید."
);
} catch (err) {
console.warn(
"❗️ ارسال پیام رفع بلاک به کاربر ممکن نبود:",
err.description
);
}
await ctx.telegram.sendMessage(
ADMIN_GROUP_ID,
`✅ کاربر ${userId} توسط @${admin.username} از بلاک خارج شد.\n\n#Unblock`
);
} else {
await ctx.telegram.sendMessage(
ADMIN_GROUP_ID,
`ℹ️ کاربر ${userId} در لیست بلاک نبود.`
);
}
await ctx.telegram.answerCbQuery(ctx.callbackQuery.id, {
text: "✅ انجام شد.",
});
}
});
bot.on("message", async (ctx, next) => {
const chat = ctx.chat;
const user = ctx.from;
if (chat.type === "private") {
if (blockedUsers.has(user.id)) {
console.log(`⛔️ Blocked user ${user.id} tried to send a message.`);
return;
}
if (isSpamming(user.id)) {
blockedUsers.add(user.id);
await ctx.telegram.sendMessage(
user.id,
"🚫 شما به دلیل ارسال بیش از حد پیام بلاک شدهاید. از این به بعد پیامهایتان برای ادمین فرستاده نخواهد شد."
);
await ctx.telegram.sendMessage(
ADMIN_GROUP_ID,
`🚫 کاربر ${user.first_name} با یوزرنیم @${
user.username ?? "—"
} با آیدی ${user.id} به دلیل اسپم بلاک شد.\n\n#SpamBlocked`
);
return;
}
const messageText = ctx.message.text || "[پیام غیرمتنی]";
const now = new Date();
const timeString = now.toLocaleString("fa-IR", {
timeZone: "Asia/Tehran",
hour12: false,
});
await ctx.telegram.sendMessage(
ADMIN_GROUP_ID,
`📥 پیام جدید در PV:\n\n🕒 ${timeString}\n👤 ${
user.first_name ?? ""
} ${user.last_name ?? ""} (@${user.username ?? "—"})\n🆔 ${
user.id
}\n\n📝 پیام:\n\n<code>${messageText}</code>\n\n#PrivateMessage`,
{
parse_mode: "HTML",
}
);
return;
}
await next();
});
bot.command("Aloha", async (ctx) => {
if (ctx.message.chat.id != GROUP_ID) {
ctx.reply(
"سلام\nاین بات فقط در گروه صف برنامه CS Internship قابل استفاده است.\n\nhttps://t.me/+X_TxP_odRO5iOWFi"
);
return;
}
if (ctx.message.from.username) {
ctx.reply(`Aloha :)\n\n@${ctx.message.from.username}\n@Ali_Sdg90`);
}
});
const isSpamming = (userId) => {
const now = Date.now();
if (!userMessageCounts.has(userId)) {
userMessageCounts.set(userId, []);
}
const timestamps = userMessageCounts.get(userId);
timestamps.push(now);
userMessageCounts.set(
userId,
timestamps.filter((t) => now - t < SPAM_TIME_WINDOW)
);
if (timestamps.length > SPAM_THRESHOLD) {
return true;
}
return false;
};
const isOnCooldown = (userId) => {
const lastUsed = userCooldowns.get(userId) || 0;
return Date.now() - lastUsed < COMMAND_COOLDOWN;
};
bot.use(async (ctx, next) => {
if (ctx.message && ctx.message.chat.id == GROUP_ID) {
const userId = ctx.from.id;
if (
userMessageCounts.has(userId) &&
userMessageCounts.get(userId).banned
) {
return;
}
if (isSpamming(userId)) {
ctx.reply(
`کاربر @${ctx.from.username} به دلیل ارسال پیامهای زیاد به عنوان اسپم شناسایی شد و از گروه حذف شد.`
);
await ctx.kickChatMember(userId);
userMessageCounts.set(userId, { banned: true });
return;
}
}
await next();
});
bot.command("add_ID", async (ctx) => {
if (ctx.message.chat.id != GROUP_ID) {
ctx.reply(
"سلام\nاین بات فقط در گروه صف برنامه CS Internship قابل استفاده است.\n\nhttps://t.me/+X_TxP_odRO5iOWFi"
);
return;
}
if (!(await isAdminTalking(ctx))) {
try {
await ctx.telegram.callApi("setMessageReaction", {
chat_id: ctx.chat.id,
message_id: ctx.message.message_id,
reaction: [{ type: "emoji", emoji: "👀" }],
});
} catch (error) {
errorReply(ctx);
}
return;
}
const userId = ctx.from.id;
if (isOnCooldown(userId)) {
ctx.reply(`⏳ لطفاً کمی صبر کنید و سپس دوباره تلاش کنید.`);
return;
}
userCooldowns.set(userId, Date.now());
if (!ctx.message.reply_to_message) {
sendIDKEmoji(ctx);
return;
}
createWorkItem(ctx, ctx.message.reply_to_message.from, true);
});
bot.on("new_chat_members", async (ctx) => {
if (ctx.message.chat.id != GROUP_ID) {
ctx.reply(
"سلام\nاین بات فقط در گروه صف برنامه CS Internship قابل استفاده است.\n\nhttps://t.me/+X_TxP_odRO5iOWFi"
);
return;
}
if (ctx.message.new_chat_participant.is_bot) {
const botInfo =
`🚫 ورود بات به گروه مجاز نیست! 🚫\n\n` +
`<b>نام بات:</b> ${
ctx.message.new_chat_participant.first_name || "نامشخص"
}\n` +
`<b>آیدی عددی:</b> <code>${ctx.message.new_chat_participant.id}</code>\n` +
`<b>یوزرنیم:</b> ${
ctx.message.new_chat_participant.username
? `@${ctx.message.new_chat_participant.username}`
: "ندارد"
}`;
ctx.replyWithHTML(botInfo);
ctx.kickChatMember(ctx.message.new_chat_participant.id);
return;
}
if (!ctx.message.new_chat_participant.username) {
ctx.reply(`سلام ${ctx.message.new_chat_participant.first_name}
به گروه صف برنامه CS Internship خوش آمدید.
لطفاً برای حساب کاربری تلگرام خود یک Username تنظیم کنید و پس از انجام این کار، در گروه اطلاع دهید.
توجه داشته باشید که داشتن Username برای شرکت در جلسات گروه و همچنین جلسه مصاحبه الزامی است.
سپاس از همکاری شما 🌱`);
return;
}
ctx.reply(`سلام @${ctx.message.new_chat_participant.username}
به گروه صف برنامه CS Internship خوش آمدید.
برای آشنایی با فرآیند مصاحبه، لطفاً پیامهای پینشده گروه را مطالعه کنید.
اگر درباره برنامه یا فرآیند مصاحبه سؤالی داشتید، میتوانید در گروه مطرح کنید؛ خوشحال میشویم راهنماییتان کنیم.
موفق باشید🌱`);
createWorkItem(ctx, ctx.message.new_chat_participant, false);
});
app.get("/", (req, res) => {
res.send("Bot is running!");
});
// app.use(bot.webhookCallback("/bot"));
app.listen(3000, () => {
console.log("Express server running on port 3000");
});
bot.launch();