From e5be1220d81df029c6159a053a0dc6901550fe93 Mon Sep 17 00:00:00 2001 From: Carlos Narro Date: Tue, 9 Jun 2026 20:44:00 +0200 Subject: [PATCH] =?UTF-8?q?Bot:=20resolver=20@lid=20al=20n=C3=BAmero=20rea?= =?UTF-8?q?l=20para=20casar=20el=20lead=20(mensajes=20entrantes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Confirmado vía /debug: WhatsApp entrega los mensajes desde una dirección @lid (p.ej. 239225534443615@lid), no desde el número. El bot tomaba el id del lid como teléfono -> no casaba con ningún lead -> ignoraba el mensaje. Ahora resolverTelefono() resuelve el @lid a número vía msg.key.remoteJidAlt o el mapa LID->PN de Baileys antes de buscar el lead. Debug captura también remoteJidAlt. Co-Authored-By: Claude Opus 4.8 --- .../src/whatsapp/whatsapp.service.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/mvp/Whatsapp-bot/src/whatsapp/whatsapp.service.ts b/mvp/Whatsapp-bot/src/whatsapp/whatsapp.service.ts index a0f093c..0930fd6 100644 --- a/mvp/Whatsapp-bot/src/whatsapp/whatsapp.service.ts +++ b/mvp/Whatsapp-bot/src/whatsapp/whatsapp.service.ts @@ -164,6 +164,23 @@ export class WhatsappService implements OnModuleInit, OnModuleDestroy { return jid.split('@')[0].replace(/\D/g, ''); } + // WhatsApp puede entregar mensajes desde una dirección @lid (id de privacidad) en vez del número. + // Resolvemos el número real vía remoteJidAlt o el mapa LID→PN de Baileys; si no, caemos al jid. + private resolverTelefono(msg: any): string { + const jid: string = msg.key?.remoteJid || ''; + if (jid.endsWith('@lid')) { + const alt = msg.key?.remoteJidAlt; + if (typeof alt === 'string' && alt.includes('@s.whatsapp.net')) return this.normalizarTelefono(alt); + try { + const pn = (this.sock as any)?.signalRepository?.lidMapping?.getPNForLID?.(jid); + if (typeof pn === 'string' && pn) return this.normalizarTelefono(pn); + } catch { + /* sin mapping disponible */ + } + } + return this.normalizarTelefono(jid); + } + private calcularDelayEscritura(longitudTexto: number): number { const min = 1500; const max = 4000; @@ -227,6 +244,7 @@ export class WhatsappService implements OnModuleInit, OnModuleDestroy { this.webhookListener.pushInbound({ type, remoteJid: msg.key.remoteJid ?? null, + remoteJidAlt: (msg.key as any).remoteJidAlt ?? null, fromMe: !!msg.key.fromMe, msgType: msg.message ? Object.keys(msg.message)[0] : null, at: new Date().toISOString(), @@ -318,7 +336,7 @@ export class WhatsappService implements OnModuleInit, OnModuleDestroy { const jid = msg.key.remoteJid!; if (jid.includes('@g.us')) return; - const telefono = jid.split('@')[0]; + const telefono = this.resolverTelefono(msg); try { const ctx = await this.getOrCreateContext(telefono, jid);