diff --git a/app/services/whatsapp/providers/whatsapp_360_dialog_service.rb b/app/services/whatsapp/providers/whatsapp_360_dialog_service.rb index a9ffb35a0..c67e448bb 100644 --- a/app/services/whatsapp/providers/whatsapp_360_dialog_service.rb +++ b/app/services/whatsapp/providers/whatsapp_360_dialog_service.rb @@ -69,17 +69,18 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS def send_attachment_message(phone_number, message) attachment = message.attachments.first type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document' - attachment_url = attachment.download_url + type_content = { + 'link': attachment.download_url + } + type_content['caption'] = message.content unless %w[audio sticker].include?(type) + type_content['filename'] = attachment.file.filename if type == 'document' response = HTTParty.post( "#{api_base_path}/messages", headers: api_headers, body: { 'to' => phone_number, 'type' => type, - type.to_s => { - 'link': attachment_url, - 'caption': message.content - } + type.to_s => type_content }.to_json ) diff --git a/app/services/whatsapp/providers/whatsapp_cloud_service.rb b/app/services/whatsapp/providers/whatsapp_cloud_service.rb index 0858b28f2..a0c591783 100644 --- a/app/services/whatsapp/providers/whatsapp_cloud_service.rb +++ b/app/services/whatsapp/providers/whatsapp_cloud_service.rb @@ -69,11 +69,11 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi def send_attachment_message(phone_number, message) attachment = message.attachments.first type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document' - attachment_url = attachment.download_url type_content = { - 'link': attachment_url + 'link': attachment.download_url } - type_content['caption'] = message.content if type != 'audio' + type_content['caption'] = message.content unless %w[audio sticker].include?(type) + type_content['filename'] = attachment.file.filename if type == 'document' response = HTTParty.post( "#{phone_id_path}/messages", headers: api_headers, diff --git a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb index 678d03b84..4cc7bcdd5 100644 --- a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb +++ b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb @@ -28,7 +28,7 @@ describe Whatsapp::Providers::WhatsappCloudService do expect(service.send_message('+123456789', message)).to eq 'message_id' end - it 'calls message endpoints for attachment message messages' do + it 'calls message endpoints for image attachment message messages' do attachment = message.attachments.new(account_id: message.account_id, file_type: :image) attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png') @@ -37,7 +37,27 @@ describe Whatsapp::Providers::WhatsappCloudService do body: hash_including({ messaging_product: 'whatsapp', to: '+123456789', - type: 'image' + type: 'image', + image: WebMock::API.hash_including({ caption: message.content, link: anything }) + }) + ) + .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 for document attachment message messages' do + attachment = message.attachments.new(account_id: message.account_id, file_type: :file) + attachment.file.attach(io: File.open(Rails.root.join('spec/assets/sample.pdf')), filename: 'sample.pdf', content_type: 'application/pdf') + + # ref: https://github.com/bblimke/webmock/issues/900 + # reason for Webmock::API.hash_including + stub_request(:post, 'https://graph.facebook.com/v13.0/123456789/messages') + .with( + body: hash_including({ + messaging_product: 'whatsapp', + to: '+123456789', + type: 'document', + document: WebMock::API.hash_including({ filename: 'sample.pdf', caption: message.content, link: anything }) }) ) .to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)