communicate_sms_job.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class CommunicateSmsJob < ApplicationJob
  3. retry_on StandardError, attempts: 4, wait: lambda { |executions|
  4. executions * 120.seconds
  5. }
  6. def perform(article_id)
  7. article = Ticket::Article.find(article_id)
  8. # set retry count
  9. article.preferences['delivery_retry'] ||= 0
  10. article.preferences['delivery_retry'] += 1
  11. ticket = Ticket.lookup(id: article.ticket_id)
  12. log_error(article, "Can't find article.preferences for Ticket::Article.find(#{article.id})") if !article.preferences
  13. # if sender is system, take article channel
  14. if article.sender.name == 'System'
  15. log_error(article, "Can't find article.preferences['sms_recipients'] for Ticket::Article.find(#{article.id})") if !article.preferences['sms_recipients']
  16. log_error(article, "Can't find article.preferences['channel_id'] for Ticket::Article.find(#{article.id})") if !article.preferences['channel_id']
  17. channel = Channel.lookup(id: article.preferences['channel_id'])
  18. log_error(article, "No such channel id #{article.preferences['channel_id']}") if !channel
  19. # if sender is agent, take create channel
  20. else
  21. log_error(article, "Can't find ticket.preferences['channel_id'] for Ticket.find(#{ticket.id})") if !ticket.preferences['channel_id']
  22. channel = Channel.lookup(id: ticket.preferences['channel_id'])
  23. log_error(article, "No such channel id #{ticket.preferences['channel_id']}") if !channel
  24. end
  25. begin
  26. if article.sender.name == 'System'
  27. article.preferences['sms_recipients'].each do |recipient|
  28. channel.deliver(
  29. recipient: recipient,
  30. message: article.body.first(160),
  31. )
  32. end
  33. else
  34. channel.deliver(
  35. recipient: article.to,
  36. message: article.body.first(160),
  37. )
  38. end
  39. rescue => e
  40. log_error(article, e.message)
  41. return
  42. end
  43. log_success(article)
  44. return if article.sender.name == 'Agent'
  45. log_history(article, ticket, 'sms', article.to)
  46. end
  47. # log successful delivery
  48. def log_success(article)
  49. article.preferences['delivery_status_message'] = nil
  50. article.preferences['delivery_status'] = 'success'
  51. article.preferences['delivery_status_date'] = Time.zone.now
  52. article.save!
  53. end
  54. def log_error(local_record, message)
  55. local_record.preferences['delivery_status'] = 'fail'
  56. local_record.preferences['delivery_status_message'] = message
  57. local_record.preferences['delivery_status_date'] = Time.zone.now
  58. local_record.save!
  59. Rails.logger.error message
  60. if local_record.preferences['delivery_retry'] >= max_attempts
  61. Ticket::Article.create(
  62. ticket_id: local_record.ticket_id,
  63. content_type: 'text/plain',
  64. body: "#{log_error_prefix}: #{message}",
  65. internal: true,
  66. sender: Ticket::Article::Sender.find_by(name: 'System'),
  67. type: Ticket::Article::Type.find_by(name: 'note'),
  68. preferences: {
  69. delivery_article_id_related: local_record.id,
  70. delivery_message: true,
  71. },
  72. updated_by_id: 1,
  73. created_by_id: 1,
  74. )
  75. end
  76. raise message
  77. end
  78. def log_history(article, ticket, history_type, recipient_list)
  79. return if recipient_list.blank?
  80. History.add(
  81. o_id: article.id,
  82. history_type: history_type,
  83. history_object: 'Ticket::Article',
  84. related_o_id: ticket.id,
  85. related_history_object: 'Ticket',
  86. value_from: article.subject,
  87. value_to: recipient_list,
  88. created_by_id: article.created_by_id,
  89. )
  90. end
  91. def log_error_prefix
  92. 'Unable to send SMS message' # rubocop:disable Zammad/DetectTranslatableString
  93. end
  94. end