|
@@ -182,27 +182,38 @@ class WhatsAppAccount(models.Model):
|
|
|
|
|
|
|
|
# Lógica para detectar self-messages
|
|
# Lógica para detectar self-messages
|
|
|
is_self_message = False
|
|
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
|
|
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
|
|
# Intentar obtener el destinatario real
|
|
|
if "to" in messages:
|
|
if "to" in messages:
|
|
|
sender_mobile = messages["to"]
|
|
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:
|
|
try:
|
|
|
- # Extraer parte entre true_ y @
|
|
|
|
|
|
|
+ # Extraer parte entre true_ y @ o primer _
|
|
|
parts = messages["id"].split("_")
|
|
parts = messages["id"].split("_")
|
|
|
if len(parts) > 1:
|
|
if len(parts) > 1:
|
|
|
- jid = parts[1] # 5215581845273@c.us
|
|
|
|
|
|
|
+ jid = parts[1] # 5215581845273@c.us or 5215581845273
|
|
|
sender_mobile = jid
|
|
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(
|
|
_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 ---
|
|
# --- RECONCILIATION LOGIC ---
|
|
|
# Si viene un job_id en metadata, reconciliar el ID antes de chequear duplicados.
|
|
# Si viene un job_id en metadata, reconciliar el ID antes de chequear duplicados.
|