From 24d348cdd35fbcaf448cd6fb4869d849665fc64c Mon Sep 17 00:00:00 2001 From: Gabriel Jablonski Date: Thu, 15 Jan 2026 23:00:30 -0300 Subject: [PATCH] feat: whastapp cloud provider voice notes (#186) --- .../providers/whatsapp_cloud_service.rb | 5 ++- .../providers/whatsapp_cloud_service_spec.rb | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/services/whatsapp/providers/whatsapp_cloud_service.rb b/app/services/whatsapp/providers/whatsapp_cloud_service.rb index 97190025e..e65ff88d7 100644 --- a/app/services/whatsapp/providers/whatsapp_cloud_service.rb +++ b/app/services/whatsapp/providers/whatsapp_cloud_service.rb @@ -156,7 +156,7 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi process_response(response, message) end - def send_attachment_message(phone_number, message) + def send_attachment_message(phone_number, message) # rubocop:disable Metrics/MethodLength attachment = message.attachments.first type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document' type_content = { @@ -164,8 +164,9 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi } type_content['caption'] = message.outgoing_content unless %w[audio sticker].include?(type) type_content['filename'] = attachment.file.filename if type == 'document' + type_content['voice'] = true if type == 'audio' && attachment.meta&.dig('is_recorded_audio') response = HTTParty.post( - "#{phone_id_path}/messages", + "#{phone_id_path('v24.0')}/messages", headers: api_headers, body: { :messaging_product => 'whatsapp', diff --git a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb index f0206793b..52dc987e7 100644 --- a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb +++ b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb @@ -60,7 +60,7 @@ describe Whatsapp::Providers::WhatsappCloudService do attachment = message.attachments.new(account_id: message.account_id, file_type: :image) attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png') - stub_request(:post, 'https://graph.facebook.com/v13.0/123456789/messages') + stub_request(:post, 'https://graph.facebook.com/v24.0/123456789/messages') .with( body: hash_including({ messaging_product: 'whatsapp', @@ -79,7 +79,7 @@ describe Whatsapp::Providers::WhatsappCloudService do # 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') + stub_request(:post, 'https://graph.facebook.com/v24.0/123456789/messages') .with( body: hash_including({ messaging_product: 'whatsapp', @@ -91,6 +91,40 @@ describe Whatsapp::Providers::WhatsappCloudService do .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 audio attachment message' do + attachment = message.attachments.new(account_id: message.account_id, file_type: :audio) + attachment.file.attach(io: Rails.root.join('spec/assets/sample.mp3').open, filename: 'sample.mp3', content_type: 'audio/mpeg') + + stub_request(:post, 'https://graph.facebook.com/v24.0/123456789/messages') + .with( + body: hash_including({ + messaging_product: 'whatsapp', + to: '+123456789', + type: 'audio', + audio: WebMock::API.hash_including({ 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 with voice flag for recorded audio attachment' do + attachment = message.attachments.new(account_id: message.account_id, file_type: :audio, meta: { 'is_recorded_audio' => true }) + attachment.file.attach(io: Rails.root.join('spec/assets/sample.mp3').open, filename: 'sample.mp3', content_type: 'audio/mpeg') + + stub_request(:post, 'https://graph.facebook.com/v24.0/123456789/messages') + .with( + body: hash_including({ + messaging_product: 'whatsapp', + to: '+123456789', + type: 'audio', + audio: WebMock::API.hash_including({ link: anything, voice: true }) + }) + ) + .to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers) + expect(service.send_message('+123456789', message)).to eq 'message_id' + end end end