Spaces:
Running

axeronis commited on
Commit
6d3b96e
Β·
1 Parent(s): 3250508

fix: send ALL product images and save to DB for chat display

Browse files
Files changed (1) hide show
  1. backend/app/api/webhook.py +49 -13
backend/app/api/webhook.py CHANGED
@@ -191,18 +191,20 @@ async def send_product_images_if_mentioned(
191
  tenant_id: UUID,
192
  bot_token: str,
193
  chat_id: str,
194
- ai_text: str
 
195
  ):
196
  """
197
- Detect product names mentioned in AI response and send their images.
 
198
  """
199
  from app.models.product import Product
 
200
  from app.services.telegram_service import telegram_service
201
 
202
  # Get all products with images for this tenant
203
  stmt = select(Product).where(
204
  Product.tenant_id == tenant_id,
205
- Product.image_url != None,
206
  Product.is_active == True
207
  )
208
  result = await session.execute(stmt)
@@ -216,15 +218,48 @@ async def send_product_images_if_mentioned(
216
  for product in products:
217
  # Check if product name is mentioned in AI response
218
  product_name_lower = product.name.lower()
219
- if product_name_lower in ai_text_lower and product.image_url:
220
- # Send product image
221
- await telegram_service.send_photo(
222
- token=bot_token,
223
- chat_id=chat_id,
224
- photo_url=product.image_url,
225
- caption=f"πŸ“¦ {product.name}"
226
- )
227
- logger.info(f"πŸ“· Auto-sent image for product: {product.name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
 
230
  async def download_telegram_file(bot_token: str, file_id: str) -> tuple[bytes, str]:
@@ -1537,7 +1572,8 @@ Answer with ONE word:
1537
  tenant_id=tenant.id,
1538
  bot_token=tenant.telegram_bot_token,
1539
  chat_id=chat_id,
1540
- ai_text=ai_response["text"]
 
1541
  )
1542
  elif channel == 'linkedin':
1543
  from app.services.linkedin_service import linkedin_service
 
191
  tenant_id: UUID,
192
  bot_token: str,
193
  chat_id: str,
194
+ ai_text: str,
195
+ conversation_id: UUID = None # NEW: to save messages in DB
196
  ):
197
  """
198
+ Detect product names mentioned in AI response and send ALL their images.
199
+ Also saves image messages to database so they appear in chat UI.
200
  """
201
  from app.models.product import Product
202
+ from app.models.message import Message
203
  from app.services.telegram_service import telegram_service
204
 
205
  # Get all products with images for this tenant
206
  stmt = select(Product).where(
207
  Product.tenant_id == tenant_id,
 
208
  Product.is_active == True
209
  )
210
  result = await session.execute(stmt)
 
218
  for product in products:
219
  # Check if product name is mentioned in AI response
220
  product_name_lower = product.name.lower()
221
+ if product_name_lower in ai_text_lower:
222
+ # Collect all image URLs
223
+ all_image_urls = []
224
+
225
+ # NEW: Add all images from image_urls array
226
+ if product.image_urls and len(product.image_urls) > 0:
227
+ all_image_urls.extend(product.image_urls)
228
+ # Fallback: add single image_url if present and not already in list
229
+ elif product.image_url and product.image_url not in all_image_urls:
230
+ all_image_urls.append(product.image_url)
231
+
232
+ # Send ALL images for this product
233
+ for idx, image_url in enumerate(all_image_urls):
234
+ caption = f"πŸ“¦ {product.name}" if idx == 0 else "" # Caption only on first image
235
+ try:
236
+ await telegram_service.send_photo(
237
+ token=bot_token,
238
+ chat_id=chat_id,
239
+ photo_url=image_url,
240
+ caption=caption
241
+ )
242
+ logger.info(f"πŸ“· Auto-sent image {idx+1}/{len(all_image_urls)} for product: {product.name}")
243
+
244
+ # NEW: Save image message to database so it appears in chat UI
245
+ if conversation_id:
246
+ image_message = Message(
247
+ conversation_id=conversation_id,
248
+ tenant_id=tenant_id,
249
+ sender_type="ai",
250
+ content=caption if caption else f"πŸ“· {product.name}",
251
+ media_url=image_url,
252
+ media_type="image",
253
+ media_name=f"{product.name}_{idx+1}.jpg"
254
+ )
255
+ session.add(image_message)
256
+
257
+ except Exception as e:
258
+ logger.error(f"❌ Failed to send image {idx+1} for {product.name}: {e}")
259
+
260
+ # Commit all image messages if any were added
261
+ if conversation_id:
262
+ await session.commit()
263
 
264
 
265
  async def download_telegram_file(bot_token: str, file_id: str) -> tuple[bytes, str]:
 
1572
  tenant_id=tenant.id,
1573
  bot_token=tenant.telegram_bot_token,
1574
  chat_id=chat_id,
1575
+ ai_text=ai_response["text"],
1576
+ conversation_id=conversation.id # Save images to DB for chat UI
1577
  )
1578
  elif channel == 'linkedin':
1579
  from app.services.linkedin_service import linkedin_service