Spaces:
Running
Running
ayoub ayoub
commited on
Create index.js
Browse files
index.js
ADDED
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const TelegramBot = require('node-telegram-bot-api');
|
2 |
+
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');
|
3 |
+
const ffmpeg = require('fluent-ffmpeg');
|
4 |
+
const si = require('systeminformation');
|
5 |
+
|
6 |
+
const TOKEN = '8065267966:AAFxE5XHEBNs-AH1_JOTm8JpmutZgwP1fUY';
|
7 |
+
const ADMIN_ID = 7708913693;
|
8 |
+
|
9 |
+
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
|
10 |
+
|
11 |
+
const bot = new TelegramBot(TOKEN, { polling: true });
|
12 |
+
|
13 |
+
const userStreams = {}; // { chatId: { streamId: { proc, timeout } } }
|
14 |
+
const streamCounter = {}; // { chatId: number }
|
15 |
+
|
16 |
+
function getSystemSettings() {
|
17 |
+
return si.cpu()
|
18 |
+
.then(cpu => si.mem()
|
19 |
+
.then(mem => {
|
20 |
+
const cores = cpu.cores || 1;
|
21 |
+
const ramGB = mem.total / 1024 / 1024 / 1024;
|
22 |
+
|
23 |
+
let preset = 'ultrafast';
|
24 |
+
if (cores >= 8) preset = 'faster';
|
25 |
+
else if (cores >= 4) preset = 'veryfast';
|
26 |
+
|
27 |
+
let bitrate = '800k';
|
28 |
+
let resolution = '640x360';
|
29 |
+
if (ramGB >= 8) {
|
30 |
+
bitrate = '3000k';
|
31 |
+
resolution = '1280x720';
|
32 |
+
} else if (ramGB >= 4) {
|
33 |
+
bitrate = '1500k';
|
34 |
+
resolution = '854x480';
|
35 |
+
}
|
36 |
+
return { preset, bitrate, resolution };
|
37 |
+
})
|
38 |
+
);
|
39 |
+
}
|
40 |
+
|
41 |
+
function usageExample(chatId) {
|
42 |
+
return bot.sendMessage(chatId,
|
43 |
+
`❌ الأمر غير صحيح أو غير مكتمل.\n\n` +
|
44 |
+
`استخدم أحد الأوامر التالية:\n\n` +
|
45 |
+
`/stream streamkey m3u8_url [cc_text]\n` +
|
46 |
+
`مثال:\n` +
|
47 |
+
`/stream FB-123456789 https://example.com/stream.m3u8 مرحباً بالعالم\n\n` +
|
48 |
+
`/stop stream_id\n` +
|
49 |
+
`مثال:\n` +
|
50 |
+
`/stop 1\n\n` +
|
51 |
+
`/check\n` +
|
52 |
+
`لعرض معلومات النظام`
|
53 |
+
);
|
54 |
+
}
|
55 |
+
|
56 |
+
// /stream
|
57 |
+
bot.onText(/^\/stream(?:\s+(.+))?$/, async (msg, match) => {
|
58 |
+
const chatId = msg.chat.id;
|
59 |
+
const argsText = match[1];
|
60 |
+
if (!argsText) return usageExample(chatId);
|
61 |
+
|
62 |
+
const args = argsText.trim().split(/\s+/);
|
63 |
+
if (args.length < 2) return usageExample(chatId);
|
64 |
+
|
65 |
+
const key = args[0];
|
66 |
+
const m3u8 = args[1];
|
67 |
+
const ccText = args.slice(2).join(' ');
|
68 |
+
|
69 |
+
if (!streamCounter[chatId]) streamCounter[chatId] = 0;
|
70 |
+
streamCounter[chatId]++;
|
71 |
+
const streamId = streamCounter[chatId];
|
72 |
+
|
73 |
+
try {
|
74 |
+
const { preset, bitrate, resolution } = await getSystemSettings();
|
75 |
+
|
76 |
+
let command = ffmpeg(m3u8)
|
77 |
+
.videoCodec('libx264')
|
78 |
+
.audioCodec('aac')
|
79 |
+
.outputOptions([
|
80 |
+
`-preset ${preset}`,
|
81 |
+
`-b:v ${bitrate}`,
|
82 |
+
'-f', 'flv'
|
83 |
+
])
|
84 |
+
.size(resolution);
|
85 |
+
|
86 |
+
if (ccText && ccText.trim() !== '') {
|
87 |
+
const filter = `drawbox=x=(w-text_w)/2-10:y=h-text_h-40:w=text_w+20:h=text_h+30:[email protected]:t=max,` +
|
88 |
+
`drawtext=text='${ccText}':fontcolor=white:fontsize=24:x=(w-text_w)/2:y=h-text_h-30`;
|
89 |
+
command = command.videoFilters(filter);
|
90 |
+
}
|
91 |
+
|
92 |
+
const rtmpUrl = `rtmps://live-api-s.facebook.com:443/rtmp/${key}`;
|
93 |
+
command = command.output(rtmpUrl);
|
94 |
+
|
95 |
+
const proc = command.spawn();
|
96 |
+
|
97 |
+
if (!userStreams[chatId]) userStreams[chatId] = {};
|
98 |
+
userStreams[chatId][streamId] = { proc };
|
99 |
+
|
100 |
+
const timeout = setTimeout(() => {
|
101 |
+
if (proc && !proc.killed) {
|
102 |
+
proc.kill('SIGTERM');
|
103 |
+
bot.sendMessage(chatId, `⏹️ تم إيقاف البث معرف ${streamId} تلقائياً بعد 4 ساعات.`);
|
104 |
+
delete userStreams[chatId][streamId];
|
105 |
+
}
|
106 |
+
}, 4 * 60 * 60 * 1000);
|
107 |
+
|
108 |
+
userStreams[chatId][streamId].timeout = timeout;
|
109 |
+
|
110 |
+
bot.sendMessage(chatId,
|
111 |
+
`✅ تم بدء البث بنجاح!\n\n` +
|
112 |
+
`▶️ معرف البث: ${streamId}\n` +
|
113 |
+
`الدقة: ${resolution}\n` +
|
114 |
+
`معدل البت: ${bitrate}\n` +
|
115 |
+
`الإعدادات: ${preset}\n` +
|
116 |
+
`⏳ سيتم إيقاف البث تلقائياً بعد 4 ساعات`
|
117 |
+
);
|
118 |
+
|
119 |
+
proc.on('error', err => {
|
120 |
+
bot.sendMessage(chatId, `❌ حدث خطأ في البث معرف ${streamId}:\n${err.message}`);
|
121 |
+
clearTimeout(timeout);
|
122 |
+
delete userStreams[chatId][streamId];
|
123 |
+
});
|
124 |
+
|
125 |
+
proc.on('exit', (code, signal) => {
|
126 |
+
if (signal === 'SIGTERM') {
|
127 |
+
bot.sendMessage(chatId, `⏹️ تم إيقاف البث معرف ${streamId}.`);
|
128 |
+
} else if (code !== 0) {
|
129 |
+
bot.sendMessage(chatId, `⚠️ البث معرف ${streamId} انتهى برمز خروج ${code}.`);
|
130 |
+
}
|
131 |
+
clearTimeout(timeout);
|
132 |
+
delete userStreams[chatId][streamId];
|
133 |
+
});
|
134 |
+
|
135 |
+
} catch (e) {
|
136 |
+
return bot.sendMessage(chatId, `❌ فشل بدء البث:\n${e.message}`);
|
137 |
+
}
|
138 |
+
});
|
139 |
+
|
140 |
+
// /stop
|
141 |
+
bot.onText(/^\/stop(?:\s+(\d+))?$/, (msg, match) => {
|
142 |
+
const chatId = msg.chat.id;
|
143 |
+
const streamId = match[1] ? Number(match[1]) : null;
|
144 |
+
if (!streamId) return usageExample(chatId);
|
145 |
+
|
146 |
+
if (!userStreams[chatId] || !userStreams[chatId][streamId]) {
|
147 |
+
return bot.sendMessage(chatId, `⚠️ لا يوجد بث نشط بالمعرف ${streamId}.`);
|
148 |
+
}
|
149 |
+
|
150 |
+
const { proc, timeout } = userStreams[chatId][streamId];
|
151 |
+
if (proc && !proc.killed) {
|
152 |
+
proc.kill('SIGTERM');
|
153 |
+
clearTimeout(timeout);
|
154 |
+
bot.sendMessage(chatId, `⏹️ تم إيقاف البث معرف ${streamId} من قبلك.`);
|
155 |
+
delete userStreams[chatId][streamId];
|
156 |
+
} else {
|
157 |
+
bot.sendMessage(chatId, `⚠️ البث معرف ${streamId} متوقف بالفعل.`);
|
158 |
+
}
|
159 |
+
});
|
160 |
+
|
161 |
+
// /check
|
162 |
+
bot.onText(/\/check/, async (msg) => {
|
163 |
+
const chatId = msg.chat.id;
|
164 |
+
try {
|
165 |
+
const cpu = await si.cpu();
|
166 |
+
const mem = await si.mem();
|
167 |
+
const osInfo = await si.osInfo();
|
168 |
+
|
169 |
+
const message = `
|
170 |
+
<b>معلومات النظام:</b>
|
171 |
+
نظام التشغيل: ${osInfo.distro} ${osInfo.release}
|
172 |
+
المعالج: ${cpu.manufacturer} ${cpu.brand} (${cpu.cores} أنوية)
|
173 |
+
الذاكرة: ${(mem.total / 1024 / 1024 / 1024).toFixed(2)} GB
|
174 |
+
`;
|
175 |
+
bot.sendMessage(chatId, message, { parse_mode: 'HTML' });
|
176 |
+
} catch (err) {
|
177 |
+
bot.sendMessage(chatId, `❌ خطأ في جلب معلومات النظام: ${err.message}`);
|
178 |
+
}
|
179 |
+
});
|
180 |
+
|
181 |
+
// رد افتراضي
|
182 |
+
bot.on('message', (msg) => {
|
183 |
+
const chatId = msg.chat.id;
|
184 |
+
const text = msg.text || "";
|
185 |
+
|
186 |
+
if (
|
187 |
+
text.startsWith('/stream') ||
|
188 |
+
text.startsWith('/stop') ||
|
189 |
+
text.startsWith('/check')
|
190 |
+
) return;
|
191 |
+
|
192 |
+
usageExample(chatId);
|
193 |
+
});
|
194 |
+
|
195 |
+
console.log('🤖 البوت يعمل الآن...');
|