diff --git a/app/services/facebook/send_on_facebook_service.rb b/app/services/facebook/send_on_facebook_service.rb index be791b38a..514ba3e1f 100644 --- a/app/services/facebook/send_on_facebook_service.rb +++ b/app/services/facebook/send_on_facebook_service.rb @@ -7,7 +7,12 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService def perform_reply send_message_to_facebook fb_text_message_params if message.content.present? - send_message_to_facebook fb_attachment_message_params if message.attachments.present? + + if message.attachments.present? + message.attachments.each do |attachment| + send_message_to_facebook fb_attachment_message_params(attachment) + end + end rescue Facebook::Messenger::FacebookError => e # TODO : handle specific errors or else page will get disconnected handle_facebook_error(e) @@ -41,8 +46,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService "#{error_code} - #{error_message}" end - def fb_attachment_message_params - attachment = message.attachments.first + def fb_attachment_message_params(attachment) { recipient: { id: contact.get_source_id(inbox.id) }, message: { @@ -64,14 +68,6 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService 'file' end - def fb_message_params - if message.attachments.blank? - fb_text_message_params - else - fb_attachment_message_params - end - end - def sent_first_outgoing_message_after_24_hours? # we can send max 1 message after 24 hour window conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1 diff --git a/app/services/instagram/send_on_instagram_service.rb b/app/services/instagram/send_on_instagram_service.rb index df24c1c25..a681745d8 100644 --- a/app/services/instagram/send_on_instagram_service.rb +++ b/app/services/instagram/send_on_instagram_service.rb @@ -14,8 +14,13 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService end def perform_reply - send_to_facebook_page attachament_message_params if message.attachments.present? - send_to_facebook_page message_params + if message.attachments.present? + message.attachments.each do |attachment| + send_to_facebook_page attachment_message_params(attachment) + end + end + + send_to_facebook_page message_params if message.content.present? rescue StandardError => e ChatwootExceptionTracker.new(e, account: message.account, user: message.sender).capture_exception # TODO : handle specific errors or else page will get disconnected @@ -33,8 +38,7 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService merge_human_agent_tag(params) end - def attachament_message_params - attachment = message.attachments.first + def attachment_message_params(attachment) params = { recipient: { id: contact.get_source_id(inbox.id) }, message: { diff --git a/spec/services/facebook/send_on_facebook_service_spec.rb b/spec/services/facebook/send_on_facebook_service_spec.rb index 59a9bfea8..2fcea8b72 100644 --- a/spec/services/facebook/send_on_facebook_service_spec.rb +++ b/spec/services/facebook/send_on_facebook_service_spec.rb @@ -90,6 +90,24 @@ describe Facebook::SendOnFacebookService do }, { page_id: facebook_channel.page_id }) end + it 'if message is sent with multiple attachments' do + message = build(:message, content: nil, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) + avatar = message.attachments.new(account_id: message.account_id, file_type: :image) + avatar.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png') + sample = message.attachments.new(account_id: message.account_id, file_type: :image) + sample.file.attach(io: Rails.root.join('spec/assets/sample.png').open, filename: 'sample.png', content_type: 'image/png') + message.save! + + service = described_class.new(message: message) + + # Stub the send_to_facebook_page method on the service instance + allow(service).to receive(:send_message_to_facebook) + service.perform + + # Now you can set expectations on the stubbed method for each attachment + expect(service).to have_received(:send_message_to_facebook).exactly(:twice) + end + it 'if message sent from chatwoot is failed' do message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation) allow(bot).to receive(:deliver).and_return({ error: { message: 'Invalid OAuth access token.', type: 'OAuthException', code: 190, diff --git a/spec/services/instagram/send_on_instagram_service_spec.rb b/spec/services/instagram/send_on_instagram_service_spec.rb index f95d452eb..8900171f3 100644 --- a/spec/services/instagram/send_on_instagram_service_spec.rb +++ b/spec/services/instagram/send_on_instagram_service_spec.rb @@ -50,7 +50,25 @@ describe Instagram::SendOnInstagramService do response = described_class.new(message: message).perform - expect(response).to eq({ message_id: 'anyrandommessageid1234567890' }) + expect(response).to eq({ message_id: 'anyrandommessageid1234567890' }) + end + + it 'if message is sent from chatwoot and is outgoing with multiple attachments' do + message = build(:message, content: nil, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation) + avatar = message.attachments.new(account_id: message.account_id, file_type: :image) + avatar.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png') + sample = message.attachments.new(account_id: message.account_id, file_type: :image) + sample.file.attach(io: Rails.root.join('spec/assets/sample.png').open, filename: 'sample.png', content_type: 'image/png') + message.save! + + service = described_class.new(message: message) + + # Stub the send_to_facebook_page method on the service instance + allow(service).to receive(:send_to_facebook_page) + service.perform + + # Now you can set expectations on the stubbed method for each attachment + expect(service).to have_received(:send_to_facebook_page).exactly(:twice) end it 'if message with attachment is sent from chatwoot and is outgoing' do