Spaces:
Sleeping
Sleeping
Fix: add missing json import in telegram_handlers.py
Browse files- telegram_handlers.py +27 -18
telegram_handlers.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from fastapi import Request
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import httpx
|
|
|
|
| 4 |
from config import TELEGRAM_URL
|
| 5 |
from ai_service import get_ai_response
|
| 6 |
from database import db_manager
|
|
@@ -46,31 +47,39 @@ 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 |
-
# 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":
|
| 59 |
}
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
print(f"DEBUG: Sending Payload: {payload}")
|
| 64 |
-
|
| 65 |
try:
|
| 66 |
-
#
|
| 67 |
response = await client.post(TELEGRAM_URL, json=payload)
|
| 68 |
except Exception as dns_err:
|
| 69 |
-
print(f"--- DNS Failed. Routing to
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
except Exception as send_error:
|
| 76 |
print(f"--- Emergency: Network Blockage Detected: {str(send_error)} ---")
|
|
|
|
| 1 |
from fastapi import Request
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import httpx
|
| 4 |
+
import json
|
| 5 |
from config import TELEGRAM_URL
|
| 6 |
from ai_service import get_ai_response
|
| 7 |
from database import db_manager
|
|
|
|
| 47 |
from config import TELEGRAM_TOKEN
|
| 48 |
|
| 49 |
async with httpx.AsyncClient(timeout=40.0, verify=False, follow_redirects=True) as client:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
payload = {
|
| 51 |
"chat_id": telegram_id,
|
| 52 |
+
"text": ai_answer or "Sorry, I couldn't generate a response. Please try again."
|
| 53 |
}
|
| 54 |
|
| 55 |
+
print(f"--- Payload being sent: {payload} ---")
|
| 56 |
+
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
+
# Attempt standard request
|
| 59 |
response = await client.post(TELEGRAM_URL, json=payload)
|
| 60 |
except Exception as dns_err:
|
| 61 |
+
print(f"--- DNS Failed. Forcing Direct IP Routing to 149.154.167.220 ---")
|
| 62 |
+
|
| 63 |
+
forced_ip_url = f"https://{TELEGRAM_IP}/bot{TELEGRAM_TOKEN}/sendMessage"
|
| 64 |
+
|
| 65 |
+
# Explicitly define headers and serialize the JSON manually
|
| 66 |
+
headers = {
|
| 67 |
+
"Host": "api.telegram.org",
|
| 68 |
+
"Content-Type": "application/json"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Use 'data' instead of 'json' to ensure absolute control over the body
|
| 72 |
+
response = await client.post(
|
| 73 |
+
forced_ip_url,
|
| 74 |
+
data=json.dumps(payload),
|
| 75 |
+
headers=headers
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if response.status_code == 200:
|
| 79 |
+
print(f"--- Success: Message delivered via Direct IP Pipeline ---")
|
| 80 |
+
else:
|
| 81 |
+
print(f"--- Payload: {payload} ---")
|
| 82 |
+
print(f"--- Telegram Rejected Request: {response.status_code} - {response.text} ---")
|
| 83 |
|
| 84 |
except Exception as send_error:
|
| 85 |
print(f"--- Emergency: Network Blockage Detected: {str(send_error)} ---")
|