Spaces:
Sleeping
Sleeping
Add payload logging for debugging empty message issue
Browse files- telegram_handlers.py +16 -17
telegram_handlers.py
CHANGED
|
@@ -46,32 +46,31 @@ async def telegram_webhook(data: WebhookData):
|
|
| 46 |
from config import TELEGRAM_TOKEN
|
| 47 |
|
| 48 |
async with httpx.AsyncClient(timeout=40.0, verify=False, follow_redirects=True) as client:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
payload = {
|
| 50 |
"chat_id": telegram_id,
|
| 51 |
-
"text":
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
try:
|
|
|
|
| 57 |
response = await client.post(TELEGRAM_URL, json=payload)
|
| 58 |
except Exception as dns_err:
|
| 59 |
-
print(f"--- DNS Failed.
|
| 60 |
-
|
| 61 |
forced_ip_url = f"https://149.154.167.220/bot{TELEGRAM_TOKEN}/sendMessage"
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
"Host": "api.telegram.org",
|
| 65 |
-
"Content-Type": "application/json"
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
response = await client.post(forced_ip_url, json=payload, headers=headers)
|
| 69 |
-
|
| 70 |
-
if response.status_code == 200:
|
| 71 |
-
print(f"--- Success: Message delivered via Direct IP Pipeline ---")
|
| 72 |
-
else:
|
| 73 |
-
print(f"--- Payload: {payload} ---")
|
| 74 |
-
print(f"--- Telegram Rejected Request: {response.status_code} - {response.text} ---")
|
| 75 |
|
| 76 |
except Exception as send_error:
|
| 77 |
print(f"--- Emergency: Network Blockage Detected: {str(send_error)} ---")
|
|
|
|
| 46 |
from config import TELEGRAM_TOKEN
|
| 47 |
|
| 48 |
async with httpx.AsyncClient(timeout=40.0, verify=False, follow_redirects=True) as client:
|
| 49 |
+
# 1. Clean the input
|
| 50 |
+
clean_answer = str(ai_answer).strip() if ai_answer else ""
|
| 51 |
+
|
| 52 |
+
# 2. Ensure it's not empty
|
| 53 |
+
if not clean_answer:
|
| 54 |
+
clean_answer = "Default fallback: The AI returned an empty result."
|
| 55 |
+
|
| 56 |
payload = {
|
| 57 |
"chat_id": telegram_id,
|
| 58 |
+
"text": clean_answer
|
| 59 |
}
|
| 60 |
|
| 61 |
+
# 3. Explicitly log the type and content
|
| 62 |
+
print(f"DEBUG: ai_answer type: {type(ai_answer)}")
|
| 63 |
+
print(f"DEBUG: Sending Payload: {payload}")
|
| 64 |
+
|
| 65 |
try:
|
| 66 |
+
# Try standard URL first
|
| 67 |
response = await client.post(TELEGRAM_URL, json=payload)
|
| 68 |
except Exception as dns_err:
|
| 69 |
+
print(f"--- DNS Failed. Routing to IP ---")
|
|
|
|
| 70 |
forced_ip_url = f"https://149.154.167.220/bot{TELEGRAM_TOKEN}/sendMessage"
|
| 71 |
+
headers = {"Host": "api.telegram.org"}
|
| 72 |
+
# Use 'json=' to ensure httpx sets the correct Content-Type automatically
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
response = await client.post(forced_ip_url, json=payload, headers=headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
except Exception as send_error:
|
| 76 |
print(f"--- Emergency: Network Blockage Detected: {str(send_error)} ---")
|