feat: Handle Facebook send message/attachments errors (#8197)

This commit is contained in:
Muhsin Keloth
2023-10-25 21:29:25 +05:30
committed by GitHub
parent 4fc62ed9ae
commit 58f47eb02c
2 changed files with 20 additions and 1 deletions

View File

@@ -15,7 +15,9 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
def send_message_to_facebook(delivery_params)
result = Facebook::Messenger::Bot.deliver(delivery_params, page_id: channel.page_id)
message.update!(source_id: JSON.parse(result)['message_id'])
parsed_result = JSON.parse(result)
message.update!(status: :failed, external_error: external_error(parsed_result)) if parsed_result['error'].present?
message.update!(source_id: parsed_result['message_id']) if parsed_result['message_id'].present?
end
def fb_text_message_params
@@ -27,6 +29,14 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService
}
end
def external_error(response)
# https://developers.facebook.com/docs/graph-api/guides/error-handling/
error_message = response['error']['message']
error_code = response['error']['code']
"#{error_code} - #{error_message}"
end
def fb_attachment_message_params
attachment = message.attachments.first
{

View File

@@ -87,6 +87,15 @@ describe Facebook::SendOnFacebookService do
tag: 'ACCOUNT_UPDATE'
}, { page_id: facebook_channel.page_id })
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,
fbtrace_id: 'BLBz/WZt8dN' } }.to_json)
described_class.new(message: message).perform
expect(bot).to have_received(:deliver)
expect(message.reload.status).to eq('failed')
end
end
end
end