From 4988d9e99fb29bafc452932534fb6e3d7fc2e132 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Thu, 22 Dec 2022 18:43:49 +0530 Subject: [PATCH] fix: whatsapp cloud to consider multiple attachments --- .../providers/whatsapp_cloud_service.rb | 47 ++++++++++--------- .../providers/whatsapp_cloud_service_spec.rb | 4 +- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/app/services/whatsapp/providers/whatsapp_cloud_service.rb b/app/services/whatsapp/providers/whatsapp_cloud_service.rb index 5ba16d690..b0f66c7c4 100644 --- a/app/services/whatsapp/providers/whatsapp_cloud_service.rb +++ b/app/services/whatsapp/providers/whatsapp_cloud_service.rb @@ -1,10 +1,10 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseService - ATTACHMENT_TYPE = [image, audio, video].freeze - MESSAGE_CONTENT = [audio, sticker].freeze + ATTACHMENT_TYPES = %w[image audio video].freeze + MESSAGE_CONTENT = %w[audio sticker].freeze def send_message(phone_number, message) if message.attachments.present? - send_attachment_message(phone_number, message) + process_attachments(phone_number, message) else send_text_message(phone_number, message) end @@ -69,27 +69,30 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi process_response(response) end - def send_attachment_message(phone_number, message) + def process_attachments(phone_number, message) + responses = [] message.attachments.each do |attachment| - type = ATTACHMENT_TYPE.include?(attachment.file_type) ? attachment.file_type : 'document' - type_content = { - 'link': attachment.download_url - } - type_content['caption'] = message.content unless MESSAGE_CONTENT.include?(type) - type_content['filename'] = attachment.file.filename if type == 'document' - response = HTTParty.post( - "#{phone_id_path}/messages", - headers: api_headers, - body: { - messaging_product: 'whatsapp', - 'to' => phone_number, - 'type' => type, - type.to_s => type_content - }.to_json - ) - - process_response(response) + responses << send_attachment_message(phone_number, message, attachment) end + responses + 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' + + response = HTTParty.post("#{phone_id_path}/messages", + headers: api_headers, + body: { + messaging_product: 'whatsapp', + 'to' => phone_number, + 'type' => type, + type.to_s => type_content + }.to_json) + + process_response(response) end def process_response(response) diff --git a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb index 4cc7bcdd5..5f83874c3 100644 --- a/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb +++ b/spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb @@ -42,7 +42,7 @@ 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' + expect(service.send_message('+123456789', message)).to eq ['message_id'] end 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) - expect(service.send_message('+123456789', message)).to eq 'message_id' + expect(service.send_message('+123456789', message)).to eq ['message_id'] end end end