This PR adds missing error handlers for the following channels and cases 1. WhatsApp - Generic Handlers for both Cloud and 360Dialog (Deprecated) 2. Instagram - Handler for a case where there is an HTTP error instead of an `:error` in the 200 response 3. Facebook - Errors from the two sentry issues ([Net::OpenTimeout](https://chatwoot-p3.sentry.io/issues/6164805227) & [JSON::ParserError](https://chatwoot-p3.sentry.io/issues/5903200786)) 4. SMS: Generic handlers for Bandwidth SMS #### Checklist - [x] Bandwidth SMS - [x] Whatsapp Cloud + 360 Dialog - [x] Twilio SMS - [x] Line - [x] Telegram - [x] Instagram - [x] Facebook - [x] GMail - [x] 365 Mail - [x] SMTP Mail --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
98 lines
3.3 KiB
Ruby
98 lines
3.3 KiB
Ruby
class Facebook::SendOnFacebookService < Base::SendOnChannelService
|
|
private
|
|
|
|
def channel_class
|
|
Channel::FacebookPage
|
|
end
|
|
|
|
def perform_reply
|
|
send_message_to_facebook fb_text_message_params if message.content.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)
|
|
message.update!(status: :failed, external_error: e.message)
|
|
end
|
|
|
|
def send_message_to_facebook(delivery_params)
|
|
parsed_result = deliver_message(delivery_params)
|
|
return if parsed_result.nil?
|
|
|
|
if parsed_result['error'].present?
|
|
message.update!(status: :failed, external_error: external_error(parsed_result))
|
|
Rails.logger.info "Facebook::SendOnFacebookService: Error sending message to Facebook : Page - #{channel.page_id} : #{parsed_result}"
|
|
end
|
|
|
|
message.update!(source_id: parsed_result['message_id']) if parsed_result['message_id'].present?
|
|
end
|
|
|
|
def deliver_message(delivery_params)
|
|
result = Facebook::Messenger::Bot.deliver(delivery_params, page_id: channel.page_id)
|
|
JSON.parse(result)
|
|
rescue JSON::ParserError
|
|
message.update!(status: :failed, external_error: 'Facebook was unable to process this request')
|
|
Rails.logger.error "Facebook::SendOnFacebookService: Error parsing JSON response from Facebook : Page - #{channel.page_id} : #{result}"
|
|
nil
|
|
rescue Net::OpenTimeout
|
|
message.update!(status: :failed, external_error: 'Request timed out, please try again later')
|
|
Rails.logger.error "Facebook::SendOnFacebookService: Timeout error sending message to Facebook : Page - #{channel.page_id}"
|
|
nil
|
|
end
|
|
|
|
def fb_text_message_params
|
|
{
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
message: { text: message.content },
|
|
messaging_type: 'MESSAGE_TAG',
|
|
tag: 'ACCOUNT_UPDATE'
|
|
}
|
|
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)
|
|
{
|
|
recipient: { id: contact.get_source_id(inbox.id) },
|
|
message: {
|
|
attachment: {
|
|
type: attachment_type(attachment),
|
|
payload: {
|
|
url: attachment.download_url
|
|
}
|
|
}
|
|
},
|
|
messaging_type: 'MESSAGE_TAG',
|
|
tag: 'ACCOUNT_UPDATE'
|
|
}
|
|
end
|
|
|
|
def attachment_type(attachment)
|
|
return attachment.file_type if %w[image audio video file].include? attachment.file_type
|
|
|
|
'file'
|
|
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
|
|
end
|
|
|
|
def handle_facebook_error(exception)
|
|
# Refer: https://github.com/jgorset/facebook-messenger/blob/64fe1f5cef4c1e3fca295b205037f64dfebdbcab/lib/facebook/messenger/error.rb
|
|
return unless exception.to_s.include?('The session has been invalidated') || exception.to_s.include?('Error validating access token')
|
|
|
|
channel.authorization_error!
|
|
end
|
|
end
|