diff --git a/app/services/twilio/incoming_message_service.rb b/app/services/twilio/incoming_message_service.rb index a11b99744..29bd05f8c 100644 --- a/app/services/twilio/incoming_message_service.rb +++ b/app/services/twilio/incoming_message_service.rb @@ -88,12 +88,16 @@ class Twilio::IncomingMessageService def contact_attributes { - name: formatted_phone_number, + name: contact_name, phone_number: phone_number, additional_attributes: additional_attributes } end + def contact_name + params[:ProfileName].presence || formatted_phone_number + end + def additional_attributes if twilio_channel.sms? { diff --git a/spec/services/twilio/incoming_message_service_spec.rb b/spec/services/twilio/incoming_message_service_spec.rb index 94fc3fdbf..63f721dde 100644 --- a/spec/services/twilio/incoming_message_service_spec.rb +++ b/spec/services/twilio/incoming_message_service_spec.rb @@ -282,5 +282,51 @@ describe Twilio::IncomingMessageService do expect(message.attachments.first.file_type).to eq('location') end end + + context 'when ProfileName is provided for WhatsApp' do + it 'uses ProfileName as contact name' do + params = { + SmsSid: 'SMxx', + From: '+1234567890', + AccountSid: 'ACxxx', + MessagingServiceSid: twilio_channel.messaging_service_sid, + Body: 'Hello with profile name', + ProfileName: 'John Doe' + } + + described_class.new(params: params).perform + contact = twilio_channel.inbox.contacts.find_by(phone_number: '+1234567890') + expect(contact.name).to eq('John Doe') + end + + it 'falls back to formatted phone number when ProfileName is blank' do + params = { + SmsSid: 'SMxx', + From: '+1234567890', + AccountSid: 'ACxxx', + MessagingServiceSid: twilio_channel.messaging_service_sid, + Body: 'Hello without profile name', + ProfileName: '' + } + + described_class.new(params: params).perform + contact = twilio_channel.inbox.contacts.find_by(phone_number: '+1234567890') + expect(contact.name).to eq('1234567890') + end + + it 'uses formatted phone number when ProfileName is not provided' do + params = { + SmsSid: 'SMxx', + From: '+1234567890', + AccountSid: 'ACxxx', + MessagingServiceSid: twilio_channel.messaging_service_sid, + Body: 'Regular SMS message' + } + + described_class.new(params: params).perform + contact = twilio_channel.inbox.contacts.find_by(phone_number: '+1234567890') + expect(contact.name).to eq('1234567890') + end + end end end