#19590: fix(whatsapp): normalize Brazilian mobile numbers in JID conversion
size: XS
Cluster:
WhatsApp Enhancements and Fixes
## Summary
Brazilian mobile numbers carry an extra '9' after the 2-digit area code (DDD) that WhatsApp does not use in its JID format. This PR strips it during JID conversion.
## Problem
When sending outbound messages to Brazilian numbers entered with the full national format (e.g. `+55 69 9 9602-1005` or `+5569996021005`), the resulting JID `5569996021005@s.whatsapp.net` is invalid. WhatsApp expects 12-digit Brazilian JIDs (`556996021005@s.whatsapp.net`).
## Solution
Add `normalizeBrazilianMobile()` helper in `toWhatsappJid()`:
- Detects 13-digit numbers starting with `55` where the 5th digit is `9`
- Strips the extra `9` to produce the 12-digit WhatsApp format
- Landlines (no leading 9 after DDD) and non-Brazilian numbers pass through unchanged
## Tests
Comprehensive test cases covering:
- Brazilian mobile normalization (multiple DDDs)
- `whatsapp:` prefix handling
- Non-Brazilian numbers (US, Argentine) unchanged
- Brazilian landlines unchanged
- Already-correct 12-digit Brazilian numbers
- Raw digits without `+`
- Numbers with formatting characters
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds normalization for Brazilian mobile numbers in WhatsApp JID conversion. Brazilian mobile numbers include an extra '9' digit after the 2-digit area code that WhatsApp doesn't use in its JID format (13 digits → 12 digits).
**Key changes:**
- Added `normalizeBrazilianMobile()` helper function in `src/utils.ts:140-148` that detects 13-digit numbers starting with `55` and strips the extra '9' at position 4
- Integrated into `toWhatsappJid()` to normalize before constructing the JID
- Added comprehensive test coverage for Brazilian mobiles, landlines, non-Brazilian numbers, and edge cases
- Landlines and already-correct 12-digit numbers pass through unchanged
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The implementation is straightforward, well-tested, and focused. The logic correctly identifies Brazilian mobile numbers (13 digits starting with 55 where position 4 is '9') and strips the extra digit. All edge cases are covered: non-Brazilian numbers, landlines, already-correct numbers, and various input formats. The function is pure with no side effects, and the change is isolated to the WhatsApp JID conversion path.
- No files require special attention
<sub>Last reviewed commit: 8aa1845</sub>
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#20190: WhatsApp: normalize BR 9th-digit outbound targets
by saurabhchopade · 2026-02-18
90.2%
#4181: fix(whatsapp): resolve Brazilian mobile JID variants (8/9 digit)
by lucasmpramos · 2026-01-29
83.7%
#7395: fix(whatsapp): strip markdown bold/italic from URLs before sending
by lailoo · 2026-02-02
78.7%
#22335: fix: harden normalizeE164 against empty/invalid inputs returning ba...
by CZH-THU · 2026-02-21
77.7%
#15786: fix: strip device suffix from selfJid in WhatsApp mention matching
by kenken64 · 2026-02-13
76.1%
#8052: fix(whatsapp): strip leading whitespace from outbound messages
by FelixFoster · 2026-02-03
75.2%
#10196: fix(whatsapp): sanitize raw mention IDs in outbound messages
by koala73 · 2026-02-06
75.0%
#13424: feat(whatsapp): add Newsletter/Channel JID support
by agentz-manfred · 2026-02-10
74.1%
#5665: fix: match group JIDs in groupAllowFrom allowlist
by koala73 · 2026-01-31
72.6%
#19303: Fix WhatsApp internal error leakage + cron.run timeout defaults
by koala73 · 2026-02-17
71.5%