Spaces:
Sleeping
Sleeping
Improve Telegram delivery with DNS fallback and update REST sample
Browse filesAdd direct-IP fallback when Telegram API hostname fails to resolve, enable redirect following, and adjust rest.http ai-test request method.
Made-with: Cursor
- rest.http +1 -1
- telegram_handlers.py +15 -9
rest.http
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
Content-Type: application/json
|
| 3 |
|
| 4 |
{
|
|
|
|
| 1 |
+
GET https://codeboker-customer-service.hf.space/ai-test HTTP/1.1
|
| 2 |
Content-Type: application/json
|
| 3 |
|
| 4 |
{
|
telegram_handlers.py
CHANGED
|
@@ -5,6 +5,8 @@ from config import TELEGRAM_URL
|
|
| 5 |
from ai_service import get_ai_response
|
| 6 |
from database import db_manager
|
| 7 |
|
|
|
|
|
|
|
| 8 |
class ChatInfo(BaseModel):
|
| 9 |
id: int
|
| 10 |
username: str = None
|
|
@@ -26,41 +28,45 @@ async def telegram_webhook(data: WebhookData):
|
|
| 26 |
telegram_id = data.message.chat.id
|
| 27 |
user_text = data.message.text
|
| 28 |
|
| 29 |
-
# معلومات المستخدم
|
| 30 |
username = data.message.chat.username
|
| 31 |
first_name = data.message.chat.first_name
|
| 32 |
|
| 33 |
print(f"--- Processing message from {first_name} ---")
|
| 34 |
|
| 35 |
-
# 1. تحديث بيانات المستخدم وحفظ رسالته (مرة واحدة فقط هنا)
|
| 36 |
if db_manager:
|
| 37 |
db_manager.create_or_update_user(telegram_id, username, first_name, data.message.chat.last_name)
|
| 38 |
db_manager.save_message(telegram_id, user_text, "user")
|
| 39 |
|
| 40 |
-
# 2. جلب الرد (تأكد من إزالة حفظ الرسالة المتكرر داخل get_ai_response)
|
| 41 |
ai_answer = await get_ai_response(user_text, telegram_id)
|
| 42 |
|
| 43 |
-
# 3. حفظ رد البوت
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
if response.status_code
|
|
|
|
|
|
|
| 60 |
print(f"--- Telegram Error: {response.status_code} - {response.text} ---")
|
| 61 |
|
| 62 |
except Exception as send_error:
|
| 63 |
-
print(f"---
|
| 64 |
|
| 65 |
return {"status": "ok"}
|
| 66 |
except Exception as e:
|
|
|
|
| 5 |
from ai_service import get_ai_response
|
| 6 |
from database import db_manager
|
| 7 |
|
| 8 |
+
TELEGRAM_IP = "149.154.167.220"
|
| 9 |
+
|
| 10 |
class ChatInfo(BaseModel):
|
| 11 |
id: int
|
| 12 |
username: str = None
|
|
|
|
| 28 |
telegram_id = data.message.chat.id
|
| 29 |
user_text = data.message.text
|
| 30 |
|
|
|
|
| 31 |
username = data.message.chat.username
|
| 32 |
first_name = data.message.chat.first_name
|
| 33 |
|
| 34 |
print(f"--- Processing message from {first_name} ---")
|
| 35 |
|
|
|
|
| 36 |
if db_manager:
|
| 37 |
db_manager.create_or_update_user(telegram_id, username, first_name, data.message.chat.last_name)
|
| 38 |
db_manager.save_message(telegram_id, user_text, "user")
|
| 39 |
|
|
|
|
| 40 |
ai_answer = await get_ai_response(user_text, telegram_id)
|
| 41 |
|
|
|
|
| 42 |
if db_manager:
|
| 43 |
db_manager.save_message(telegram_id, ai_answer, "assistant")
|
| 44 |
|
|
|
|
| 45 |
if TELEGRAM_URL:
|
| 46 |
try:
|
| 47 |
+
async with httpx.AsyncClient(timeout=30.0, verify=False, follow_redirects=True) as client:
|
| 48 |
payload = {
|
| 49 |
"chat_id": telegram_id,
|
| 50 |
"text": ai_answer,
|
| 51 |
"parse_mode": "Markdown"
|
| 52 |
}
|
| 53 |
|
| 54 |
+
try:
|
| 55 |
+
response = await client.post(TELEGRAM_URL, json=payload)
|
| 56 |
+
except Exception as dns_err:
|
| 57 |
+
print(f"--- DNS Failed (Errno -5), trying direct IP routing... ---")
|
| 58 |
+
ip_url = TELEGRAM_URL.replace("api.telegram.org", TELEGRAM_IP)
|
| 59 |
+
headers = {"Host": "api.telegram.org"}
|
| 60 |
+
|
| 61 |
+
response = await client.post(ip_url, json=payload, headers=headers)
|
| 62 |
|
| 63 |
+
if response.status_code == 200:
|
| 64 |
+
print(f"--- Success: Message delivered to {first_name} ---")
|
| 65 |
+
else:
|
| 66 |
print(f"--- Telegram Error: {response.status_code} - {response.text} ---")
|
| 67 |
|
| 68 |
except Exception as send_error:
|
| 69 |
+
print(f"--- Critical: All sending attempts failed: {str(send_error)} ---")
|
| 70 |
|
| 71 |
return {"status": "ok"}
|
| 72 |
except Exception as e:
|