Skip to content

fix(utils): prevent stripping 9th digit on Brazilian mobile numbers with DDD >= 31 - #208

Open
alejandro-mero wants to merge 1 commit into
evolution-foundation:mainfrom
alejandro-mero:fix/preserve-br-ninth-digit
Open

alejandro-mero wants to merge 1 commit into
evolution-foundation:mainfrom
alejandro-mero:fix/preserve-br-ninth-digit

Conversation

@alejandro-mero

@alejandro-mero alejandro-mero commented Sep 30, 2026 •

Copy link
Copy Markdown

Summary

Prevents formatBRNumber from unconditionally stripping the 9th digit on Brazilian mobile numbers when DDD >= 31.

Problem

In pkg/utils/utils.go, formatBRNumber was stripping the 9th digit for any 13-digit Brazilian mobile number where DDD >= 31 (e.g. Paraná DDD 45, Santa Catarina DDD 48, Minas Gerais DDD 31, etc.):

if firstDigitNum >= 7 && dddNum >= 31 {
    return jid[:4] + jid[5:]
}

Historically, WhatsApp mapped some older Brazilian accounts outside DDD 11–28 with 8-digit JIDs. However, all Brazilian mobile lines nationwide have had 9 digits since Anatel completed the rollout in 2016. Modern WhatsApp accounts are registered natively with the full 9 digits.

When sending a message to a modern 13-digit number (e.g., 5545920031456), formatBRNumber strips the 9, converting it to +554520031456@s.whatsapp.net. Because that 8-digit variant does not exist on WhatsApp, the server returns an HTTP 500 error:
{"error":"number +554520031456@s.whatsapp.net is not registered on WhatsApp"}.

Solution

Preserve the 13-digit number provided by the caller so whatsmeow can address the user with their legitimate JID without mutating the digits.

  • Legacy numbers with 12 digits remain untouched.
  • Modern numbers with 13 digits are preserved as-is.
  • Unit tests in pkg/utils/utils_test.go updated and passing.

Summary by Sourcery

Preserve modern Brazilian mobile numbers without removing their 9th digit.

Bug Fixes:

  • Preserve the full 13-digit format of modern Brazilian mobile numbers so WhatsApp JIDs remain valid and messages can be delivered correctly.

Tests:

  • Update Brazilian number formatting and JID parsing tests to expect 13-digit mobile numbers to remain unchanged.

@sourcery-ai

sourcery-ai Bot commented Sep 30, 2026 •

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

Reviewer's Guide

The PR fixes Brazilian mobile JID generation by removing the legacy rule that stripped the 9th digit for DDDs >= 31. formatBRNumber now preserves input numbers unchanged, and related formatting, JID creation, and parsing tests verify the full modern 13-digit representation.

Flow diagram for Brazilian number preservation

flowchart TD
    A[Caller provides Brazilian number] --> B[formatBRNumber]
    B --> C[Return jid unchanged]
    C --> D[whatsmeow uses full 13-digit JID]
    D --> E[Message addresses registered WhatsApp number]
Loading

File-Level Changes

Change Details Files
Stop rewriting Brazilian mobile numbers and preserve the caller-provided 13-digit format.
  • Remove the DDD, prefix, and length-based logic that stripped the 9th digit.
  • Return Brazilian numbers unchanged, including legacy 12-digit and modern 13-digit forms.
  • Document that nationwide Brazilian mobile numbers now use 9 digits and that preserving them avoids invalid WhatsApp JIDs.
pkg/utils/utils.go
Update number-formatting and JID-generation expectations for preserved Brazilian mobile digits.
  • Change formatting tests to expect the 9th digit to remain.
  • Update CreateJID and ParseJID expectations to use the full 13-digit Brazilian number.
pkg/utils/utils_test.go

Possibly linked issues

  • #unknown: The PR directly fixes the reported behavior by preserving 13-digit Brazilian mobile numbers instead of stripping their ninth digit.

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 reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If preserving the ninth digit is wrong for some Brazilian numbers, messages may fail to reach the intended WhatsApp account or could be routed to a different account, and any messages already sent cannot be undone by reverting. The impact is bounded to affected Brazilian recipients and is normally repairable by correcting the formatting and retrying.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

oseiastsilva added a commit to oseiastsilva/evolution-go that referenced this pull request Oct 21, 2026
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