fix: whatsapp cloud to consider multiple attachments

This commit is contained in:
Tejaswini Chile 2022-12-22 18:43:49 +05:30
parent 34f7405689
commit a356c824d0

View file

@ -1,4 +1,7 @@
class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService
ATTACHMENT_TYPE = [image, audio, video].freeze
MESSAGE_CONTENT = [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) send_attachment_message(phone_number, message)
@ -67,25 +70,26 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
end end
def send_attachment_message(phone_number, message) def send_attachment_message(phone_number, message)
attachment = message.attachments.first message.attachments.each do |attachment|
type = %w[image audio video].include?(attachment.file_type) ? attachment.file_type : 'document' type = ATTACHMENT_TYPE.include?(attachment.file_type) ? attachment.file_type : 'document'
type_content = { type_content = {
'link': attachment.download_url 'link': attachment.download_url
} }
type_content['caption'] = message.content unless %w[audio sticker].include?(type) type_content['caption'] = message.content unless 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,
body: { body: {
messaging_product: 'whatsapp', messaging_product: 'whatsapp',
'to' => phone_number, 'to' => phone_number,
'type' => type, 'type' => type,
type.to_s => type_content type.to_s => type_content
}.to_json }.to_json
) )
process_response(response) process_response(response)
end
end end
def process_response(response) def process_response(response)