handles_error.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Whatsapp::Webhook
  3. module Concerns::HandlesError
  4. private
  5. def error
  6. raise NotImplementedError
  7. end
  8. def handle_error
  9. # https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes
  10. #
  11. # Log any error status to the Rails log. Update the channel status on
  12. # any unrecoverable error - errors that need action from an administator
  13. # and block the channel from sending or receiving messages.
  14. Rails.logger.error "WhatsApp channel (#{@channel.options[:callback_url_uuid]}) - failed message: #{error[:title]} (#{error[:code]})"
  15. return if recoverable_error?(error[:code])
  16. return message_sender_error if sender_error?(error[:code])
  17. @channel.update!(
  18. status_out: 'error',
  19. last_log_out: "#{error[:title]} (#{error[:code]})",
  20. )
  21. end
  22. def recoverable_error?(code)
  23. [
  24. 130_472, # User's number is part of an experiment
  25. 131_021, # Recipient cannot be sender
  26. 131_026, # Message undeliverable
  27. 131_047, # Re-engagement message
  28. 131_052, # Media download error
  29. 131_053, # Media upload error
  30. ].include?(code)
  31. end
  32. def sender_error?(code)
  33. [
  34. 131_051, # Unsupported message type
  35. ].include?(code)
  36. end
  37. def message_sender_error
  38. body = Translation.translate(
  39. Setting.get('locale_default') || 'en-us',
  40. __("Apologies, we're unable to process this kind of message due to restrictions within WhatsApp Business.")
  41. )
  42. Whatsapp::Outgoing::Message::Text.new(
  43. access_token: @channel.options[:access_token],
  44. phone_number_id: @channel.options[:phone_number_id],
  45. recipient_number: @data[:entry].first[:changes].first[:value][:messages].first[:from]
  46. ).deliver(body:)
  47. end
  48. end
  49. end