From 693f2531ab556873a9565b2426e2236f450c71f9 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 20 Jan 2022 17:09:30 -0800 Subject: [PATCH] feat: Support 360Dialog Interactive Templates (#3823) fixes: #3795 --- .../whatsapp/incoming_message_service.rb | 3 ++- .../whatsapp/incoming_message_service_spec.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/services/whatsapp/incoming_message_service.rb b/app/services/whatsapp/incoming_message_service.rb index a10d40797..78d7ae28d 100644 --- a/app/services/whatsapp/incoming_message_service.rb +++ b/app/services/whatsapp/incoming_message_service.rb @@ -29,6 +29,7 @@ class Whatsapp::IncomingMessageService def message_content(message) # TODO: map interactive messages back to button messages in chatwoot message.dig(:text, :body) || + message.dig(:button, :text) || message.dig(:interactive, :button_reply, :title) || message.dig(:interactive, :list_reply, :title) end @@ -77,7 +78,7 @@ class Whatsapp::IncomingMessageService def attach_files 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_file = Down.download(inbox.channel.media_url(attachment_payload[:id]), headers: inbox.channel.api_headers) diff --git a/spec/services/whatsapp/incoming_message_service_spec.rb b/spec/services/whatsapp/incoming_message_service_spec.rb index 2c2b587b3..0144dd169 100644 --- a/spec/services/whatsapp/incoming_message_service_spec.rb +++ b/spec/services/whatsapp/incoming_message_service_spec.rb @@ -43,6 +43,24 @@ describe Whatsapp::IncomingMessageService do 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 it 'creates appropriate conversations, message and contacts' do stub_request(:get, whatsapp_channel.media_url('b1c68f38-8734-4ad3-b4a1-ef0c10d683')).to_return(