Spaces:
Sleeping
Sleeping
Add error handling and logging to webhook endpoints
Browse files- telegram_handlers.py +66 -52
telegram_handlers.py
CHANGED
|
@@ -5,60 +5,74 @@ from ai_service import get_ai_response
|
|
| 5 |
from database import db_manager
|
| 6 |
|
| 7 |
async def telegram_webhook(request: Request):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
telegram_id = data["message"]["chat"]["id"]
|
| 12 |
-
user_text = data["message"].get("text", "")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
first_name = user_info.get("first_name")
|
| 18 |
-
last_name = user_info.get("last_name")
|
| 19 |
-
|
| 20 |
-
if user_text:
|
| 21 |
-
# Save user message to database if available
|
| 22 |
-
if db_manager:
|
| 23 |
-
db_manager.save_message(telegram_id, user_text, "user")
|
| 24 |
-
db_manager.create_or_update_user(telegram_id, username, first_name, last_name)
|
| 25 |
-
|
| 26 |
-
# Get the intelligent response with conversation history
|
| 27 |
-
ai_answer = await get_ai_response(user_text, telegram_id)
|
| 28 |
-
|
| 29 |
-
# Save assistant response to database if available
|
| 30 |
-
if db_manager:
|
| 31 |
-
db_manager.save_message(telegram_id, ai_answer, "assistant")
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
async def test_webhook(request: Request):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from database import db_manager
|
| 6 |
|
| 7 |
async def telegram_webhook(request: Request):
|
| 8 |
+
try:
|
| 9 |
+
data = await request.json()
|
| 10 |
+
print(f"Received webhook data: {data}")
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
if "message" in data:
|
| 13 |
+
telegram_id = data["message"]["chat"]["id"]
|
| 14 |
+
user_text = data["message"].get("text", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Extract user info
|
| 17 |
+
user_info = data["message"]["chat"]
|
| 18 |
+
username = user_info.get("username")
|
| 19 |
+
first_name = user_info.get("first_name")
|
| 20 |
+
last_name = user_info.get("last_name")
|
| 21 |
+
|
| 22 |
+
if user_text:
|
| 23 |
+
# Save user message to database if available
|
| 24 |
+
if db_manager:
|
| 25 |
+
db_manager.save_message(telegram_id, user_text, "user")
|
| 26 |
+
db_manager.create_or_update_user(telegram_id, username, first_name, last_name)
|
| 27 |
+
|
| 28 |
+
# Get the intelligent response with conversation history
|
| 29 |
+
ai_answer = await get_ai_response(user_text, telegram_id)
|
| 30 |
+
|
| 31 |
+
# Save assistant response to database if available
|
| 32 |
+
if db_manager:
|
| 33 |
+
db_manager.save_message(telegram_id, ai_answer, "assistant")
|
| 34 |
|
| 35 |
+
# Send back to Telegram if TELEGRAM_URL is available
|
| 36 |
+
if TELEGRAM_URL:
|
| 37 |
+
async with httpx.AsyncClient(verify=False) as client:
|
| 38 |
+
await client.post(
|
| 39 |
+
TELEGRAM_URL,
|
| 40 |
+
headers={"Host": "api.telegram.org"},
|
| 41 |
+
json={
|
| 42 |
+
"chat_id": telegram_id,
|
| 43 |
+
"text": ai_answer,
|
| 44 |
+
"parse_mode": "Markdown"
|
| 45 |
+
}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return {"status": "ok"}
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error in webhook: {str(e)}")
|
| 51 |
+
return {"status": "error", "message": str(e)}
|
| 52 |
|
| 53 |
async def test_webhook(request: Request):
|
| 54 |
+
try:
|
| 55 |
+
data = await request.json()
|
| 56 |
+
print(f"Received test webhook data: {data}")
|
| 57 |
+
|
| 58 |
+
if "message" not in data:
|
| 59 |
+
return {"error": "Missing 'message' field in request"}
|
| 60 |
+
|
| 61 |
+
telegram_id = data["message"]["chat"]["id"]
|
| 62 |
+
user_text = data["message"]["text"]
|
| 63 |
+
|
| 64 |
+
# Save user message to database if available
|
| 65 |
+
if db_manager:
|
| 66 |
+
db_manager.save_message(telegram_id, user_text, "user")
|
| 67 |
+
|
| 68 |
+
# Get response with conversation history
|
| 69 |
+
response = await get_ai_response(user_text, telegram_id)
|
| 70 |
+
|
| 71 |
+
# Save assistant response to database if available
|
| 72 |
+
if db_manager:
|
| 73 |
+
db_manager.save_message(telegram_id, response, "assistant")
|
| 74 |
+
|
| 75 |
+
return {"response": response}
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Error in test webhook: {str(e)}")
|
| 78 |
+
return {"error": str(e)}
|