feat: Support 360Dialog Interactive Templates (#3823)

fixes: #3795
This commit is contained in:
Sojan Jose 2022-01-20 17:09:30 -08:00 committed by GitHub
parent e997aaceb7
commit 693f2531ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View file

@ -29,6 +29,7 @@ class Whatsapp::IncomingMessageService
def message_content(message) def message_content(message)
# TODO: map interactive messages back to button messages in chatwoot # TODO: map interactive messages back to button messages in chatwoot
message.dig(:text, :body) || message.dig(:text, :body) ||
message.dig(:button, :text) ||
message.dig(:interactive, :button_reply, :title) || message.dig(:interactive, :button_reply, :title) ||
message.dig(:interactive, :list_reply, :title) message.dig(:interactive, :list_reply, :title)
end end
@ -77,7 +78,7 @@ class Whatsapp::IncomingMessageService
def attach_files def attach_files
message_type = params[:messages].first[:type] message_type = params[:messages].first[:type]
return if %w[text interactive].include?(message_type) return if %w[text button interactive].include?(message_type)
attachment_payload = params[:messages].first[message_type.to_sym] attachment_payload = params[:messages].first[message_type.to_sym]
attachment_file = Down.download(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers) attachment_file = Down.download(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers)

View file

@ -43,6 +43,24 @@ describe Whatsapp::IncomingMessageService do
end end
end end
# ref: https://github.com/chatwoot/chatwoot/issues/3795#issuecomment-1018057318
context 'when valid template button message params' do
it 'creates appropriate conversations, message and contacts' do
params = {
'contacts' => [{ 'profile' => { 'name' => 'Sojan Jose' }, 'wa_id' => '2423423243' }],
'messages' => [{ 'from' => '2423423243', 'id' => 'SDFADSf23sfasdafasdfa',
'button' => {
'text' => 'Yes this is a button'
},
'timestamp' => '1633034394', 'type' => 'button' }]
}.with_indifferent_access
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
expect(whatsapp_channel.inbox.conversations.count).not_to eq(0)
expect(Contact.all.first.name).to eq('Sojan Jose')
expect(whatsapp_channel.inbox.messages.first.content).to eq('Yes this is a button')
end
end
context 'when valid attachment message params' do context 'when valid attachment message params' do
it 'creates appropriate conversations, message and contacts' do it 'creates appropriate conversations, message and contacts' do
stub_request(:get, whatsapp_channel.media_url('b1c68f38-8734-4ad3-b4a1-ef0c10d683')).to_return( stub_request(:get, whatsapp_channel.media_url('b1c68f38-8734-4ad3-b4a1-ef0c10d683')).to_return(