Riy777 commited on
Commit
5034a9d
·
verified ·
1 Parent(s): b0f0343

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -136,7 +136,7 @@ class MedicalLabBot:
136
  logger.info("🔄 جاري تهيئة تطبيق التليجرام...")
137
 
138
  # ========== الحل الجذري (DNS) ==========
139
- logger.info("🔧 إعداد عميل HTTP مخصص مع CustomDNSTransport...")
140
  custom_transport = CustomDNSTransport()
141
  custom_client = httpx.AsyncClient(transport=custom_transport)
142
  # ============================================
@@ -328,7 +328,7 @@ class MedicalLabBot:
328
  match = re.search(r'(?:lecture|lec|محاضرة)\s*(\d+)', name_lower)
329
  if not match:
330
  # إذا لم يجد الصيغة السابقة، يبحث عن رقم لوحده في بداية أو نهاية الاسم
331
- match = re.search(r'^(\d+)\s*-|[\s_-](\d+)$', name_lower)
332
  if match:
333
  # يأخذ الرقم الثاني إذا وجد (المجموعة الثانية)، وإلا الأول
334
  lecture_num_str = match.group(2) or match.group(1)
@@ -1084,8 +1084,7 @@ async def root():
1084
  </html>
1085
  """
1086
 
1087
- # ========== !! الحل البديل المضمون: تحديد نوع الاستجابة صراحة !! ==========
1088
- @app.post("/telegram", response_model=Dict[str, str])
1089
  async def handle_telegram_update(request: Request):
1090
  """معالجة تحديثات Telegram"""
1091
  try:
@@ -1093,7 +1092,10 @@ async def handle_telegram_update(request: Request):
1093
  if not bot.is_initialized or not bot.application:
1094
  logger.error("❌ التطبيق غير مهيأ، لا يمكن معالجة التحديث (ربما لا يزال قيد التهيئة).")
1095
  # إرجاع 503 Service Unavailable لإخبار Telegram بإعادة المحاولة لاحقًا
1096
- raise HTTPException(status_code=503, detail="Application not initialized or still initializing")
 
 
 
1097
 
1098
  update_data = await request.json()
1099
  # logger.debug(f"Received update: {update_data}") # إلغاء التعليق للتتبع المكثف
@@ -1103,22 +1105,23 @@ async def handle_telegram_update(request: Request):
1103
  asyncio.create_task(bot.application.process_update(update))
1104
 
1105
  # إرجاع استجابة سريعة لـ Telegram
1106
- return {"status": "ok"}
1107
 
1108
  except json.JSONDecodeError:
1109
  logger.error("❌ Received invalid JSON data.")
1110
- raise HTTPException(status_code=400, detail="Invalid JSON data")
1111
- except HTTPException as http_exc:
1112
- # إعادة إرسال الخطأ HTTP كما هو (مثل 503 أعلاه)
1113
- raise http_exc
1114
  except Exception as e:
1115
  logger.error(f"❌ Error processing update: {e}", exc_info=True)
1116
  # إرجاع خطأ عام 500 Internal Server Error
1117
- raise HTTPException(status_code=500, detail="Internal server error while processing update")
1118
-
 
 
1119
 
1120
- # ========== تعديل احترازي: تحديد نوع الاستجابة لمسار health أيضاً ==========
1121
- @app.get("/health", response_model=Dict[str, Any])
1122
  async def health_check():
1123
  """فحص صحة الخدمة"""
1124
  materials_count = len(bot.available_materials)
 
136
  logger.info("🔄 جاري تهيئة تطبيق التليجرام...")
137
 
138
  # ========== الحل الجذري (DNS) ==========
139
+ logger.info("🔧 إعداد عملي HTTP مخصص مع CustomDNSTransport...")
140
  custom_transport = CustomDNSTransport()
141
  custom_client = httpx.AsyncClient(transport=custom_transport)
142
  # ============================================
 
328
  match = re.search(r'(?:lecture|lec|محاضرة)\s*(\d+)', name_lower)
329
  if not match:
330
  # إذا لم يجد الصيغة السابقة، يبحث عن رقم لوحده في بداية أو نهاية الاسم
331
+ match = re.search(r('^(\d+)\s*-|[\s_-](\d+)$', name_lower)
332
  if match:
333
  # يأخذ الرقم الثاني إذا وجد (المجموعة الثانية)، وإلا الأول
334
  lecture_num_str = match.group(2) or match.group(1)
 
1084
  </html>
1085
  """
1086
 
1087
+ @app.post("/telegram")
 
1088
  async def handle_telegram_update(request: Request):
1089
  """معالجة تحديثات Telegram"""
1090
  try:
 
1092
  if not bot.is_initialized or not bot.application:
1093
  logger.error("❌ التطبيق غير مهيأ، لا يمكن معالجة التحديث (ربما لا يزال قيد التهيئة).")
1094
  # إرجاع 503 Service Unavailable لإخبار Telegram بإعادة المحاولة لاحقًا
1095
+ return JSONResponse(
1096
+ status_code=503,
1097
+ content={"status": "error", "detail": "Application not initialized or still initializing"}
1098
+ )
1099
 
1100
  update_data = await request.json()
1101
  # logger.debug(f"Received update: {update_data}") # إلغاء التعليق للتتبع المكثف
 
1105
  asyncio.create_task(bot.application.process_update(update))
1106
 
1107
  # إرجاع استجابة سريعة لـ Telegram
1108
+ return JSONResponse(content={"status": "ok"})
1109
 
1110
  except json.JSONDecodeError:
1111
  logger.error("❌ Received invalid JSON data.")
1112
+ return JSONResponse(
1113
+ status_code=400,
1114
+ content={"status": "error", "detail": "Invalid JSON data"}
1115
+ )
1116
  except Exception as e:
1117
  logger.error(f"❌ Error processing update: {e}", exc_info=True)
1118
  # إرجاع خطأ عام 500 Internal Server Error
1119
+ return JSONResponse(
1120
+ status_code=500,
1121
+ content={"status": "error", "detail": "Internal server error while processing update"}
1122
+ )
1123
 
1124
+ @app.get("/health")
 
1125
  async def health_check():
1126
  """فحص صحة الخدمة"""
1127
  materials_count = len(bot.available_materials)