Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
·
a30ed82
1
Parent(s):
2a5855c
handle document images
Browse files
app.py
CHANGED
@@ -371,41 +371,61 @@ class WhatsAppBot:
|
|
371 |
message_data = payload.get("messageData", {})
|
372 |
type_message = message_data.get("typeMessage")
|
373 |
|
374 |
-
# ---
|
375 |
-
|
376 |
-
# 1. Handle image caching
|
377 |
-
if type_message == "imageMessage":
|
378 |
-
download_url = message_data.get("fileMessageData", {}).get("downloadUrl")
|
379 |
-
if download_url:
|
380 |
-
self.logger.debug(f"Caching image message {message_id} from {chat_id} with URL: {download_url}")
|
381 |
-
self.image_cache[chat_id].append((message_id, download_url))
|
382 |
-
|
383 |
-
# 2. Extract text from various message types
|
384 |
text = ""
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
elif type_message == "quotedMessage":
|
390 |
-
text = message_data.get("extendedTextMessageData", {}).get("text", "")
|
391 |
-
|
392 |
-
|
|
|
|
|
|
|
|
|
393 |
|
394 |
-
|
395 |
if not text:
|
396 |
-
self.logger.debug(f"Received message type '{type_message}' without actionable text from {chat_id}.
|
397 |
return
|
398 |
-
|
399 |
-
# --- End of improved logic ---
|
400 |
-
|
401 |
-
self.logger.info(f"Processing message from {chat_id}: '{text}'")
|
402 |
-
self.conv_manager.add_user_message(chat_id, text)
|
403 |
|
404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
405 |
if text.startswith('/'):
|
406 |
self._handle_command(chat_id, message_id, text, payload)
|
|
|
407 |
else:
|
408 |
-
# Handle natural language and replies
|
409 |
self._handle_natural_language(chat_id, message_id, text, payload)
|
410 |
|
411 |
except KeyError as e:
|
@@ -468,13 +488,19 @@ class WhatsAppBot:
|
|
468 |
"""Checks for a replied-to image and dispatches the edit task."""
|
469 |
message_data = payload.get("messageData", {})
|
470 |
|
471 |
-
#
|
472 |
if message_data.get("typeMessage") != "quotedMessage":
|
473 |
self.api_client.send_message(chat_id, "To edit an image, please reply to an image with your instructions.", message_id)
|
474 |
return
|
475 |
|
476 |
quoted_info = message_data.get("quotedMessage")
|
477 |
-
if not quoted_info
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
self.api_client.send_message(chat_id, "You must reply to an image to edit it.", message_id)
|
479 |
return
|
480 |
|
|
|
371 |
message_data = payload.get("messageData", {})
|
372 |
type_message = message_data.get("typeMessage")
|
373 |
|
374 |
+
# --- Unified File & Text Extraction ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
text = ""
|
376 |
+
download_url = None
|
377 |
+
is_image_file = False
|
378 |
+
|
379 |
+
# Case 1 & 4: Image or Document with potential caption
|
380 |
+
if type_message in ["imageMessage", "documentMessage"]:
|
381 |
+
file_data = message_data.get("fileMessageData", {})
|
382 |
+
mime_type = file_data.get("mimeType", "")
|
383 |
+
|
384 |
+
if type_message == "imageMessage" or (type_message == "documentMessage" and mime_type.startswith("image/")):
|
385 |
+
is_image_file = True
|
386 |
+
download_url = file_data.get("downloadUrl")
|
387 |
+
text = file_data.get("caption", "").strip()
|
388 |
+
if download_url:
|
389 |
+
self.logger.debug(f"Caching file message {message_id} from {chat_id}")
|
390 |
+
self.image_cache[chat_id].append((message_id, download_url))
|
391 |
+
|
392 |
+
# Case 2 & 3: Reply to something (quoted message)
|
393 |
elif type_message == "quotedMessage":
|
394 |
+
text = message_data.get("extendedTextMessageData", {}).get("text", "").strip()
|
395 |
+
|
396 |
+
# Fallback for other text types
|
397 |
+
elif type_message == "textMessage":
|
398 |
+
text = message_data.get("textMessageData", {}).get("textMessage", "").strip()
|
399 |
+
elif type_message == "extendedTextMessage":
|
400 |
+
text = message_data.get("extendedTextMessageData", {}).get("text", "").strip()
|
401 |
|
402 |
+
# --- Action Dispatching ---
|
403 |
if not text:
|
404 |
+
self.logger.debug(f"Received message type '{type_message}' without actionable text from {chat_id}.")
|
405 |
return
|
|
|
|
|
|
|
|
|
|
|
406 |
|
407 |
+
self.logger.info(f"Processing text from {chat_id}: '{text}'")
|
408 |
+
self.conv_manager.add_user_message(chat_id, text)
|
409 |
+
|
410 |
+
# If it's a file with a caption, check if the caption is an edit command
|
411 |
+
if is_image_file and text:
|
412 |
+
intent = self.intent_router.get_intent(text, chat_id)
|
413 |
+
if isinstance(intent, EditImageIntent):
|
414 |
+
self.logger.info("Detected direct edit command in image caption.")
|
415 |
+
self.task_queue.put({
|
416 |
+
"type": "edit_image",
|
417 |
+
"chat_id": chat_id,
|
418 |
+
"message_id": message_id,
|
419 |
+
"prompt": intent.prompt,
|
420 |
+
"download_url": download_url
|
421 |
+
})
|
422 |
+
return # Task dispatched, we are done.
|
423 |
+
|
424 |
+
# Handle slash commands
|
425 |
if text.startswith('/'):
|
426 |
self._handle_command(chat_id, message_id, text, payload)
|
427 |
+
# Handle natural language replies or regular chat
|
428 |
else:
|
|
|
429 |
self._handle_natural_language(chat_id, message_id, text, payload)
|
430 |
|
431 |
except KeyError as e:
|
|
|
488 |
"""Checks for a replied-to image and dispatches the edit task."""
|
489 |
message_data = payload.get("messageData", {})
|
490 |
|
491 |
+
# This function is only for replies, so the top-level type must be quotedMessage
|
492 |
if message_data.get("typeMessage") != "quotedMessage":
|
493 |
self.api_client.send_message(chat_id, "To edit an image, please reply to an image with your instructions.", message_id)
|
494 |
return
|
495 |
|
496 |
quoted_info = message_data.get("quotedMessage")
|
497 |
+
if not quoted_info:
|
498 |
+
self.api_client.send_message(chat_id, "Couldn't find the message you replied to.", message_id)
|
499 |
+
return
|
500 |
+
|
501 |
+
# Check if the replied-to message is an image OR a document
|
502 |
+
quoted_type = quoted_info.get("typeMessage")
|
503 |
+
if quoted_type not in ["imageMessage", "documentMessage"]:
|
504 |
self.api_client.send_message(chat_id, "You must reply to an image to edit it.", message_id)
|
505 |
return
|
506 |
|