chore: Fix 360Dialog template message breakage (#3750)

Template parsing fails when regexp characters are present in templates.

Fixes: #3587
This commit is contained in:
Sojan Jose
2022-01-12 17:41:42 -08:00
committed by GitHub
parent 94209d29cb
commit 1c99294c8c
3 changed files with 52 additions and 9 deletions

View File

@@ -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