Skip to content

Commit ec180ba

Browse files
committed
feat(whatsapp): add legacy server retry logic for message sending failures
1 parent b5993ba commit ec180ba

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

internal/messaging/whatsapp_service.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ func (s *WhatsAppService) resolveRecipientAddress(canonicalPhone string) string
9494
return canonicalPhone
9595
}
9696

97+
func shouldRetryWithLegacyServer(err error) bool {
98+
if err == nil {
99+
return false
100+
}
101+
msg := err.Error()
102+
return strings.Contains(msg, "failed to get user info") || strings.Contains(msg, "rate-overlimit")
103+
}
104+
97105
// ValidateAndCanonicalizeRecipient validates and canonicalizes a WhatsApp phone number.
98106
// It removes all non-numeric characters and validates the result has at least 6 digits.
99107
func (s *WhatsAppService) ValidateAndCanonicalizeRecipient(recipient string) (string, error) {
@@ -186,6 +194,19 @@ func (s *WhatsAppService) SendMessage(ctx context.Context, to string, body strin
186194
}
187195
err = s.client.SendMessage(ctx, sendTo, body)
188196
if err != nil {
197+
if sendTo == canonicalTo && shouldRetryWithLegacyServer(err) {
198+
legacyTo := fmt.Sprintf("%s@%s", canonicalTo, types.LegacyUserServer)
199+
slog.Warn("WhatsAppService SendMessage retrying with legacy server", "to", legacyTo, "phone", canonicalTo)
200+
retryErr := s.client.SendMessage(ctx, legacyTo, body)
201+
if retryErr == nil {
202+
s.safeEmitReceipt(models.Receipt{To: canonicalTo, Status: models.MessageStatusSent, Time: time.Now().Unix()})
203+
slog.Info("WhatsAppService message sent via legacy server", "to", legacyTo, "phone", canonicalTo)
204+
return nil
205+
}
206+
slog.Error("WhatsAppService SendMessage legacy retry failed", "error", retryErr, "to", legacyTo, "phone", canonicalTo)
207+
return retryErr
208+
}
209+
189210
slog.Error("WhatsAppService SendMessage error", "error", err, "to", sendTo, "phone", canonicalTo)
190211
return err
191212
}

0 commit comments

Comments
 (0)