Spaces:
Running
Running
axeronis commited on
Commit ·
cca9494
1
Parent(s): 10e7b9a
feat: add reply_to_message support so AI understands when user replies to specific message
Browse files- backend/app/api/webhook.py +25 -0
backend/app/api/webhook.py
CHANGED
|
@@ -390,6 +390,27 @@ async def telegram_webhook(
|
|
| 390 |
|
| 391 |
# Extract text (or caption from media) or transcribe voice
|
| 392 |
user_text = message.get("text") or message.get("caption") or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
voice_file_id = voice.get("file_id") if voice else None
|
| 394 |
photo_file_id = photo_list[-1]["file_id"] if photo_list else None
|
| 395 |
doc_file_id = document.get("file_id") if document else None
|
|
@@ -432,6 +453,10 @@ async def telegram_webhook(
|
|
| 432 |
if not user_text:
|
| 433 |
user_text = "[Медиа-сообщение без содержимого]"
|
| 434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
# Tenant already fetched and validated above
|
| 436 |
|
| 437 |
# Find or create lead
|
|
|
|
| 390 |
|
| 391 |
# Extract text (or caption from media) or transcribe voice
|
| 392 |
user_text = message.get("text") or message.get("caption") or ""
|
| 393 |
+
|
| 394 |
+
# Handle reply_to_message - include context of quoted message
|
| 395 |
+
reply_to = message.get("reply_to_message")
|
| 396 |
+
reply_context = ""
|
| 397 |
+
if reply_to:
|
| 398 |
+
# Extract quoted message content
|
| 399 |
+
quoted_text = reply_to.get("text") or reply_to.get("caption") or ""
|
| 400 |
+
quoted_from = reply_to.get("from", {})
|
| 401 |
+
is_from_bot = quoted_from.get("is_bot", False)
|
| 402 |
+
|
| 403 |
+
if quoted_text:
|
| 404 |
+
quoted_source = "бот" if is_from_bot else "клиент"
|
| 405 |
+
reply_context = f"[В ответ на сообщение {quoted_source}: \"{quoted_text[:200]}{'...' if len(quoted_text) > 200 else ''}\"] "
|
| 406 |
+
logger.info(f"↩️ Reply detected: quoting {quoted_source} message")
|
| 407 |
+
elif reply_to.get("photo"):
|
| 408 |
+
reply_context = "[В ответ на фото] "
|
| 409 |
+
logger.info("↩️ Reply to photo detected")
|
| 410 |
+
elif reply_to.get("voice") or reply_to.get("audio"):
|
| 411 |
+
reply_context = "[В ответ на голосовое сообщение] "
|
| 412 |
+
logger.info("↩️ Reply to voice detected")
|
| 413 |
+
|
| 414 |
voice_file_id = voice.get("file_id") if voice else None
|
| 415 |
photo_file_id = photo_list[-1]["file_id"] if photo_list else None
|
| 416 |
doc_file_id = document.get("file_id") if document else None
|
|
|
|
| 453 |
if not user_text:
|
| 454 |
user_text = "[Медиа-сообщение без содержимого]"
|
| 455 |
|
| 456 |
+
# Prepend reply context so AI knows what message user is replying to
|
| 457 |
+
if reply_context:
|
| 458 |
+
user_text = reply_context + user_text
|
| 459 |
+
|
| 460 |
# Tenant already fetched and validated above
|
| 461 |
|
| 462 |
# Find or create lead
|