feat: added input_select type message support for whatsapp (#6886)
- Added input_select message type support for Whatsapp Cloud API and Whatsapp 360dialog. Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
committed by
GitHub
parent
c8fac08e0b
commit
c715e396f0
@@ -1,10 +1,12 @@
|
||||
## the specs are covered in send in spec/services/whatsapp/send_on_whatsapp_service_spec.rb
|
||||
## the older specs are covered in send in spec/services/whatsapp/send_on_whatsapp_service_spec.rb
|
||||
require 'rails_helper'
|
||||
|
||||
describe Whatsapp::Providers::Whatsapp360DialogService do
|
||||
subject(:service) { described_class.new(whatsapp_channel: whatsapp_channel) }
|
||||
|
||||
let!(:whatsapp_channel) { create(:channel_whatsapp, sync_templates: false, validate_provider_config: false) }
|
||||
let(:response_headers) { { 'Content-Type' => 'application/json' } }
|
||||
let(:whatsapp_response) { { messages: [{ id: 'message_id' }] } }
|
||||
|
||||
describe '#sync_templates' do
|
||||
context 'when called' do
|
||||
@@ -18,4 +20,63 @@ describe Whatsapp::Providers::Whatsapp360DialogService do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#send_interactive message' do
|
||||
context 'when called' do
|
||||
it 'calls message endpoints with button payload when number of items is less than or equal to 3' do
|
||||
message = create(:message, message_type: :outgoing, content: 'test',
|
||||
inbox: whatsapp_channel.inbox, content_type: 'input_select',
|
||||
content_attributes: {
|
||||
items: [
|
||||
{ title: 'Burito', value: 'Burito' },
|
||||
{ title: 'Pasta', value: 'Pasta' },
|
||||
{ title: 'Sushi', value: 'Sushi' }
|
||||
]
|
||||
})
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
.with(
|
||||
body: {
|
||||
to: '+123456789',
|
||||
interactive: {
|
||||
type: 'button',
|
||||
body: {
|
||||
text: 'test'
|
||||
},
|
||||
action: '{"buttons":[{"type":"reply","reply":{"id":"Burito","title":"Burito"}},{"type":"reply",' \
|
||||
'"reply":{"id":"Pasta","title":"Pasta"}},{"type":"reply","reply":{"id":"Sushi","title":"Sushi"}}]}'
|
||||
}, type: 'interactive'
|
||||
}.to_json
|
||||
).to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
||||
expect(service.send_message('+123456789', message)).to eq 'message_id'
|
||||
end
|
||||
|
||||
it 'calls message endpoints with list payload when number of items is greater than 3' do
|
||||
message = create(:message, message_type: :outgoing, content: 'test', inbox: whatsapp_channel.inbox,
|
||||
content_type: 'input_select',
|
||||
content_attributes: {
|
||||
items: [
|
||||
{ title: 'Burito', value: 'Burito' },
|
||||
{ title: 'Pasta', value: 'Pasta' },
|
||||
{ title: 'Sushi', value: 'Sushi' },
|
||||
{ title: 'Salad', value: 'Salad' }
|
||||
]
|
||||
})
|
||||
stub_request(:post, 'https://waba.360dialog.io/v1/messages')
|
||||
.with(
|
||||
body: {
|
||||
to: '+123456789',
|
||||
interactive: {
|
||||
type: 'list',
|
||||
body: {
|
||||
text: 'test'
|
||||
},
|
||||
action: '{"button":"Choose an item","sections":[{"rows":[{"id":"Burito","title":"Burito"},' \
|
||||
'{"id":"Pasta","title":"Pasta"},{"id":"Sushi","title":"Sushi"},{"id":"Salad","title":"Salad"}]}]}'
|
||||
}, type: 'interactive'
|
||||
}.to_json
|
||||
).to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
||||
expect(service.send_message('+123456789', message)).to eq 'message_id'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -66,6 +66,65 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#send_interactive message' do
|
||||
context 'when called' do
|
||||
it 'calls message endpoints with button payload when number of items is less than or equal to 3' do
|
||||
message = create(:message, message_type: :outgoing, content: 'test',
|
||||
inbox: whatsapp_channel.inbox, content_type: 'input_select',
|
||||
content_attributes: {
|
||||
items: [
|
||||
{ title: 'Burito', value: 'Burito' },
|
||||
{ title: 'Pasta', value: 'Pasta' },
|
||||
{ title: 'Sushi', value: 'Sushi' }
|
||||
]
|
||||
})
|
||||
stub_request(:post, 'https://graph.facebook.com/v13.0/123456789/messages')
|
||||
.with(
|
||||
body: {
|
||||
messaging_product: 'whatsapp', to: '+123456789',
|
||||
interactive: {
|
||||
type: 'button',
|
||||
body: {
|
||||
text: 'test'
|
||||
},
|
||||
action: '{"buttons":[{"type":"reply","reply":{"id":"Burito","title":"Burito"}},{"type":"reply",' \
|
||||
'"reply":{"id":"Pasta","title":"Pasta"}},{"type":"reply","reply":{"id":"Sushi","title":"Sushi"}}]}'
|
||||
}, type: 'interactive'
|
||||
}.to_json
|
||||
).to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
||||
expect(service.send_message('+123456789', message)).to eq 'message_id'
|
||||
end
|
||||
|
||||
it 'calls message endpoints with list payload when number of items is greater than 3' do
|
||||
message = create(:message, message_type: :outgoing, content: 'test', inbox: whatsapp_channel.inbox,
|
||||
content_type: 'input_select',
|
||||
content_attributes: {
|
||||
items: [
|
||||
{ title: 'Burito', value: 'Burito' },
|
||||
{ title: 'Pasta', value: 'Pasta' },
|
||||
{ title: 'Sushi', value: 'Sushi' },
|
||||
{ title: 'Salad', value: 'Salad' }
|
||||
]
|
||||
})
|
||||
stub_request(:post, 'https://graph.facebook.com/v13.0/123456789/messages')
|
||||
.with(
|
||||
body: {
|
||||
messaging_product: 'whatsapp', to: '+123456789',
|
||||
interactive: {
|
||||
type: 'list',
|
||||
body: {
|
||||
text: 'test'
|
||||
},
|
||||
action: '{"button":"Choose an item","sections":[{"rows":[{"id":"Burito","title":"Burito"},' \
|
||||
'{"id":"Pasta","title":"Pasta"},{"id":"Sushi","title":"Sushi"},{"id":"Salad","title":"Salad"}]}]}'
|
||||
}, type: 'interactive'
|
||||
}.to_json
|
||||
).to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
||||
expect(service.send_message('+123456789', message)).to eq 'message_id'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#send_template' do
|
||||
let(:template_info) do
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user