failed.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Whatsapp::Webhook::Message::Status::Failed < Whatsapp::Webhook::Message::Status
  3. private
  4. def create_article?
  5. true
  6. end
  7. def update_related_article?
  8. true
  9. end
  10. def update_related_article_attributes
  11. status_message = "#{error[:title]} (#{error[:code]})"
  12. if error.dig(:error_data, :details).present?
  13. status_message = error.dig(:error_data, :details)
  14. end
  15. preferences = @related_article.preferences
  16. preferences[:whatsapp][:delivery_status] = 'fail'
  17. preferences[:whatsapp][:delivery_status_date] = Time.zone.now
  18. preferences[:whatsapp][:delivery_status_message] = status_message
  19. { preferences: }
  20. end
  21. def body
  22. body = "#{error[:title]} (#{error[:code]})"
  23. if error.dig(:error_data, :details).present?
  24. body = "#{body}\n\n#{error.dig(:error_data, :details)}"
  25. end
  26. if error[:href].present?
  27. body = "#{body}\n\n#{error[:href]}"
  28. end
  29. handle_error
  30. body
  31. end
  32. def error
  33. @error = status[:errors].first
  34. end
  35. def handle_error
  36. # https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes
  37. #
  38. # Log any error status to the Rails log. Update the channel status on
  39. # any unrecoverable error - errors that need action from an administator
  40. # and block the channel from sending or receiving messages.
  41. Rails.logger.error "WhatsApp channel (#{@channel.options[:callback_url_uuid]}) - failed status message: #{error[:title]} (#{error[:code]})"
  42. recoverable_errors = [
  43. 130_472, # User's number is part of an experiment'
  44. 131_021, # Recipient cannot be sender'
  45. 131_026, # Message undeliverable'
  46. 131_047, # Re-engagement message
  47. 131_052, # Media download error'
  48. 131_053 # Media upload error'
  49. ]
  50. return if recoverable_errors.include?(error[:code])
  51. @channel.update!(
  52. status_out: 'error',
  53. last_log_out: "#{error[:title]} (#{error[:code]})",
  54. )
  55. end
  56. end