Skip to content

fix(whatsapp): resolve LID addressing on Evolution inbound (use remoteJidAlt)#112

Open
firstplaceservdig1 wants to merge 2 commits into
evolution-foundation:mainfrom
firstplaceservdig1:fix/evolution-lid-inbound
Open

fix(whatsapp): resolve LID addressing on Evolution inbound (use remoteJidAlt)#112
firstplaceservdig1 wants to merge 2 commits into
evolution-foundation:mainfrom
firstplaceservdig1:fix/evolution-lid-inbound

Conversation

@firstplaceservdig1

@firstplaceservdig1 firstplaceservdig1 commented Jun 1, 2026

Copy link
Copy Markdown

Problem

WhatsApp's LID addressing mode (2024+ privacy feature) delivers 1:1 messages with remoteJid: "<id>@lid" instead of the phone JID. On the Evolution path:

  • jid_type (evolution_handlers/helpers.rb) returns 'lid' for @lid
  • message_processable? only accepts jid_type in %w[user group]

→ the message is silently dropped (no contact, no conversation, no log beyond the event). In one production deployment this was ~99.6% of inbound 1:1 traffic (1096 @lid vs 4 @s.whatsapp.net over 30 days). Related to #19 (which covers the outgoing side).

Fix

Evolution already ships the real phone JID in remoteJidAlt. Add effective_remote_jid that prefers remoteJidAlt whenever the primary JID is a @lid, and use it from jid_type and phone_number_from_jid. Falls back to the raw @lid when remoteJidAlt is absent (no behavior change in that case).

def effective_remote_jid
  jid = @raw_message[:remoteJid] || @raw_message.dig(:key, :remoteJid)
  return jid unless jid.to_s.end_with?('@lid')

  alt = @raw_message[:remoteJidAlt] || @raw_message.dig(:key, :remoteJidAlt)
  alt.presence || jid
end

Verified in production

After deploying, 1:1 messages in LID mode now create a contact with the real phone number (from remoteJidAlt) plus a conversation — instead of being dropped.

Tests

Adds spec/services/whatsapp/incoming_message_evolution_service_lid_spec.rb:

  • LID + remoteJidAltjid_type == 'user', phone extracted from remoteJidAlt, message_processable? true
  • LID without remoteJidAlt → graceful fallback to lid
  • normal @s.whatsapp.net → unchanged (regression guard)

Refs

#19 (LID addressing — outgoing). This addresses the inbound 1:1 side.

Summary by Sourcery

Handle WhatsApp Evolution inbound messages that use LID addressing by resolving the effective remote JID from remoteJidAlt so 1:1 messages are processed instead of dropped.

Bug Fixes:

  • Ensure inbound 1:1 WhatsApp messages addressed via LID are recognized as user messages and no longer silently discarded.

Tests:

  • Add specs covering LID inbound scenarios with and without remoteJidAlt and standard @s.whatsapp.net messages to guard the new behavior.

…eJidAlt)

WhatsApp's LID addressing mode (2024+ privacy) delivers 1:1 messages with
remoteJid "<id>@lid" instead of the phone JID. jid_type then returned 'lid',
message_processable? rejected it, and the message was silently dropped. In one
production deployment this was ~99.6% of inbound 1:1 traffic.

Evolution ships the real phone JID in remoteJidAlt. Add effective_remote_jid to
prefer remoteJidAlt whenever the primary JID is a @lid, and use it from jid_type
and phone_number_from_jid. Falls back to the raw lid when remoteJidAlt is absent.

Adds spec covering LID resolution, the no-remoteJidAlt fallback, and a normal
1:1 regression guard.

Refs evolution-foundation/evo-crm-community#49
@sourcery-ai

sourcery-ai Bot commented Jun 1, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Implements LID-aware JID resolution for WhatsApp Evolution inbound messages by introducing an effective_remote_jid helper, wiring it into JID classification and phone extraction, and adding targeted specs to ensure 1:1 LID messages are processed instead of dropped.

File-Level Changes

Change Details Files
Introduce LID-aware JID resolution helper and use it for phone extraction and JID classification so LID 1:1 messages are processed as normal user messages.
  • Add effective_remote_jid helper that prefers remoteJidAlt when remoteJid ends with '@lid', falling back to the original JID if no alternative is present.
  • Refactor phone_number_from_jid to use effective_remote_jid instead of reading remoteJid directly.
  • Refactor jid_type to use effective_remote_jid to classify LID 1:1 messages as 'user' rather than 'lid'.
app/services/whatsapp/evolution_handlers/helpers.rb
Add regression and behavior coverage for Evolution inbound handling of LID-addressed messages.
  • Add a spec file to cover LID inbound scenarios with and without remoteJidAlt, asserting correct jid_type, phone extraction, and message_processable? behavior.
  • Include tests to ensure non-LID (@s.whatsapp.net) behavior remains unchanged.
spec/services/whatsapp/incoming_message_evolution_service_lid_spec.rb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Now that effective_remote_jid encapsulates the LID resolution logic, consider using it anywhere else that reads remoteJid (e.g., other helpers that infer behavior from the JID) to avoid future divergence in how inbound messages are interpreted.
  • If there’s a chance that remoteJidAlt could also be a LID-style value in some future Evolution/WhatsApp change, you may want to defensively handle that (e.g., by only preferring remoteJidAlt when it looks like a standard user JID) to avoid misclassification.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `effective_remote_jid` encapsulates the LID resolution logic, consider using it anywhere else that reads `remoteJid` (e.g., other helpers that infer behavior from the JID) to avoid future divergence in how inbound messages are interpreted.
- If there’s a chance that `remoteJidAlt` could also be a LID-style value in some future Evolution/WhatsApp change, you may want to defensively handle that (e.g., by only preferring `remoteJidAlt` when it looks like a standard user JID) to avoid misclassification.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

- effective_remote_jid: only prefer remoteJidAlt when it is a real (non-LID)
  JID, guarding against a future payload where the alternate is also LID-style.
- group_jid: route through effective_remote_jid so the remote JID is interpreted
  in one place (no behavior change today; group JIDs are @g.us).
- spec: add a case where remoteJidAlt is itself a @lid (stays 'lid', dropped).

Addresses Sourcery review feedback on evolution-foundation#112.
@firstplaceservdig1

Copy link
Copy Markdown
Author

Thanks @sourcery-ai — both addressed in the latest commit:

  1. Single source of truth: group_jid now also routes through effective_remote_jid (no behavior change today since group JIDs are @g.us, but keeps JID interpretation in one place).
  2. Defensive remoteJidAlt: effective_remote_jid now only prefers remoteJidAlt when it is a real (non-@lid) JID — if a future payload has a LID-style alternate, we keep the original LID rather than misclassifying it as user. Added a spec for that case.

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.

1 participant