Skip to content

fix(baileys): resolve @lid senders to a phone number (#362)#372

Merged
rmyndharis merged 1 commit into
mainfrom
fix/baileys-lid-phone-resolution
Jun 20, 2026
Merged

fix(baileys): resolve @lid senders to a phone number (#362)#372
rmyndharis merged 1 commit into
mainfrom
fix/baileys-lid-phone-resolution

Conversation

@rmyndharis

Copy link
Copy Markdown
Owner

Closes #362.

Problem

On the Baileys engine, senderPhone and GET /sessions/:id/contacts/:contactId/phone always returned null for privacy-id (@lid) contacts — the #263 lid→phone feature was effectively non-functional there. Root cause (verified against baileys@6.7.23):

  • BaileysSessionStore.resolvePhone() only consulted in-memory mappings populated from contacts.upsert / messaging-history.set lidPnMappingsneither fires for a fresh inbound @lid sender.
  • The server-query path used by whatsapp-web.js (getContactLidAndPhone) has no equivalent in baileys@6.7.23 — there is no signalRepository.lidMapping.getPNForLID (confirmed absent in the installed lib), so a runtime lookup wasn't an option.

Fix

Baileys attaches the sender's phone JID right on the inbound message keysenderPn / participantPn alongside senderLid / participantLid (present in the 6.7.23 WAMessageKey type). The adapter now harvests that pair into the session store's lid → pn map at the start of processInboundMessage, before any id canonicalization. As a result:

  • the @lid sender of the incoming message resolves to <phone>@c.us, and
  • a later GET /contacts/:id/phone for that contact returns the number.

Still best-effort by design (matching the issue): a number is only revealed once WhatsApp delivers the mapping — i.e. after an inbound message from that contact. Truly hidden-number contacts remain null.

Tests (TDD, watched fail first)

  • baileys-session-store.spec.ts: 4 cases for recordKeyLidMappings (sender pair, participant pair, canonicalization after learning, ignores empty/half pairs).
  • baileys.adapter.spec.ts: 1 end-to-end inbound case — an @lid message whose key carries senderPn resolves from to <phone>@c.us with no prior history-sync mapping.

npx jest933/933 pass (86 suites); npm run build + eslint clean.

Notes

… key (#362)

senderPhone and GET /contacts/:id/phone always returned null for @lid
contacts on Baileys: the resolver only used mappings from contacts.* /
messaging-history.set (which don't fire for a fresh inbound @lid sender),
and baileys@6.7.23 has no getPNForLID lookup. Learn the lid->pn pair Baileys
attaches to the inbound message key (senderPn / participantPn) before
canonicalizing, so the sender resolves to its number and later contact
lookups succeed. Best-effort by design (only when WhatsApp delivers it).
@rmyndharis rmyndharis force-pushed the fix/baileys-lid-phone-resolution branch from f0b38f7 to 3bce16f Compare June 20, 2026 01:58
@rmyndharis rmyndharis merged commit 27fdd7c into main Jun 20, 2026
5 checks passed
@rmyndharis rmyndharis deleted the fix/baileys-lid-phone-resolution branch June 20, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Baileys engine: @lid -> phone never resolves (senderPhone / contacts/:id/phone always null); #263 feature non-functional on Baileys

1 participant