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. REMINDER_TEMPLATE = __('Hello, the customer service window for this conversation is about to expire, please reply to keep it open.').freeze
  5. def lock_key
  6. # "ScheduledWhatsappReminderJob/1"
  7. "#{self.class.name}/#{arguments[0].id}"
  8. end
  9. def self.perform_at(scheduled_time, ticket, locale)
  10. set(wait_until: scheduled_time).perform_later(ticket, locale)
  11. end
  12. def perform(ticket, locale)
  13. channel = Channel.find_by(id: ticket.preferences[:channel_id], area: 'WhatsApp::Business')
  14. return if channel.nil? || !channel.options[:reminder_active]
  15. # Do not run for closed tickets.
  16. return if Ticket::State.where(name: %w[closed merged removed]).pluck(:id).include?(ticket.state_id)
  17. profile_name = ticket.preferences.dig(:whatsapp, :from, :display_name)
  18. phone_number = ticket.preferences.dig(:whatsapp, :from, :phone_number)
  19. translated_reminder_message = Translation.translate(locale, REMINDER_TEMPLATE)
  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