File size: 3,047 Bytes
bf635ca |
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 |
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); |