diff --git a/app/services/whatsapp/send_on_whatsapp_service.rb b/app/services/whatsapp/send_on_whatsapp_service.rb index 4fbaa1312..2cc96f041 100644 --- a/app/services/whatsapp/send_on_whatsapp_service.rb +++ b/app/services/whatsapp/send_on_whatsapp_service.rb @@ -56,7 +56,16 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService def build_template_match_regex(template_text) # Converts the whatsapp template to a comparable regex string to check against the message content # the variables are of the format {{num}} ex:{{1}} - template_match_string = "^#{template_text.gsub(/{{\d}}/, '(.*)')}$" + + # transform the template text into a regex string + # we need to replace the {{num}} with matchers that can be used to capture the variables + template_text = template_text.gsub(/{{\d}}/, '(.*)') + # escape if there are regex characters in the template text + template_text = Regexp.escape(template_text) + # ensuring only the variables remain as capture groups + template_text = template_text.gsub(Regexp.escape('(.*)'), '(.*)') + + template_match_string = "^#{template_text}$" Regexp.new template_match_string end diff --git a/spec/factories/channel/channel_whatsapp.rb b/spec/factories/channel/channel_whatsapp.rb index 87ded15ab..0c60cc78f 100644 --- a/spec/factories/channel/channel_whatsapp.rb +++ b/spec/factories/channel/channel_whatsapp.rb @@ -13,6 +13,15 @@ FactoryBot.define do [{ 'text' => 'Paket Anda sudah dikirim. Paket akan sampai dalam {{1}} hari kerja.', 'type' => 'BODY' }, { 'text' => 'Pesan ini berasal dari bisnis yang tidak terverifikasi.', 'type' => 'FOOTER' }], 'rejected_reason' => 'NONE' }, + { 'name' => 'customer_yes_no', + 'status' => 'approved', + 'category' => 'SHIPPING_UPDATE', + 'language' => 'ar', + 'namespace' => '2342384942_32423423_23423fdsdaf23', + 'components' => + [{ 'text' => 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.', + 'type' => 'BODY' }], + 'rejected_reason' => 'NONE' }, { 'name' => 'sample_shipping_confirmation', 'status' => 'approved', 'category' => 'SHIPPING_UPDATE', diff --git a/spec/services/whatsapp/send_on_whatsapp_service_spec.rb b/spec/services/whatsapp/send_on_whatsapp_service_spec.rb index 0dc8f6809..29c583031 100644 --- a/spec/services/whatsapp/send_on_whatsapp_service_spec.rb +++ b/spec/services/whatsapp/send_on_whatsapp_service_spec.rb @@ -7,11 +7,12 @@ describe Whatsapp::SendOnWhatsappService do end context 'when a valid message' do + let(:whatsapp_request) { double } + let!(:whatsapp_channel) { create(:channel_whatsapp) } + 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) } + it 'calls channel.send_message when with in 24 hour limit' do - whatsapp_request = double - whatsapp_channel = create(:channel_whatsapp) - contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789') - conversation = create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox) # to handle the case of 24 hour window limit. create(:message, message_type: :incoming, content: 'test', conversation: conversation) @@ -30,10 +31,6 @@ describe Whatsapp::SendOnWhatsappService do end it 'calls channel.send_template when after 24 hour limit' do - whatsapp_request = double - whatsapp_channel = create(:channel_whatsapp) - contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: '123456789') - conversation = create(:conversation, contact_inbox: contact_inbox, inbox: whatsapp_channel.inbox) message = create(:message, message_type: :outgoing, content: 'Your package has been shipped. It will be delivered in 3 business days.', conversation: conversation) allow(HTTParty).to receive(:post).and_return(whatsapp_request) @@ -56,6 +53,34 @@ describe Whatsapp::SendOnWhatsappService do 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, + message_type: :outgoing, + content: 'عميلنا العزيز الرجاء الرد على هذه الرسالة بكلمة *نعم* للرد على إستفساركم من قبل خدمة العملاء.', + conversation: conversation + ) + allow(HTTParty).to receive(:post).and_return(whatsapp_request) + allow(whatsapp_request).to receive(:success?).and_return(true) + allow(whatsapp_request).to receive(:[]).with('messages').and_return([{ 'id' => '123456789' }]) + expect(HTTParty).to receive(:post).with( + 'https://waba.360dialog.io/v1/messages', + headers: { 'D360-API-KEY' => 'test_key', 'Content-Type' => 'application/json' }, + body: { + to: '123456789', + template: { + name: 'customer_yes_no', + namespace: '2342384942_32423423_23423fdsdaf23', + language: { 'policy': 'deterministic', 'code': 'ar' }, + components: [{ 'type': 'body', 'parameters': [] }] + }, + type: 'template' + }.to_json + ) + described_class.new(message: message).perform + expect(message.reload.source_id).to eq('123456789') + end end end end