codeBOKER commited on
Commit
75fe874
·
1 Parent(s): 690e4e2

Harden Telegram message send error handling

Browse files

Wrap outbound Telegram send in a try/except block, keep SSL verify disabled for current deployment constraints, and log non-200 API responses for easier debugging.

Made-with: Cursor

Files changed (1) hide show
  1. telegram_handlers.py +16 -10
telegram_handlers.py CHANGED
@@ -44,17 +44,23 @@ async def telegram_webhook(data: WebhookData):
44
  if db_manager:
45
  db_manager.save_message(telegram_id, ai_answer, "assistant")
46
 
47
- # 4. الإرسال لتليجرام (تم تنظيفه من الهيدرز الزائدة)
48
  if TELEGRAM_URL:
49
- async with httpx.AsyncClient(timeout=30.0) as client:
50
- payload = {
51
- "chat_id": telegram_id,
52
- "text": ai_answer,
53
- "parse_mode": "Markdown"
54
- }
55
- # حذفنا Host Header و verify=False (إلا لو كنت متأكداً من حاجتك لها)
56
- response = await client.post(TELEGRAM_URL, json=payload)
57
- print(f"Telegram response status: {response.status_code}")
 
 
 
 
 
 
58
 
59
  return {"status": "ok"}
60
  except Exception as e:
 
44
  if db_manager:
45
  db_manager.save_message(telegram_id, ai_answer, "assistant")
46
 
47
+
48
  if TELEGRAM_URL:
49
+ try:
50
+ async with httpx.AsyncClient(timeout=30.0, verify=False) as client:
51
+ payload = {
52
+ "chat_id": telegram_id,
53
+ "text": ai_answer,
54
+ "parse_mode": "Markdown"
55
+ }
56
+
57
+ response = await client.post(TELEGRAM_URL, json=payload)
58
+
59
+ if response.status_code != 200:
60
+ print(f"--- Telegram Error: {response.status_code} - {response.text} ---")
61
+
62
+ except Exception as send_error:
63
+ print(f"--- Failed to send to Telegram: {str(send_error)} ---")
64
 
65
  return {"status": "ok"}
66
  except Exception as e: