chore: Support Whatsapp Interactive messages (360Dialog) (#3751)

Render button and list reply clicks as text messages in agent dashboard

fixes: #3684
This commit is contained in:
Sojan Jose 2022-01-12 19:42:23 -08:00 committed by GitHub
parent 1c99294c8c
commit 905fd62974
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -13,7 +13,7 @@ class Whatsapp::IncomingMessageService
return if params[:messages].blank? return if params[:messages].blank?
@message = @conversation.messages.build( @message = @conversation.messages.build(
content: params[:messages].first.dig(:text, :body), content: message_content(params[:messages].first),
account_id: @inbox.account_id, account_id: @inbox.account_id,
inbox_id: @inbox.id, inbox_id: @inbox.id,
message_type: :incoming, message_type: :incoming,
@ -26,6 +26,13 @@ class Whatsapp::IncomingMessageService
private 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 def account
@account ||= inbox.account @account ||= inbox.account
end end
@ -70,7 +77,7 @@ class Whatsapp::IncomingMessageService
def attach_files def attach_files
message_type = params[:messages].first[:type] 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_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

@ -22,6 +22,27 @@ describe Whatsapp::IncomingMessageService do
end end
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 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(