fix: Extend phone number normalization to Twilio WhatsApp (#12655)
### Problem WhatsApp Cloud channels already handle Brazil/Argentina phone number format mismatches (PRs #12492, #11173), but Twilio WhatsApp channels were creating duplicate contacts when: - Template sent to new format: `whatsapp:+5541988887777` (13 digits) - User responds from old format: `whatsapp:+554188887777` (12 digits) ### Solution The solution extends the existing phone number normalization infrastructure to support both WhatsApp providers while handling their different payload formats: ### Provider Format Differences - **WhatsApp Cloud**: `wa_id: "919745786257"` (clean number) - **Twilio WhatsApp**: `From: "whatsapp:+919745786257"` (prefixed format) ### Test Coverage #### Brazil Phone Number Tests **Case 1: New Format (13 digits with "9")** - **Test 1**: No existing contact → Creates new contact with original format - **Test 2**: Contact exists in same format → Appends to existing conversation **Case 2: Old Format (12 digits without "9")** - **Test 3**: Contact exists in old format → Appends to existing conversation - **Test 4** *(Critical)*: Contact exists in new format, message in old format → Finds existing contact, prevents duplicate - **Test 5**: No contact exists → Creates new contact with incoming format #### Argentina Phone Number Tests **Case 3: With "9" after country code** - **Test 6**: No existing contact → Creates new contact - **Test 7**: Contact exists in normalized format → Uses existing contact **Case 4: Without "9" after country code** - **Test 8**: Contact exists in same format → Appends to existing - **Test 9**: No contact exists → Creates new contact Fixes https://linear.app/chatwoot/issue/CW-5565/inconsistencies-for-mobile-numbersargentina-brazil-and-mexico-numbers
This commit is contained in:
@@ -44,6 +44,12 @@ class Twilio::IncomingMessageService
|
||||
twilio_channel.sms? ? params[:From] : params[:From].gsub('whatsapp:', '')
|
||||
end
|
||||
|
||||
def normalized_phone_number
|
||||
return phone_number unless twilio_channel.whatsapp?
|
||||
|
||||
Whatsapp::PhoneNumberNormalizationService.new(inbox).normalize_and_find_contact_by_provider("whatsapp:#{phone_number}", :twilio)
|
||||
end
|
||||
|
||||
def formatted_phone_number
|
||||
TelephoneNumber.parse(phone_number).international_number
|
||||
end
|
||||
@@ -53,8 +59,10 @@ class Twilio::IncomingMessageService
|
||||
end
|
||||
|
||||
def set_contact
|
||||
source_id = twilio_channel.whatsapp? ? normalized_phone_number : params[:From]
|
||||
|
||||
contact_inbox = ::ContactInboxWithContactBuilder.new(
|
||||
source_id: params[:From],
|
||||
source_id: source_id,
|
||||
inbox: inbox,
|
||||
contact_attributes: contact_attributes
|
||||
).perform
|
||||
|
||||
@@ -47,17 +47,8 @@ module Whatsapp::IncomingMessageServiceHelpers
|
||||
%w[reaction ephemeral unsupported request_welcome].include?(message_type)
|
||||
end
|
||||
|
||||
def argentina_phone_number?(phone_number)
|
||||
phone_number.match(/^54/)
|
||||
end
|
||||
|
||||
def normalised_argentina_mobil_number(phone_number)
|
||||
# Remove 9 before country code
|
||||
phone_number.sub(/^549/, '54')
|
||||
end
|
||||
|
||||
def processed_waid(waid)
|
||||
Whatsapp::PhoneNumberNormalizationService.new(inbox).normalize_and_find_contact(waid)
|
||||
Whatsapp::PhoneNumberNormalizationService.new(inbox).normalize_and_find_contact_by_provider(waid, :cloud)
|
||||
end
|
||||
|
||||
def error_webhook_event?(message)
|
||||
|
||||
@@ -1,23 +1,32 @@
|
||||
# Service to handle phone number normalization for WhatsApp messages
|
||||
# Currently supports Brazil and Argentina phone number format variations
|
||||
# Designed to be extensible for additional countries in future PRs
|
||||
#
|
||||
# Usage: Whatsapp::PhoneNumberNormalizationService.new(inbox).normalize_and_find_contact(waid)
|
||||
# Supports both WhatsApp Cloud API and Twilio WhatsApp providers
|
||||
class Whatsapp::PhoneNumberNormalizationService
|
||||
def initialize(inbox)
|
||||
@inbox = inbox
|
||||
end
|
||||
|
||||
# Main entry point for phone number normalization
|
||||
# Returns the source_id of an existing contact if found, otherwise returns original waid
|
||||
def normalize_and_find_contact(waid)
|
||||
normalizer = find_normalizer_for_country(waid)
|
||||
return waid unless normalizer
|
||||
# @param raw_number [String] The phone number in provider-specific format
|
||||
# - Cloud: "5541988887777" (clean number)
|
||||
# - Twilio: "whatsapp:+5541988887777" (prefixed format)
|
||||
# @param provider [Symbol] :cloud or :twilio
|
||||
# @return [String] Normalized source_id in provider format or original if not found
|
||||
def normalize_and_find_contact_by_provider(raw_number, provider)
|
||||
# Extract clean number based on provider format
|
||||
clean_number = extract_clean_number(raw_number, provider)
|
||||
|
||||
normalized_waid = normalizer.normalize(waid)
|
||||
existing_contact_inbox = find_existing_contact_inbox(normalized_waid)
|
||||
# Find appropriate normalizer for the country
|
||||
normalizer = find_normalizer_for_country(clean_number)
|
||||
return raw_number unless normalizer
|
||||
|
||||
existing_contact_inbox&.source_id || waid
|
||||
# Normalize the clean number
|
||||
normalized_clean_number = normalizer.normalize(clean_number)
|
||||
|
||||
# Format for provider and check for existing contact
|
||||
provider_format = format_for_provider(normalized_clean_number, provider)
|
||||
existing_contact_inbox = find_existing_contact_inbox(provider_format)
|
||||
|
||||
existing_contact_inbox&.source_id || raw_number
|
||||
end
|
||||
|
||||
private
|
||||
@@ -33,6 +42,26 @@ class Whatsapp::PhoneNumberNormalizationService
|
||||
inbox.contact_inboxes.find_by(source_id: normalized_waid)
|
||||
end
|
||||
|
||||
# Extract clean number from provider-specific format
|
||||
def extract_clean_number(raw_number, provider)
|
||||
case provider
|
||||
when :twilio
|
||||
raw_number.gsub(/^whatsapp:\+/, '') # Remove prefix: "whatsapp:+5541988887777" → "5541988887777"
|
||||
else
|
||||
raw_number # Default fallback for unknown providers
|
||||
end
|
||||
end
|
||||
|
||||
# Format normalized number for provider-specific storage
|
||||
def format_for_provider(clean_number, provider)
|
||||
case provider
|
||||
when :twilio
|
||||
"whatsapp:+#{clean_number}" # Add prefix: "5541988887777" → "whatsapp:+5541988887777"
|
||||
else
|
||||
clean_number # Default for :cloud and unknown providers: "5541988887777"
|
||||
end
|
||||
end
|
||||
|
||||
NORMALIZERS = [
|
||||
Whatsapp::PhoneNormalizers::BrazilPhoneNormalizer,
|
||||
Whatsapp::PhoneNormalizers::ArgentinaPhoneNormalizer
|
||||
|
||||
Reference in New Issue
Block a user