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
@@ -531,6 +531,16 @@ func (s *sendService) validateAndCheckUserExists(phone string, formatJid *bool,
|
||||
return types.NewJID("", types.DefaultUserServer), fmt.Errorf("failed to connect client: %v", err)
|
||||
}
|
||||
|
||||
// Brazilian mobiles: ask WhatsApp for both forms (with and without the 9th
|
||||
// digit) and use the one that is registered.
|
||||
if variants := utils.BRPhoneVariants(phone); len(variants) > 1 {
|
||||
if remoteJID, found := s.checkBRPhoneVariants(client, variants, instance.Id); found {
|
||||
s.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Number %s verified as valid WhatsApp user, using remoteJID: %s", instance.Id, phone, remoteJID)
|
||||
formatJidFalse := false
|
||||
return validateMessageFields(remoteJID, &formatJidFalse, messageID, participant)
|
||||
}
|
||||
}
|
||||
|
||||
// Use CheckUser approach: formatJid=false by default
|
||||
formatJidForCheck := false
|
||||
|
||||
@@ -592,6 +602,40 @@ func (s *sendService) checkSingleUserExists(client *whatsmeow.Client, phone stri
|
||||
return remoteJID, true, nil
|
||||
}
|
||||
|
||||
// checkBRPhoneVariants queries every variant in a single IsOnWhatsApp call and
|
||||
// returns the JID of the first one that is registered, in the given order.
|
||||
// Errors are logged and reported as not found so the regular check still runs.
|
||||
func (s *sendService) checkBRPhoneVariants(client *whatsmeow.Client, variants []string, instanceId string) (string, bool) {
|
||||
phones := make([]string, len(variants))
|
||||
for i, v := range variants {
|
||||
phones[i] = "+" + v
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
resp, err := client.IsOnWhatsApp(ctx, phones)
|
||||
if err != nil {
|
||||
s.loggerWrapper.GetLogger(instanceId).LogWarn("[%s] Failed to check BR number variants %v: %v", instanceId, variants, err)
|
||||
return "", false
|
||||
}
|
||||
|
||||
for _, v := range variants {
|
||||
for _, r := range resp {
|
||||
if r.IsIn && strings.TrimPrefix(r.Query, "+") == v {
|
||||
return r.JID.String(), true
|
||||
}
|
||||
}
|
||||
}
|
||||
// Query not echoed back as sent: any registered result is one of our variants.
|
||||
for _, r := range resp {
|
||||
if r.IsIn {
|
||||
return r.JID.String(), true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func findURL(text string) string {
|
||||
urlRegex := `http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+`
|
||||
re := regexp.MustCompile(urlRegex)
|
||||
|
||||
Reference in new issue
Block a user