Spaces:
Running
Running
axeronis commited on
Commit Β·
34739db
1
Parent(s): d223b85
Debug: Add detailed logging for media upload failures
Browse files- backend/app/api/webhook.py +59 -17
backend/app/api/webhook.py
CHANGED
|
@@ -375,28 +375,38 @@ async def telegram_webhook(
|
|
| 375 |
|
| 376 |
mime_type = get_mime_type(file_ext)
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
else:
|
| 396 |
logger.warning("β οΈ Supabase client not available, media will not be uploaded")
|
| 397 |
|
| 398 |
except Exception as e:
|
|
|
|
| 399 |
logger.error(f"β Failed to download/upload media: {e}")
|
|
|
|
| 400 |
# Continue without media - at least save the transcribed text
|
| 401 |
|
| 402 |
# Save user message WITH media fields
|
|
@@ -411,6 +421,22 @@ async def telegram_webhook(
|
|
| 411 |
)
|
| 412 |
session.add(user_message)
|
| 413 |
await session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
|
| 415 |
# Extract real name from message (avoid using Telegram nicknames)
|
| 416 |
try:
|
|
@@ -1099,6 +1125,22 @@ async def process_ai_response_background(
|
|
| 1099 |
)
|
| 1100 |
session.add(ai_message)
|
| 1101 |
await session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1102 |
|
| 1103 |
# Send response to Telegram
|
| 1104 |
logger.info(
|
|
|
|
| 375 |
|
| 376 |
mime_type = get_mime_type(file_ext)
|
| 377 |
|
| 378 |
+
try:
|
| 379 |
+
logger.info(f"β¬οΈ Uploading to Supabase: {path} ({mime_type})")
|
| 380 |
+
upload_result = supabase_client.storage.from_(bucket).upload(
|
| 381 |
+
path=path,
|
| 382 |
+
file=file_bytes,
|
| 383 |
+
file_options={"content-type": mime_type}
|
| 384 |
+
)
|
| 385 |
+
media_url = supabase_client.storage.from_(bucket).get_public_url(path)
|
| 386 |
+
logger.info(f"β
Uploaded to Supabase: {media_url[:80]}...")
|
| 387 |
+
|
| 388 |
+
# 3. Set media metadata
|
| 389 |
+
media_type = mime_type
|
| 390 |
+
if voice_file_id:
|
| 391 |
+
media_name = "voice.ogg"
|
| 392 |
+
elif photo_file_id:
|
| 393 |
+
media_name = f"photo.{file_ext}"
|
| 394 |
+
elif doc_file_id:
|
| 395 |
+
media_name = doc_file_name or f"document.{file_ext}"
|
| 396 |
+
except Exception as upload_err:
|
| 397 |
+
import traceback
|
| 398 |
+
logger.error(f"β Supabase upload failed: {upload_err}")
|
| 399 |
+
logger.error(f"π Traceback: {traceback.format_exc()}")
|
| 400 |
+
# Check if bucket exists issue
|
| 401 |
+
if "not found" in str(upload_err).lower() or "bucket" in str(upload_err).lower():
|
| 402 |
+
logger.error(f"π‘ Hint: Check if bucket '{bucket}' exists in Supabase Storage")
|
| 403 |
else:
|
| 404 |
logger.warning("β οΈ Supabase client not available, media will not be uploaded")
|
| 405 |
|
| 406 |
except Exception as e:
|
| 407 |
+
import traceback
|
| 408 |
logger.error(f"β Failed to download/upload media: {e}")
|
| 409 |
+
logger.error(f"π Full traceback: {traceback.format_exc()}")
|
| 410 |
# Continue without media - at least save the transcribed text
|
| 411 |
|
| 412 |
# Save user message WITH media fields
|
|
|
|
| 421 |
)
|
| 422 |
session.add(user_message)
|
| 423 |
await session.commit()
|
| 424 |
+
await session.refresh(user_message)
|
| 425 |
+
|
| 426 |
+
# Notify WebSocket clients about new message (real-time updates)
|
| 427 |
+
try:
|
| 428 |
+
from app.api.websocket_chat import notify_new_message
|
| 429 |
+
await notify_new_message(str(lead.id), {
|
| 430 |
+
"id": str(user_message.id),
|
| 431 |
+
"content": user_message.content,
|
| 432 |
+
"sender_type": "user",
|
| 433 |
+
"created_at": user_message.created_at.isoformat() if user_message.created_at else None,
|
| 434 |
+
"media_url": user_message.media_url,
|
| 435 |
+
"media_type": user_message.media_type
|
| 436 |
+
})
|
| 437 |
+
logger.debug(f"π‘ WebSocket notified: user message {user_message.id}")
|
| 438 |
+
except Exception as e:
|
| 439 |
+
logger.warning(f"β οΈ WebSocket notify failed (non-critical): {e}")
|
| 440 |
|
| 441 |
# Extract real name from message (avoid using Telegram nicknames)
|
| 442 |
try:
|
|
|
|
| 1125 |
)
|
| 1126 |
session.add(ai_message)
|
| 1127 |
await session.commit()
|
| 1128 |
+
await session.refresh(ai_message)
|
| 1129 |
+
|
| 1130 |
+
# Notify WebSocket clients about AI response (real-time updates)
|
| 1131 |
+
try:
|
| 1132 |
+
from app.api.websocket_chat import notify_new_message
|
| 1133 |
+
await notify_new_message(str(lead_id), {
|
| 1134 |
+
"id": str(ai_message.id),
|
| 1135 |
+
"content": ai_message.content,
|
| 1136 |
+
"sender_type": "ai",
|
| 1137 |
+
"created_at": ai_message.created_at.isoformat() if ai_message.created_at else None,
|
| 1138 |
+
"media_url": None,
|
| 1139 |
+
"media_type": None
|
| 1140 |
+
})
|
| 1141 |
+
logger.debug(f"π‘ WebSocket notified: AI message {ai_message.id}")
|
| 1142 |
+
except Exception as e:
|
| 1143 |
+
logger.warning(f"β οΈ WebSocket notify failed (non-critical): {e}")
|
| 1144 |
|
| 1145 |
# Send response to Telegram
|
| 1146 |
logger.info(
|