Spaces:
Running

axeronis commited on
Commit
101c2eb
ยท
1 Parent(s): bb11f5e

feat: auto-send product images when mentioned - no URLs in chat

Browse files
backend/app/api/webhook.py CHANGED
@@ -119,6 +119,47 @@ def _extract_booking_info_from_conversation(conversation_history, lead, user_tex
119
  return info
120
 
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  router = APIRouter(prefix="/webhook", tags=["Webhooks"])
123
 
124
 
@@ -890,6 +931,16 @@ async def process_ai_response_background(
890
  chat_id=chat_id,
891
  text=ai_response["text"]
892
  )
 
 
 
 
 
 
 
 
 
 
893
  elif channel == 'linkedin':
894
  from app.services.linkedin_service import linkedin_service
895
  if lead.linkedin_urn:
 
119
  return info
120
 
121
 
122
+ async def send_product_images_if_mentioned(
123
+ session,
124
+ tenant_id: UUID,
125
+ bot_token: str,
126
+ chat_id: str,
127
+ ai_text: str
128
+ ):
129
+ """
130
+ Detect product names mentioned in AI response and send their images.
131
+ """
132
+ from app.models.product import Product
133
+ from app.services.telegram_service import telegram_service
134
+
135
+ # Get all products with images for this tenant
136
+ stmt = select(Product).where(
137
+ Product.tenant_id == tenant_id,
138
+ Product.image_url != None,
139
+ Product.is_active == True
140
+ )
141
+ result = await session.execute(stmt)
142
+ products = result.scalars().all()
143
+
144
+ if not products:
145
+ return
146
+
147
+ ai_text_lower = ai_text.lower()
148
+
149
+ for product in products:
150
+ # Check if product name is mentioned in AI response
151
+ product_name_lower = product.name.lower()
152
+ if product_name_lower in ai_text_lower and product.image_url:
153
+ # Send product image
154
+ await telegram_service.send_photo(
155
+ token=bot_token,
156
+ chat_id=chat_id,
157
+ photo_url=product.image_url,
158
+ caption=f"๐Ÿ“ฆ {product.name}"
159
+ )
160
+ logger.info(f"๐Ÿ“ท Auto-sent image for product: {product.name}")
161
+
162
+
163
  router = APIRouter(prefix="/webhook", tags=["Webhooks"])
164
 
165
 
 
931
  chat_id=chat_id,
932
  text=ai_response["text"]
933
  )
934
+
935
+ # Auto-send product images if mentioned in response
936
+ if send_success:
937
+ await send_product_images_if_mentioned(
938
+ session=session,
939
+ tenant_id=tenant.id,
940
+ bot_token=tenant.telegram_bot_token,
941
+ chat_id=chat_id,
942
+ ai_text=ai_response["text"]
943
+ )
944
  elif channel == 'linkedin':
945
  from app.services.linkedin_service import linkedin_service
946
  if lead.linkedin_urn:
backend/app/services/ai_memory_manager.py CHANGED
@@ -302,11 +302,8 @@ class AIMemoryManager:
302
  context += "\nโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n"
303
  context += "[๐Ÿ“ฆ ะšะะขะะ›ะžะ“ ะขะžะ’ะะ ะžะ’]\n"
304
  context += "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n"
305
- context += "๐Ÿ’ก ะšะพะณะดะฐ ั€ะฐััะบะฐะทั‹ะฒะฐะตัˆัŒ ะพ ั‚ะพะฒะฐั€ะต ั ะบะฐั€ั‚ะธะฝะบะพะน โ€” ะ’ะšะ›ะฎะงะ˜ URL ะฒ ะพั‚ะฒะตั‚, ัะธัั‚ะตะผะฐ ะพั‚ะฟั€ะฐะฒะธั‚ ั„ะพั‚ะพ ะฐะฒั‚ะพะผะฐั‚ะธั‡ะตัะบะธ!\n\n"
306
  for p in products:
307
  context += f"โ€ข {p.name} | ะฆะตะฝะฐ: {p.price} {p.currency}\n"
308
- if p.image_url:
309
- context += f" ๐Ÿ“ท ะคะพั‚ะพ: {p.image_url}\n"
310
  if p.description:
311
  context += f" ะžะฟะธัะฐะฝะธะต: {p.description}\n"
312
 
 
302
  context += "\nโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n"
303
  context += "[๐Ÿ“ฆ ะšะะขะะ›ะžะ“ ะขะžะ’ะะ ะžะ’]\n"
304
  context += "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n"
 
305
  for p in products:
306
  context += f"โ€ข {p.name} | ะฆะตะฝะฐ: {p.price} {p.currency}\n"
 
 
307
  if p.description:
308
  context += f" ะžะฟะธัะฐะฝะธะต: {p.description}\n"
309