feat: allow multiple files for FB and Insta [CW-3019] (#8783)

* feat: allow multiple messages

* fix: typo

* feat: send content and attachments both

* refactor: message sequence

* test: multiple attachments for instagram

* test: multiple attachments on facebook
This commit is contained in:
Shivam Mishra
2024-01-28 13:20:54 +05:30
committed by GitHub
parent 12916ceca6
commit 082793290a
4 changed files with 52 additions and 16 deletions

View File

@@ -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

View File

@@ -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: {

View File

@@ -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,

View File

@@ -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