mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 20:18:08 +00:00
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:
@@ -7,7 +7,12 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
|||||||
|
|
||||||
def perform_reply
|
def perform_reply
|
||||||
send_message_to_facebook fb_text_message_params if message.content.present?
|
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
|
rescue Facebook::Messenger::FacebookError => e
|
||||||
# TODO : handle specific errors or else page will get disconnected
|
# TODO : handle specific errors or else page will get disconnected
|
||||||
handle_facebook_error(e)
|
handle_facebook_error(e)
|
||||||
@@ -41,8 +46,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
|||||||
"#{error_code} - #{error_message}"
|
"#{error_code} - #{error_message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def fb_attachment_message_params
|
def fb_attachment_message_params(attachment)
|
||||||
attachment = message.attachments.first
|
|
||||||
{
|
{
|
||||||
recipient: { id: contact.get_source_id(inbox.id) },
|
recipient: { id: contact.get_source_id(inbox.id) },
|
||||||
message: {
|
message: {
|
||||||
@@ -64,14 +68,6 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
|||||||
'file'
|
'file'
|
||||||
end
|
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?
|
def sent_first_outgoing_message_after_24_hours?
|
||||||
# we can send max 1 message after 24 hour window
|
# we can send max 1 message after 24 hour window
|
||||||
conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1
|
conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1
|
||||||
|
|||||||
@@ -14,8 +14,13 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def perform_reply
|
def perform_reply
|
||||||
send_to_facebook_page attachament_message_params if message.attachments.present?
|
if message.attachments.present?
|
||||||
send_to_facebook_page message_params
|
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
|
rescue StandardError => e
|
||||||
ChatwootExceptionTracker.new(e, account: message.account, user: message.sender).capture_exception
|
ChatwootExceptionTracker.new(e, account: message.account, user: message.sender).capture_exception
|
||||||
# TODO : handle specific errors or else page will get disconnected
|
# TODO : handle specific errors or else page will get disconnected
|
||||||
@@ -33,8 +38,7 @@ class Instagram::SendOnInstagramService < Base::SendOnChannelService
|
|||||||
merge_human_agent_tag(params)
|
merge_human_agent_tag(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachament_message_params
|
def attachment_message_params(attachment)
|
||||||
attachment = message.attachments.first
|
|
||||||
params = {
|
params = {
|
||||||
recipient: { id: contact.get_source_id(inbox.id) },
|
recipient: { id: contact.get_source_id(inbox.id) },
|
||||||
message: {
|
message: {
|
||||||
|
|||||||
@@ -90,6 +90,24 @@ describe Facebook::SendOnFacebookService do
|
|||||||
}, { page_id: facebook_channel.page_id })
|
}, { page_id: facebook_channel.page_id })
|
||||||
end
|
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
|
it 'if message sent from chatwoot is failed' do
|
||||||
message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation)
|
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,
|
allow(bot).to receive(:deliver).and_return({ error: { message: 'Invalid OAuth access token.', type: 'OAuthException', code: 190,
|
||||||
|
|||||||
@@ -53,6 +53,24 @@ describe Instagram::SendOnInstagramService do
|
|||||||
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
expect(response).to eq({ message_id: 'anyrandommessageid1234567890' })
|
||||||
end
|
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
|
it 'if message with attachment is sent from chatwoot and is outgoing' do
|
||||||
message = build(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
message = build(:message, message_type: 'outgoing', inbox: instagram_inbox, account: account, conversation: conversation)
|
||||||
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
||||||
|
|||||||
Reference in New Issue
Block a user