From 905fd62974b967987db5c8066a0747db76991b7e Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Wed, 12 Jan 2022 19:42:23 -0800 Subject: [PATCH] chore: Support Whatsapp Interactive messages (360Dialog) (#3751) Render button and list reply clicks as text messages in agent dashboard fixes: #3684 --- .../whatsapp/incoming_message_service.rb | 11 ++++++++-- .../whatsapp/incoming_message_service_spec.rb | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/services/whatsapp/incoming_message_service.rb b/app/services/whatsapp/incoming_message_service.rb index 4169d4fb2..a10d40797 100644 --- a/app/services/whatsapp/incoming_message_service.rb +++ b/app/services/whatsapp/incoming_message_service.rb @@ -13,7 +13,7 @@ class Whatsapp::IncomingMessageService return if params[:messages].blank? @message = @conversation.messages.build( - content: params[:messages].first.dig(:text, :body), + content: message_content(params[:messages].first), account_id: @inbox.account_id, inbox_id: @inbox.id, message_type: :incoming, @@ -26,6 +26,13 @@ class Whatsapp::IncomingMessageService private + def message_content(message) + # TODO: map interactive messages back to button messages in chatwoot + message.dig(:text, :body) || + message.dig(:interactive, :button_reply, :title) || + message.dig(:interactive, :list_reply, :title) + end + def account @account ||= inbox.account end @@ -70,7 +77,7 @@ class Whatsapp::IncomingMessageService def attach_files message_type = params[:messages].first[:type] - return if message_type == 'text' + return if %w[text 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 cc000e5af..2c2b587b3 100644 --- a/spec/services/whatsapp/incoming_message_service_spec.rb +++ b/spec/services/whatsapp/incoming_message_service_spec.rb @@ -22,6 +22,27 @@ describe Whatsapp::IncomingMessageService do end end + context 'when valid interactive 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', + 'interactive': { + 'button_reply': { + 'id': '1', + 'title': 'First Button' + }, + 'type': 'button_reply' + }, + 'timestamp' => '1633034394', 'type' => 'interactive' }] + }.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('First 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(