Spaces:
Running
Running
Harden Telegram message send error handling
Browse filesWrap 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
- 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 |
-
|
| 48 |
if TELEGRAM_URL:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|