[core] Resolve Brazilian 9th-digit ambiguity on send (opt-in) - #2180
[core] Resolve Brazilian 9th-digit ambiguity on send (opt-in)#2180bergpinheiro wants to merge 1 commit into
Conversation
|
@devlikepro If you have time to review this PR before the next version, and if it makes sense for Waha, everyone here was asking me for this. |
|
I love you |
|
nice job! |
|
Pushed a follow-up commit fixing a regression this PR itself introduced. The PR routed
It affected GOWS, NOWEB and WEBJS (WPP has no I hit this in production before opening the PR and had fixed it only in GOWS on my side, which is why it slipped through here — sorry about that. |
Brazilian mobile numbers may or may not carry the 9th digit, and only one of the two forms is on WhatsApp. Sending to the wrong one fails silently. Behind WAHA_BR_PHONE_NORMALIZE (off by default), outbound chat ids for Brazilian mobiles are resolved to the form the account actually uses, querying the engine once and caching the answer per session. Reject is deliberately left alone: the caller's own JID is not an outbound destination, and normalizing it would retarget the reject at a different number, which WhatsApp drops silently while the caller keeps ringing.
6e15336 to
4a00aa8
Compare
Problem
Brazilian mobile numbers went through a 9th-digit migration. For DDD 11–30
every mobile carries the extra 9, so a static rule is enough. For DDD 31–99
the form is ambiguous: some accounts are registered with the 9, some
without, and the number alone does not tell which.
Integrations send whatever their CRM holds — usually the carrier form, with
the 9 — and the message fails or goes nowhere depending on how the account is
actually registered.
What this adds
resolveOutboundChatId()onWhatsappSession, applied to the send paths ofall four engines. It resolves the number to the chat id the account is
actually registered under, tiered cheapest-first:
number share one query
Read and presence operations resolve locally only (
{ validate: false }):they never reach the network and never throw.
A number confirmed not to exist is soft by default — warn and send the best
guess — so a usync false negative never blocks a valid send.
WAHA_BR_PHONE_STRICT=truerejects it with 422 instead.Toll-free (0800) numbers
Brazilian toll-free numbers are non-geographic: dialed as
0800+ 7subscriber digits, but stored on WhatsApp under country code 55 with the
leading
0dropped —08000464636is stored as558000464636. A callersending the dialed form (
08000464636or5508000464636) hits a send thatcannot resolve it to a LID and fails.
This rewrites the dialed form to the stored one with a deterministic rule —
no lookup, since unlike the 9th digit there is no ambiguity. The already-stored
form routes through untouched. Only
0800is handled (0300/0500/0900sharethe shape but are not covered).
Opt-in
Off unless
WAHA_BR_PHONE_NORMALIZE=true. Non-Brazilian numbers exit on thefirst check (
isBrazilCountryCode): no cache, no lookup, no cost.WAHA_BR_PHONE_NORMALIZEfalseWAHA_BR_PHONE_STRICTfalseRelationship to
PhoneJidNormalizerPhoneJidNormalizerconverts a JID into an E.164 string for the Chatwootcontact record — one direction, static rule. This resolves the opposite
direction: an inbound phone number into the chat id to address, with a server
lookup for the range where a static rule cannot decide.
They do not overlap, and its rule stays correct for DDD 11–30 — that is
exactly what tier 2 does here.
checkNumberStatusanswering with a LIDGOWS
checkNumberStatusreturned whateverjidthe usync answer carried.Since whatsmeow queries contacts with
addressing_mode=lid, that can be aLID — a contract break for an endpoint documented as returning the "Chat id
for the phone number", and it also breaks tier 4 of this feature.
Fixed by recovering the phone number from the LID map (
findPNByLid), with afallback to the LID when no mapping exists.
WANumberExistResultgains anoptional
lidso no identifier is lost. Happy to split this out if you wouldrather handle it separately.
Also
isBrazilMobilenow accepts any digit after the leading 9 on a 9-digit local.Brazil has no 9-digit landline, so
9+ 8 digits is unambiguously mobile;requiring 6–9 right after the 9 rejected real numbers.
Validation
Against a live GOWS session:
5585991203123(extra 9) and558591203123(correct form) resolve to thesame chat id, with a single usync shared between them through the cache
0800sent in either dialed form addresses the stored numberUnit tests in
brPhone.test.tsandsession.abc.brPhone.test.ts.Please excuse my explanation of our complex Brazilian numbering system;