Spaces:
Configuration error
Configuration error
| import os | |
| import requests | |
| from telegram import Update | |
| from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes | |
| # --- ضع مفاتيحك السرية هنا مباشرة --- | |
| # ================================================================= | |
| TELEGRAM_TOKEN = "4f3RezB0UeVmGoyTHptvsnhFAc" | |
| OPENROUTER_API_KEY = "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" | |
| # ================================================================= | |
| # يمكنك تغيير النموذج الافتراضي هنا في أي وقت | |
| DEFAULT_MODEL = "google/gemma-7b-it:free" | |
| # ذاكرة لحفظ سجل المحادثات لكل مستخدم على حدة | |
| user_conversations = {} | |
| # --- دوال البوت --- | |
| async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| """يرسل رسالة ترحيبية.""" | |
| user_id = update.message.from_user.id | |
| user_conversations[user_id] = [] | |
| await update.message.reply_text("مرحباً! أنا بوت الذكاء الاصطناعي الخاص بهاشم (نسخة نصية). أرسل لي أي سؤال وسأجيب عليه.") | |
| async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| """يعالج الرسائل النصية الواردة.""" | |
| user_id = update.message.from_user.id | |
| user_message = update.message.text | |
| if user_id not in user_conversations: | |
| user_conversations[user_id] = [] | |
| user_conversations[user_id].append({"role": "user", "content": user_message}) | |
| thinking_message = await update.message.reply_text("أفكر...") | |
| try: | |
| response = requests.post( | |
| url="https://openrouter.ai/api/v1/chat/completions", | |
| headers={ | |
| "Authorization": f"Bearer {OPENROUTER_API_KEY}", | |
| "Content-Type": "application/json" | |
| }, | |
| json={ | |
| "model": DEFAULT_MODEL, | |
| "messages": user_conversations[user_id] | |
| } | |
| ) | |
| response.raise_for_status() | |
| data = response.json() | |
| bot_response = data['choices'][0]['message']['content'] | |
| user_conversations[user_id].append({"role": "assistant", "content": bot_response}) | |
| await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=thinking_message.message_id, text=bot_response) | |
| except Exception as e: | |
| error_message = f"عذراً، حدث خطأ: {e}" | |
| await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=thinking_message.message_id, text=error_message) | |
| def main() -> None: | |
| """الدالة الرئيسية لتشغيل البوت.""" | |
| if not TELEGRAM_TOKEN or not OPENROUTER_API_KEY: | |
| print("خطأ فادح: يرجى التأكد من وضع التوكن والمفتاح في بداية الملف.") | |
| return | |
| application = Application.builder().token(TELEGRAM_TOKEN).build() | |
| application.add_handler(CommandHandler("start", start)) | |
| application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) | |
| print("البوت النصي بدأ بالعمل...") | |
| application.run_polling() | |
| if __name__ == "__main__": | |
| main() |