-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
84 lines (71 loc) · 3.12 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
const { Telegraf } = require('telegraf');
const translate = require('google-translate-api-x');
require('dotenv').config();
const botToken = process.env.BOT_TOKEN;
const targetChannel = "@TrackingIsraeliGenocideAR";
const allowedUserId = 2124127983;
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT
// خدمة الملفات الثابتة من المجلد الحالي
app.use(express.static(path.join(__dirname)));
// توجيه طلب الصفحة الرئيسية إلى index.html
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
const bot = new Telegraf(botToken);
const mediaGroups = {};
bot.on("message", async (ctx) => {
if (ctx.from && ctx.from.id === allowedUserId) {
const mediaGroupId = ctx.message.media_group_id;
// إذا كانت رسالة فيديو فقط مع نص
if (ctx.message.video && !ctx.message.media_group_id) {
const caption = ctx.message.caption ? await translate(ctx.message.caption, { to: 'ar' }) : null;
await ctx.telegram.sendVideo(targetChannel, ctx.message.video.file_id, { caption: caption ? caption.text : undefined });
return; // إرجاع بعد الإرسال لتجنب أي معالجة إضافية
}
// إذا كانت هناك مجموعة وسائط
if (ctx.message.media_group_id) {
if (!mediaGroups[mediaGroupId]) {
mediaGroups[mediaGroupId] = {
media: [],
caption: null,
isSent: false,
};
setTimeout(async () => {
if (!mediaGroups[mediaGroupId].isSent) {
mediaGroups[mediaGroupId].isSent = true;
try {
await bot.telegram.sendMediaGroup(targetChannel, mediaGroups[mediaGroupId].media);
console.log("Media group sent successfully!");
} catch (error) {
console.error("Error sending media group:", error);
}
}
}, 10000);
}
if (ctx.message.photo) {
mediaGroups[mediaGroupId].media.push({
type: "photo",
media: ctx.message.photo[ctx.message.photo.length - 1].file_id,
});
} else if (ctx.message.video) {
mediaGroups[mediaGroupId].media.push({
type: "video",
media: ctx.message.video.file_id,
});
}
if (ctx.message.caption && !mediaGroups[mediaGroupId].caption) {
const translated = await translate(ctx.message.caption, { to: 'ar' });
mediaGroups[mediaGroupId].caption = translated.text;
mediaGroups[mediaGroupId].media[0].caption = translated.text;
}
}
}
});
bot.launch();
console.log('Bot is running...');