feat: whastapp cloud provider voice notes (#186)
This commit is contained in:
parent
5a2658fd19
commit
24d348cdd3
@ -156,7 +156,7 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
|
|||||||
process_response(response, message)
|
process_response(response, message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_attachment_message(phone_number, message)
|
def send_attachment_message(phone_number, message) # rubocop:disable Metrics/MethodLength
|
||||||
attachment = message.attachments.first
|
attachment = message.attachments.first
|
||||||
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
||||||
type_content = {
|
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['caption'] = message.outgoing_content unless %w[audio sticker].include?(type)
|
||||||
type_content['filename'] = attachment.file.filename if type == 'document'
|
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(
|
response = HTTParty.post(
|
||||||
"#{phone_id_path}/messages",
|
"#{phone_id_path('v24.0')}/messages",
|
||||||
headers: api_headers,
|
headers: api_headers,
|
||||||
body: {
|
body: {
|
||||||
:messaging_product => 'whatsapp',
|
:messaging_product => 'whatsapp',
|
||||||
|
|||||||
@ -60,7 +60,7 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
|||||||
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
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')
|
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(
|
.with(
|
||||||
body: hash_including({
|
body: hash_including({
|
||||||
messaging_product: 'whatsapp',
|
messaging_product: 'whatsapp',
|
||||||
@ -79,7 +79,7 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
|||||||
|
|
||||||
# ref: https://github.com/bblimke/webmock/issues/900
|
# ref: https://github.com/bblimke/webmock/issues/900
|
||||||
# reason for Webmock::API.hash_including
|
# 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(
|
.with(
|
||||||
body: hash_including({
|
body: hash_including({
|
||||||
messaging_product: 'whatsapp',
|
messaging_product: 'whatsapp',
|
||||||
@ -91,6 +91,40 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
|||||||
.to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
.to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
||||||
expect(service.send_message('+123456789', message)).to eq 'message_id'
|
expect(service.send_message('+123456789', message)).to eq 'message_id'
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user