scheduled_whatsapp_reminder_job.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ScheduledWhatsappReminderJob < ApplicationJob
  3. include HasActiveJobLock
  4. DEFAULT_REMINDER_MESSAGE = __('Hello, the customer service window for this conversation is about to expire, please reply to keep it open.').freeze
  5. def lock_key
  6. "#{self.class.name}/#{SecureRandom.uuid}"
  7. end
  8. def self.perform_at(scheduled_time, ticket, locale)
  9. set(wait_until: scheduled_time).perform_later(ticket, locale)
  10. end
  11. def perform(ticket, locale)
  12. channel = Channel.find_by(id: ticket.preferences[:channel_id], area: 'WhatsApp::Business')
  13. return if channel.nil? || !channel.options[:reminder_active]
  14. # Do not run for closed tickets.
  15. return if Ticket::State.where(name: %w[closed merged removed]).pluck(:id).include?(ticket.state_id)
  16. profile_name = ticket.preferences.dig(:whatsapp, :from, :display_name)
  17. phone_number = ticket.preferences.dig(:whatsapp, :from, :phone_number)
  18. reminder_message = channel.options[:reminder_message].presence || DEFAULT_REMINDER_MESSAGE
  19. translated_reminder_message = Translation.translate(locale, reminder_message)
  20. UserInfo.with_user_id(1) do
  21. Ticket::Article.create!(
  22. ticket_id: ticket.id,
  23. type_id: Ticket::Article::Type.lookup(name: 'whatsapp message').id,
  24. sender_id: Ticket::Article::Sender.lookup(name: 'System').id,
  25. from: "#{channel.options[:name]} (#{channel.options[:phone_number]})",
  26. to: "#{profile_name} (#{phone_number})",
  27. subject: translated_reminder_message.truncate(100, omission: '…'),
  28. internal: false,
  29. body: translated_reminder_message,
  30. content_type: 'text/plain',
  31. )
  32. end
  33. end
  34. end