fix: Files in Whatsapp arrives with a different Name (#5907)
- Add file name parameter to the WhatsApp attachment payload fixes: #4481 Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
parent
edcbd53425
commit
0cad3bed71
3 changed files with 31 additions and 10 deletions
|
@ -69,17 +69,18 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
|
||||||
def send_attachment_message(phone_number, message)
|
def send_attachment_message(phone_number, message)
|
||||||
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'
|
||||||
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(
|
response = HTTParty.post(
|
||||||
"#{api_base_path}/messages",
|
"#{api_base_path}/messages",
|
||||||
headers: api_headers,
|
headers: api_headers,
|
||||||
body: {
|
body: {
|
||||||
'to' => phone_number,
|
'to' => phone_number,
|
||||||
'type' => type,
|
'type' => type,
|
||||||
type.to_s => {
|
type.to_s => type_content
|
||||||
'link': attachment_url,
|
|
||||||
'caption': message.content
|
|
||||||
}
|
|
||||||
}.to_json
|
}.to_json
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -69,11 +69,11 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
|
||||||
def send_attachment_message(phone_number, message)
|
def send_attachment_message(phone_number, message)
|
||||||
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'
|
||||||
attachment_url = attachment.download_url
|
|
||||||
type_content = {
|
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(
|
response = HTTParty.post(
|
||||||
"#{phone_id_path}/messages",
|
"#{phone_id_path}/messages",
|
||||||
headers: api_headers,
|
headers: api_headers,
|
||||||
|
|
|
@ -28,7 +28,7 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
||||||
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 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 = 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')
|
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({
|
body: hash_including({
|
||||||
messaging_product: 'whatsapp',
|
messaging_product: 'whatsapp',
|
||||||
to: '+123456789',
|
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)
|
.to_return(status: 200, body: whatsapp_response.to_json, headers: response_headers)
|
||||||
|
|
Loading…
Reference in a new issue