Skip to content

Fix contact lookup for WhatsApp LID (Linked Device ID) migration#209

Open
coucaj wants to merge 1 commit intolharries:mainfrom
coucaj:fix/lid-contact-lookup
Open

Fix contact lookup for WhatsApp LID (Linked Device ID) migration#209
coucaj wants to merge 1 commit intolharries:mainfrom
coucaj:fix/lid-contact-lookup

Conversation

@coucaj
Copy link
Copy Markdown

@coucaj coucaj commented Apr 6, 2026

Problem

WhatsApp migrated contacts from phone-based JIDs (155...@s.whatsapp.net) to internal LID JIDs (123...@lid) for privacy. This broke several functions in whatsapp.py:

  • search_contacts("Name") returned nothing even if the contact existed
  • get_direct_chat_by_contact(phone) returned None
  • list_messages(sender_phone_number=phone) returned an empty list
  • Contact names showed as raw numeric IDs

The root cause: whatsapp.py only used messages.db for contact lookup, but messages.db stores chats by LID and does not have real names for those contacts. The real mapping lives in whatsapp.db (written by the Go bridge).

Solution

Add three helpers and patch four functions to use the LID -> phone mapping from whatsapp.db.

New helpers

  • _normalize_phone(phone) — strips +, spaces, dashes
  • _resolve_phone_to_jids(phone) — queries whatsmeow_lid_map to return all JID variants for a phone number; uses suffix match (last 10 digits) to handle number format differences (e.g. 15550001234 vs 115550001234)
  • _get_contact_name(phone) — looks up full_name / push_name in whatsmeow_contacts

Fixed functions

Function Change
search_contacts Query whatsapp.db first (real names + LID support), fall back to messages.db
get_direct_chat_by_contact Use _resolve_phone_to_jids and resolve name from store
list_messages Filter by all JID variants when sender_phone_number is provided
list_messages Skip include_context expansion when filtering by phone (prevents duplicate messages)

Testing

Tested with an account where multiple contacts had migrated to LID JIDs. After the patch:

search_contacts("John")                        # Returns contact with real name
get_direct_chat_by_contact("+1 555 000 1234")  # Returns chat by phone number
list_messages(sender_phone_number="1555...")   # Returns clean conversation, no duplicates

Notes

  • No breaking changes — all existing function signatures are preserved
  • whatsapp.db path is derived relative to the script location (same pattern as messages.db)
  • Suffix matching handles the common case where WhatsApp stores numbers with/without country code prefix digits

@coucaj coucaj force-pushed the fix/lid-contact-lookup branch 2 times, most recently from d037a14 to c87cd3c Compare April 6, 2026 00:40
WhatsApp migrated contacts from phone JIDs (155...@s.whatsapp.net)
to internal LID JIDs (123...@lid) for privacy. This broke contact
search and message lookup by phone number.

Changes:
- Add _normalize_phone: strip +, spaces, dashes
- Add _resolve_phone_to_jids: returns all JID variants for a phone
  number by querying whatsmeow_lid_map in whatsapp.db. Uses suffix
  match (last 10 digits) to handle number format differences.
- Add _get_contact_name: looks up name in whatsmeow_contacts table
- Fix search_contacts: query whatsapp.db first (real names + LID),
  fall back to messages.db
- Fix get_direct_chat_by_contact: use _resolve_phone_to_jids and
  resolve name from store DB
- Fix list_messages: filter by all JID variants when
  sender_phone_number is provided
- Fix list_messages: skip include_context expansion when filtering
  by phone to avoid duplicate messages
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.

2 participants