Compare commits
4 commits
develop
...
fix/6120-w
Author | SHA1 | Date | |
---|---|---|---|
|
10f72cdf31 | ||
|
bce8695d0e | ||
|
4988d9e99f | ||
|
a356c824d0 |
2 changed files with 19 additions and 10 deletions
|
@ -1,7 +1,10 @@
|
||||||
class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService
|
class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService
|
||||||
|
ATTACHMENT_TYPES = %w[image audio video].freeze
|
||||||
|
MESSAGE_CONTENT = %w[audio sticker].freeze
|
||||||
|
|
||||||
def send_message(phone_number, message)
|
def send_message(phone_number, message)
|
||||||
if message.attachments.present?
|
if message.attachments.present?
|
||||||
send_attachment_message(phone_number, message)
|
process_attachments(phone_number, message)
|
||||||
else
|
else
|
||||||
send_text_message(phone_number, message)
|
send_text_message(phone_number, message)
|
||||||
end
|
end
|
||||||
|
@ -66,14 +69,20 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
|
||||||
process_response(response)
|
process_response(response)
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_attachment_message(phone_number, message)
|
def process_attachments(phone_number, message)
|
||||||
attachment = message.attachments.first
|
responses = []
|
||||||
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document'
|
message.attachments.each do |attachment|
|
||||||
type_content = {
|
responses << send_attachment_message(phone_number, message, attachment)
|
||||||
'link': attachment.download_url
|
end
|
||||||
}
|
responses
|
||||||
type_content['caption'] = message.content unless %w[audio sticker].include?(type)
|
end
|
||||||
|
|
||||||
|
def send_attachment_message(phone_number, message, attachment)
|
||||||
|
type = self.class::ATTACHMENT_TYPES.include?(attachment.file_type) ? attachment.file_type : 'document'
|
||||||
|
type_content = { 'link': attachment.download_url }
|
||||||
|
type_content['caption'] = message.content unless self.class::MESSAGE_CONTENT.include?(type)
|
||||||
type_content['filename'] = attachment.file.filename if type == 'document'
|
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,
|
||||||
|
|
|
@ -42,7 +42,7 @@ 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 document attachment message messages' do
|
it 'calls message endpoints for document attachment message messages' do
|
||||||
|
@ -61,7 +61,7 @@ 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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue