diff --git a/app/services/contacts/contactable_inboxes_service.rb b/app/services/contacts/contactable_inboxes_service.rb index c5cde516f..92d4160fe 100644 --- a/app/services/contacts/contactable_inboxes_service.rb +++ b/app/services/contacts/contactable_inboxes_service.rb @@ -42,20 +42,20 @@ class Contacts::ContactableInboxesService end def email_contactable_inbox(inbox) - return unless @contact.email + return if @contact.email.blank? { source_id: @contact.email, inbox: inbox } end def whatsapp_contactable_inbox(inbox) - return unless @contact.phone_number + return if @contact.phone_number.blank? # Remove the plus since thats the format 360 dialog uses { source_id: @contact.phone_number.delete('+'), inbox: inbox } end def sms_contactable_inbox(inbox) - return unless @contact.phone_number + return if @contact.phone_number.blank? { source_id: @contact.phone_number, inbox: inbox } end diff --git a/app/services/whatsapp/send_on_whatsapp_service.rb b/app/services/whatsapp/send_on_whatsapp_service.rb index 3d843e64b..186c8b2ae 100644 --- a/app/services/whatsapp/send_on_whatsapp_service.rb +++ b/app/services/whatsapp/send_on_whatsapp_service.rb @@ -28,14 +28,13 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService message.update!(source_id: message_id) if message_id.present? end - # rubocop:disable Metrics/CyclomaticComplexity def processable_channel_message_template if template_params.present? return [ template_params['name'], template_params['namespace'], template_params['language'], - template_params['processed_params']&.map { |_, value| { type: 'text', text: value } } + processed_templates_params(template_params) ] end @@ -56,7 +55,6 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService end [nil, nil, nil, nil] end - # rubocop:enable Metrics/CyclomaticComplexity def template_match_object(template) body_object = validated_body_object(template) @@ -82,6 +80,25 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService Regexp.new template_match_string end + def template(template_params) + channel.message_templates.find do |t| + t['name'] == template_params['name'] && t['language'] == template_params['language'] + end + end + + def processed_templates_params(template_params) + template = template(template_params) + return if template.blank? + + parameter_format = template['parameter_format'] + + if parameter_format == 'NAMED' + template_params['processed_params']&.map { |key, value| { type: 'text', parameter_name: key, text: value } } + else + template_params['processed_params']&.map { |_, value| { type: 'text', text: value } } + end + end + def validated_body_object(template) # we don't care if its not approved template return if template['status'] != 'approved' diff --git a/spec/factories/channel/channel_whatsapp.rb b/spec/factories/channel/channel_whatsapp.rb index ccbbf7172..d437ed346 100644 --- a/spec/factories/channel/channel_whatsapp.rb +++ b/spec/factories/channel/channel_whatsapp.rb @@ -30,7 +30,39 @@ FactoryBot.define do 'components' => [{ 'text' => 'Your package has been shipped. It will be delivered in {{1}} business days.', 'type' => 'BODY' }, { 'text' => 'This message is from an unverified business.', 'type' => 'FOOTER' }], - 'rejected_reason' => 'NONE' }] + 'rejected_reason' => 'NONE' }, + { + 'name' => 'ticket_status_updated', + 'status' => 'APPROVED', + 'category' => 'UTILITY', + 'language' => 'en', + 'components' => [ + { 'text' => "Hello {{name}}, Your support ticket with ID: \#{{ticket_id}} has been updated by the support agent.", + 'type' => 'BODY', + 'example' => { 'body_text_named_params' => [ + { 'example' => 'John', 'param_name' => 'name' }, + { 'example' => '2332', 'param_name' => 'ticket_id' } + ] } } + ], + 'sub_category' => 'CUSTOM', + 'parameter_format' => 'NAMED' + }, + { + 'name' => 'ticket_status_updated', + 'status' => 'APPROVED', + 'category' => 'UTILITY', + 'language' => 'en_US', + 'components' => [ + { 'text' => "Hello {{last_name}}, Your support ticket with ID: \#{{ticket_id}} has been updated by the support agent.", + 'type' => 'BODY', + 'example' => { 'body_text_named_params' => [ + { 'example' => 'Dale', 'param_name' => 'last_name' }, + { 'example' => '2332', 'param_name' => 'ticket_id' } + ] } } + ], + 'sub_category' => 'CUSTOM', + 'parameter_format' => 'NAMED' + }] end message_templates_last_updated { Time.now.utc } diff --git a/spec/services/whatsapp/send_on_whatsapp_service_spec.rb b/spec/services/whatsapp/send_on_whatsapp_service_spec.rb index 88f3f7740..a0e3e893a 100644 --- a/spec/services/whatsapp/send_on_whatsapp_service_spec.rb +++ b/spec/services/whatsapp/send_on_whatsapp_service_spec.rb @@ -18,6 +18,7 @@ describe Whatsapp::SendOnWhatsappService do context 'when a valid message' do let(:whatsapp_request) { instance_double(HTTParty::Response) } let!(:whatsapp_channel) { create(:channel_whatsapp, sync_templates: false) } + let!(:contact_inbox) { create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789') } let!(:conversation) { create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox) } let(:api_key) { 'test_key' } @@ -35,6 +36,21 @@ describe Whatsapp::SendOnWhatsappService do } end + let(:named_template_body) do + { + messaging_product: 'whatsapp', + to: '123456789', + template: { + name: 'ticket_status_updated', + language: { 'policy': 'deterministic', 'code': 'en_US' }, + components: [{ 'type': 'body', + 'parameters': [{ 'type': 'text', parameter_name: 'last_name', 'text': 'Dale' }, + { 'type': 'text', parameter_name: 'ticket_id', 'text': '2332' }] }] + }, + type: 'template' + } + end + let(:success_response) { { 'messages' => [{ 'id' => '123456789' }] }.to_json } it 'calls channel.send_message when with in 24 hour limit' do @@ -82,6 +98,31 @@ describe Whatsapp::SendOnWhatsappService do expect(message.reload.source_id).to eq('123456789') end + it 'calls channel.send_template with named params if template parameter type is NAMED' do + whatsapp_cloud_channel = create(:channel_whatsapp, provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false) + cloud_contact_inbox = create(:contact_inbox, inbox: whatsapp_cloud_channel.inbox, source_id: '123456789') + cloud_conversation = create(:conversation, contact_inbox: cloud_contact_inbox, inbox: whatsapp_cloud_channel.inbox) + + named_template_params = { + name: 'ticket_status_updated', + language: 'en_US', + category: 'UTILITY', + processed_params: { 'last_name' => 'Dale', 'ticket_id' => '2332' } + } + + stub_request(:post, "https://graph.facebook.com/v13.0/#{whatsapp_cloud_channel.provider_config['phone_number_id']}/messages") + .with( + :headers => { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{whatsapp_cloud_channel.provider_config['api_key']}" }, + :body => named_template_body.to_json + ).to_return(status: 200, body: success_response, headers: { 'content-type' => 'application/json' }) + message = create(:message, + additional_attributes: { template_params: named_template_params }, + content: 'Your package will be delivered in 3 business days.', conversation: cloud_conversation, message_type: :outgoing) + + described_class.new(message: message).perform + expect(message.reload.source_id).to eq('123456789') + end + it 'calls channel.send_template when template has regexp characters' do message = create( :message,