const TelegramBot = require('node-telegram-bot-api'); const express = require('express'); const axios = require('axios'); const data = require('./data'); const app = express(); const port = process.env.PORT || 3000; // إعداد البوت const bot = new TelegramBot('7330777255:AAHDypo5Jno6YH_BSmwQ90UtPDMWhNap68o', { polling: true, request: { timeout: 30000 } }); // تحقق من صلاحية الأدمن function isAdmin(userId) { return userId === data.adminId; } // تشغيل السيرفر app.get('/', (req, res) => res.send('🤖 البوت يعمل بشكل صحيح')); app.listen(port, () => console.log(`السيرفر يعمل على البورت ${port}`)); // معالجة أمر /start bot.onText(/\/start/, (msg) => { const chatId = msg.chat.id; if (!data.users.has(chatId)) { data.users.add(chatId); } bot.sendMessage(chatId, data.welcomeMessage); }); // معالجة الأسئلة العادية bot.on('message', async (msg) => { const chatId = msg.chat.id; const userId = msg.from.id; if (msg.text && !msg.text.startsWith('/')) { try { const aiResponse = await axios.post('https://api.aimlapi.com/predict', { model: "gpt-4-turbo", messages: [{ role: "user", content: msg.text }], max_tokens: 1500, temperature: 0.7 }, { headers: { 'Authorization': `Bearer ${data.aimlapiKey}`, 'Content-Type': 'application/json' }, timeout: 20000 }); const answer = aiResponse.data.choices[0].message.content; bot.sendMessage(chatId, answer, { parse_mode: 'Markdown' }); } catch (error) { console.error('خطأ في API:', error.response?.data || error.message); bot.sendMessage(chatId, '⚠️ حدث خطأ أثناء معالجة سؤالك، يرجى المحاولة لاحقًا.'); } } }); // أوامر الأدمن bot.onText(/\/setwelcome (.+)/, (msg, match) => { const userId = msg.from.id; if (isAdmin(userId)) { data.welcomeMessage = match[1]; data.saveWelcomeMessage(); bot.sendMessage(msg.chat.id, '✅ تم تحديث رسالة الترحيب بنجاح!'); } }); bot.onText(/\/broadcast (.+)/, (msg, match) => { const userId = msg.from.id; if (isAdmin(userId)) { const message = match[1]; data.users.forEach(user => { bot.sendMessage(user, `📢 إشعار عام:\n${message}`); }); bot.sendMessage(msg.chat.id, `✔️ تم الإرسال لـ ${data.users.size} مستخدم`); } }); // الحفاظ على البوت نشطًا setInterval(() => { axios.get(`https://${process.env.PROJECT_DOMAIN}.glitch.me/`) .then(() => console.log('تم تجنب السكون ✅')) .catch(err => console.error('خطأ في الحفاظ على النشاط:', err)); }, 280000);