Skip to content

Commit

Permalink
Fix: skip invalid access token from sentry (chatwoot#6639)
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaswinichile authored Mar 9, 2023
1 parent 54b7c98 commit fbdc79d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/services/facebook/send_on_facebook_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ 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?
rescue Facebook::Messenger::FacebookError => e
ChatwootExceptionTracker.new(e, account: message.account, user: message.sender).capture_exception
# TODO : handle specific errors or else page will get disconnected
# channel.authorization_error!
handle_facebook_error(e)
end

def send_message_to_facebook(delivery_params)
Expand Down Expand Up @@ -67,4 +66,13 @@ def sent_first_outgoing_message_after_24_hours?
def last_incoming_message
conversation.messages.incoming.last
end

def handle_facebook_error(exception)
# Refer: https://github.com/jgorset/facebook-messenger/blob/64fe1f5cef4c1e3fca295b205037f64dfebdbcab/lib/facebook/messenger/error.rb
if exception.to_s.include?('The session has been invalidated') || exception.to_s.include?('Error validating access token')
channel.authorization_error!
else
ChatwootExceptionTracker.new(exception, account: message.account, user: message.sender).capture_exception
end
end
end
8 changes: 8 additions & 0 deletions spec/services/facebook/send_on_facebook_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
expect(bot).to have_received(:deliver)
end

it 'raise and exception to validate access token' do
message = create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation)
allow(bot).to receive(:deliver).and_raise(Facebook::Messenger::FacebookError.new('message' => 'Error validating access token'))
::Facebook::SendOnFacebookService.new(message: message).perform

expect(facebook_channel.authorization_error_count).to eq(1)
end

it 'if message with attachment is sent from chatwoot and is outgoing' do
message = build(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, conversation: conversation)
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
Expand Down

0 comments on commit fbdc79d

Please sign in to comment.