Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import asyncio
|
|
| 4 |
import re
|
| 5 |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
| 6 |
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters, ContextTypes, ConversationHandler
|
| 7 |
-
from telegram.request import HTTPXRequest
|
| 8 |
from huggingface_hub import HfApi, hf_hub_download, list_repo_files
|
| 9 |
from openai import OpenAI
|
| 10 |
import pickle
|
|
@@ -52,12 +51,20 @@ class MedicalLabBot:
|
|
| 52 |
self.conversation_memory = {}
|
| 53 |
self.available_materials = {}
|
| 54 |
self.file_cache = {}
|
| 55 |
-
self.load_all_materials()
|
| 56 |
self.application = None
|
|
|
|
| 57 |
|
| 58 |
def initialize_application(self):
|
| 59 |
"""تهيئة تطبيق التليجرام"""
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
self.application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
| 62 |
self.setup_handlers()
|
| 63 |
|
|
@@ -82,6 +89,7 @@ class MedicalLabBot:
|
|
| 82 |
|
| 83 |
self.application.add_handler(conv_handler)
|
| 84 |
self.application.add_handler(CallbackQueryHandler(self.handle_callback_query, pattern='^more_questions|change_subject|back_to_actions$'))
|
|
|
|
| 85 |
|
| 86 |
def load_all_materials(self):
|
| 87 |
"""تحميل جميع المواد والملفات من Hugging Face"""
|
|
@@ -793,20 +801,26 @@ class MedicalLabBot:
|
|
| 793 |
|
| 794 |
# ========== إنشاء كائن البوت ==========
|
| 795 |
bot = MedicalLabBot()
|
| 796 |
-
bot.initialize_application()
|
| 797 |
|
| 798 |
# ========== دوال FastAPI ==========
|
| 799 |
@app.on_event("startup")
|
| 800 |
async def on_startup():
|
| 801 |
"""إعداد Webhook عند بدء التشغيل"""
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 810 |
|
| 811 |
@app.get("/", response_class=HTMLResponse)
|
| 812 |
async def root():
|
|
@@ -852,7 +866,7 @@ async def handle_telegram_update(request: Request):
|
|
| 852 |
await bot.application.process_update(update)
|
| 853 |
return {"status": "ok"}
|
| 854 |
except Exception as e:
|
| 855 |
-
logger.error(f"Error processing update: {e}")
|
| 856 |
raise HTTPException(status_code=400, detail="Invalid update")
|
| 857 |
|
| 858 |
@app.get("/health")
|
|
|
|
| 4 |
import re
|
| 5 |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
| 6 |
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters, ContextTypes, ConversationHandler
|
|
|
|
| 7 |
from huggingface_hub import HfApi, hf_hub_download, list_repo_files
|
| 8 |
from openai import OpenAI
|
| 9 |
import pickle
|
|
|
|
| 51 |
self.conversation_memory = {}
|
| 52 |
self.available_materials = {}
|
| 53 |
self.file_cache = {}
|
|
|
|
| 54 |
self.application = None
|
| 55 |
+
self.load_all_materials()
|
| 56 |
|
| 57 |
def initialize_application(self):
|
| 58 |
"""تهيئة تطبيق التليجرام"""
|
| 59 |
+
try:
|
| 60 |
+
if not self.application:
|
| 61 |
+
# استخدام تكوين مبسط لتجنب مشكلة proxies
|
| 62 |
+
self.application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
| 63 |
+
self.setup_handlers()
|
| 64 |
+
logger.info("✅ تم تهيئة تطبيق التليجرام بنجاح")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
logger.error(f"❌ خطأ في تهيئة التطبيق: {e}")
|
| 67 |
+
# محاولة بديلة
|
| 68 |
self.application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
| 69 |
self.setup_handlers()
|
| 70 |
|
|
|
|
| 89 |
|
| 90 |
self.application.add_handler(conv_handler)
|
| 91 |
self.application.add_handler(CallbackQueryHandler(self.handle_callback_query, pattern='^more_questions|change_subject|back_to_actions$'))
|
| 92 |
+
logger.info("✅ تم إعداد معالجات التليجرام")
|
| 93 |
|
| 94 |
def load_all_materials(self):
|
| 95 |
"""تحميل جميع المواد والملفات من Hugging Face"""
|
|
|
|
| 801 |
|
| 802 |
# ========== إنشاء كائن البوت ==========
|
| 803 |
bot = MedicalLabBot()
|
|
|
|
| 804 |
|
| 805 |
# ========== دوال FastAPI ==========
|
| 806 |
@app.on_event("startup")
|
| 807 |
async def on_startup():
|
| 808 |
"""إعداد Webhook عند بدء التشغيل"""
|
| 809 |
+
try:
|
| 810 |
+
# تهيئة التطبيق
|
| 811 |
+
bot.initialize_application()
|
| 812 |
+
|
| 813 |
+
if WEBHOOK_URL:
|
| 814 |
+
await bot.application.bot.set_webhook(
|
| 815 |
+
url=f"{WEBHOOK_URL}/telegram",
|
| 816 |
+
allowed_updates=Update.ALL_TYPES
|
| 817 |
+
)
|
| 818 |
+
logger.info(f"✅ Webhook set to: {WEBHOOK_URL}/telegram")
|
| 819 |
+
else:
|
| 820 |
+
logger.warning("⚠️ WEBHOOK_URL not set, using polling instead")
|
| 821 |
+
|
| 822 |
+
except Exception as e:
|
| 823 |
+
logger.error(f"❌ Error during startup: {e}")
|
| 824 |
|
| 825 |
@app.get("/", response_class=HTMLResponse)
|
| 826 |
async def root():
|
|
|
|
| 866 |
await bot.application.process_update(update)
|
| 867 |
return {"status": "ok"}
|
| 868 |
except Exception as e:
|
| 869 |
+
logger.error(f"❌ Error processing update: {e}")
|
| 870 |
raise HTTPException(status_code=400, detail="Invalid update")
|
| 871 |
|
| 872 |
@app.get("/health")
|