Bot: resolver @lid al número real para casar el lead (mensajes entrantes)

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 <noreply@anthropic.com>
This commit is contained in:
Carlos Narro
2026-06-09 20:44:00 +02:00
parent 78079e9455
commit e5be1220d8

View File

@@ -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);