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