Просмотр исходного кода

Fix: Improve self-message recipient resolution by relaxing ID parsing constraints

Antigravity Bot 2 недель назад
Родитель
Сommit
4765b32f76
1 измененных файлов с 23 добавлено и 12 удалено
  1. 23 12
      models/whatsapp_account.py

+ 23 - 12
models/whatsapp_account.py

@@ -182,27 +182,38 @@ class WhatsAppAccount(models.Model):
 
             # Lógica para detectar self-messages
             is_self_message = False
-            if my_phone_id and sender_mobile == my_phone_id:
+            # Check explicit flag first (if provided by bridge)
+            if messages.get("fromMe") or messages.get("from_me"):
                 is_self_message = True
+
+            # Check phone ID match as fallback
+            if not is_self_message and my_phone_id and sender_mobile == my_phone_id:
+                is_self_message = True
+
+            if is_self_message:
                 # Intentar obtener el destinatario real
                 if "to" in messages:
                     sender_mobile = messages["to"]
-                elif (
-                    "id" in messages
-                    and "true_" in messages["id"]
-                    and "@c.us" in messages["id"]
-                ):
-                    # Fallback: intentar parsear del ID (formato true_NUMBER@c.us_ID)
+                elif "id" in messages and "true_" in messages["id"]:
+                    # Fallback: intentar parsear del ID (formato true_NUMBER@c.us_ID o true_NUMBER_ID)
+                    # Relaxed check: Removed mandatory @c.us to support raw numbers
                     try:
-                        # Extraer parte entre true_ y @
+                        # Extraer parte entre true_ y @ o primer _
                         parts = messages["id"].split("_")
                         if len(parts) > 1:
-                            jid = parts[1]  # 5215581845273@c.us
+                            jid = parts[1]  # 5215581845273@c.us or 5215581845273
                             sender_mobile = jid
-                    except:
-                        pass
+                            _logger.info(
+                                "Recuperado destinatario real desde ID: %s",
+                                sender_mobile,
+                            )
+                    except Exception as e:
+                        _logger.warning("Error parseando ID de self-message: %s", e)
+
                 _logger.info(
-                    "Detectado self-message. Redirigiendo a chat de: %s", sender_mobile
+                    "Detectado self-message. Redirigiendo a chat de: %s (Original From: %s)",
+                    sender_mobile,
+                    messages["from"],
                 )
             # --- RECONCILIATION LOGIC ---
             # Si viene un job_id en metadata, reconciliar el ID antes de chequear duplicados.