fix(send): resolve Brazilian 9th digit by asking WhatsApp for both variants
Accounts created before the 9th digit rollout are still registered without it (554291389691), newer ones with it (5542991389691). The DDD-based rewrite in CreateJID cannot be right for every account, and the existence check queried only the number as received, so sends to 5542991389691 failed for an account registered as 554291389691. validateAndCheckUserExists now queries both forms in one IsOnWhatsApp call and uses the registered one (received form first). Non-Brazilian numbers, groups, LIDs and landlines follow the previous path unchanged. Related: upstream issue #214 / PR #208. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
03d619bb55
commit
5b71495e93
4 files changed
+123
No files matched your search
@@ -187,6 +187,49 @@ func formatBRNumber(jid string) string {
|
||||
return jid
|
||||
}
|
||||
|
||||
// BRPhoneVariants returns both forms of a Brazilian mobile number — with and
|
||||
// without the 9th digit — with the received form first, or nil when the number
|
||||
// is not a Brazilian mobile.
|
||||
//
|
||||
// Accounts created before the 9th digit rollout are still registered without
|
||||
// it (e.g. 554291389691), newer ones with it (5542991389691). No fixed rule
|
||||
// derived from the DDD is right for every account, so callers should ask
|
||||
// WhatsApp which of the two exists.
|
||||
func BRPhoneVariants(number string) []string {
|
||||
if strings.Contains(number, "@") {
|
||||
if !strings.HasSuffix(number, "@"+whatsmeow_types.DefaultUserServer) {
|
||||
return nil
|
||||
}
|
||||
number = strings.Split(number, "@")[0]
|
||||
}
|
||||
number = strings.Split(number, ":")[0]
|
||||
|
||||
digits := strings.Map(func(r rune) rune {
|
||||
if r >= '0' && r <= '9' {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}, number)
|
||||
|
||||
// 55 + DDD (11-99, never starting with 0) + subscriber
|
||||
if !strings.HasPrefix(digits, "55") || len(digits) < 12 || digits[2] == '0' {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch len(digits) {
|
||||
case 13:
|
||||
if digits[4] == '9' {
|
||||
return []string{digits, digits[:4] + digits[5:]}
|
||||
}
|
||||
case 12:
|
||||
// 8-digit mobile numbers start with 6, 7, 8 or 9; landlines with 2-5
|
||||
if digits[4] >= '6' {
|
||||
return []string{digits, digits[:4] + "9" + digits[4:]}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseJID parses a number string into a WhatsApp JID with validation
|
||||
func ParseJID(arg string) (whatsmeow_types.JID, bool) {
|
||||
if arg == "" {
|
||||
|
||||
@@ -384,3 +384,34 @@ func TestCanonicalJID(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBRPhoneVariants(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want []string
|
||||
}{
|
||||
{"13 digits with 9", "5542991389691", []string{"5542991389691", "554291389691"}},
|
||||
{"12 digits without 9", "554291389691", []string{"554291389691", "5542991389691"}},
|
||||
{"plus and JID suffix", "[email protected]", []string{"5511987654321", "551187654321"}},
|
||||
{"formatted", "+55 (42) 99138-9691", []string{"5542991389691", "554291389691"}},
|
||||
{"landline", "554232221234", nil},
|
||||
{"not Brazil", "15551234567", nil},
|
||||
{"group", "[email protected]", nil},
|
||||
{"lid", "255284433936406@lid", nil},
|
||||
{"13 digits without 9 after DDD", "5542891389691", nil},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := BRPhoneVariants(tt.input)
|
||||
if len(got) != len(tt.want) {
|
||||
t.Fatalf("BRPhoneVariants(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != tt.want[i] {
|
||||
t.Fatalf("BRPhoneVariants(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user