Spaces:
Running
Running
axeronis commited on
Commit ·
8a72cf3
1
Parent(s): e972df3
feat: Optimize media sending - Reply instead of Resend for products
Browse files- backend/app/api/webhook.py +34 -2
backend/app/api/webhook.py
CHANGED
|
@@ -194,15 +194,16 @@ async def send_product_images_if_mentioned(
|
|
| 194 |
bot_token: str,
|
| 195 |
chat_id: str,
|
| 196 |
ai_text: str,
|
| 197 |
-
|
|
|
|
| 198 |
) -> Optional[int]:
|
| 199 |
"""
|
| 200 |
Detect product names mentioned in AI response and send ALL their images.
|
| 201 |
Also saves image messages to database so they appear in chat UI.
|
| 202 |
"""
|
| 203 |
from app.models.product import Product
|
| 204 |
-
from app.models.lead import Message
|
| 205 |
from app.services.telegram_service import telegram_service
|
|
|
|
| 206 |
|
| 207 |
# Get all products with images for this tenant
|
| 208 |
stmt = select(Product).where(
|
|
@@ -222,6 +223,29 @@ async def send_product_images_if_mentioned(
|
|
| 222 |
# Check if product name is mentioned in AI response
|
| 223 |
product_name_lower = product.name.lower()
|
| 224 |
if product_name_lower in ai_text_lower:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
# Collect all image URLs
|
| 226 |
all_image_urls = []
|
| 227 |
|
|
@@ -249,6 +273,13 @@ async def send_product_images_if_mentioned(
|
|
| 249 |
if result["success"]:
|
| 250 |
last_message_id = result["message_id"]
|
| 251 |
logger.info(f"📷 Auto-sent {len(all_image_urls)} images as album for product: {product.name} (msg_id: {last_message_id})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# Save one message for the album in database
|
| 254 |
if conversation_id and result["success"]:
|
|
@@ -1862,6 +1893,7 @@ async def process_ai_response_background(
|
|
| 1862 |
bot_token=tenant.telegram_bot_token,
|
| 1863 |
chat_id=chat_id,
|
| 1864 |
ai_text=ai_response["text"],
|
|
|
|
| 1865 |
conversation_id=conversation.id
|
| 1866 |
)
|
| 1867 |
if prod_msg_id:
|
|
|
|
| 194 |
bot_token: str,
|
| 195 |
chat_id: str,
|
| 196 |
ai_text: str,
|
| 197 |
+
lead_id: UUID, # NEW: for media tracking
|
| 198 |
+
conversation_id: UUID = None
|
| 199 |
) -> Optional[int]:
|
| 200 |
"""
|
| 201 |
Detect product names mentioned in AI response and send ALL their images.
|
| 202 |
Also saves image messages to database so they appear in chat UI.
|
| 203 |
"""
|
| 204 |
from app.models.product import Product
|
|
|
|
| 205 |
from app.services.telegram_service import telegram_service
|
| 206 |
+
from app.services.ai_core.media_tracker import media_tracker # Import tracker
|
| 207 |
|
| 208 |
# Get all products with images for this tenant
|
| 209 |
stmt = select(Product).where(
|
|
|
|
| 223 |
# Check if product name is mentioned in AI response
|
| 224 |
product_name_lower = product.name.lower()
|
| 225 |
if product_name_lower in ai_text_lower:
|
| 226 |
+
product_id_str = str(product.id)
|
| 227 |
+
lead_id_str = str(lead_id)
|
| 228 |
+
|
| 229 |
+
# CHECK TRACKER: Already sent?
|
| 230 |
+
if not media_tracker.should_send_product_media(lead_id_str, product_id_str):
|
| 231 |
+
# Already sent! Get cached message_id to Reply instead
|
| 232 |
+
cached_msg_id = media_tracker.get_message_id_for_product(lead_id_str, product_id_str)
|
| 233 |
+
if cached_msg_id:
|
| 234 |
+
logger.info(f"♻️ Product {product.name} already sent. Using cached msg_id {cached_msg_id} for reply.")
|
| 235 |
+
try:
|
| 236 |
+
last_message_id = int(cached_msg_id)
|
| 237 |
+
# We found the product reference to reply to.
|
| 238 |
+
# Stop searching (or continue if we want to support multiple? AI usually replies to one)
|
| 239 |
+
# Let's break to reply to the first found product
|
| 240 |
+
return last_message_id
|
| 241 |
+
except:
|
| 242 |
+
pass
|
| 243 |
+
# If no cached ID (e.g. from previous session), we might skip or re-send.
|
| 244 |
+
# To be safe and avoid duplicates without context, we skip.
|
| 245 |
+
# BUT if user asked for it specifically?
|
| 246 |
+
# Let's rely on should_send_product_media returning False.
|
| 247 |
+
continue
|
| 248 |
+
|
| 249 |
# Collect all image URLs
|
| 250 |
all_image_urls = []
|
| 251 |
|
|
|
|
| 273 |
if result["success"]:
|
| 274 |
last_message_id = result["message_id"]
|
| 275 |
logger.info(f"📷 Auto-sent {len(all_image_urls)} images as album for product: {product.name} (msg_id: {last_message_id})")
|
| 276 |
+
|
| 277 |
+
# TRACK SENT STATUS
|
| 278 |
+
media_tracker.mark_product_sent(
|
| 279 |
+
lead_id_str,
|
| 280 |
+
product_id_str,
|
| 281 |
+
message_id=str(last_message_id)
|
| 282 |
+
)
|
| 283 |
|
| 284 |
# Save one message for the album in database
|
| 285 |
if conversation_id and result["success"]:
|
|
|
|
| 1893 |
bot_token=tenant.telegram_bot_token,
|
| 1894 |
chat_id=chat_id,
|
| 1895 |
ai_text=ai_response["text"],
|
| 1896 |
+
lead_id=lead.id, # Pass Lead ID
|
| 1897 |
conversation_id=conversation.id
|
| 1898 |
)
|
| 1899 |
if prod_msg_id:
|