A while back, we added a UX enhancement that disabled sending the OOO message when an agent had replied to the conversation in the last 5 minutes. https://github.com/chatwoot/chatwoot/pull/11073 This would not send a "we will be back" message even if that reply was a private note. This would break for bots that posted private notes, and even for agents who need to just add a note and not actually reply to the email.
78 lines
2.7 KiB
Ruby
78 lines
2.7 KiB
Ruby
class MessageTemplates::HookExecutionService
|
|
pattr_initialize [:message!]
|
|
|
|
def perform
|
|
return if conversation.campaign.present?
|
|
return if conversation.last_incoming_message.blank?
|
|
|
|
trigger_templates
|
|
end
|
|
|
|
private
|
|
|
|
delegate :inbox, :conversation, to: :message
|
|
delegate :contact, to: :conversation
|
|
|
|
def trigger_templates
|
|
::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message?
|
|
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
|
|
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if inbox.enable_email_collect && should_send_email_collect?
|
|
::MessageTemplates::Template::CsatSurvey.new(conversation: conversation).perform if should_send_csat_survey?
|
|
end
|
|
|
|
def should_send_out_of_office_message?
|
|
# should not send if its a tweet message
|
|
return false if conversation.tweet?
|
|
# should not send for outbound messages
|
|
return false unless message.incoming?
|
|
# prevents sending out-of-office message if an agent has sent a message in last 5 minutes
|
|
# ensures better UX by not interrupting active conversations at the end of business hours
|
|
return false if conversation.messages.outgoing.where(private: false).exists?(['created_at > ?', 5.minutes.ago])
|
|
|
|
inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present?
|
|
end
|
|
|
|
def first_message_from_contact?
|
|
conversation.messages.outgoing.count.zero? && conversation.messages.template.count.zero?
|
|
end
|
|
|
|
def should_send_greeting?
|
|
# should not send if its a tweet message
|
|
return false if conversation.tweet?
|
|
|
|
first_message_from_contact? && inbox.greeting_enabled? && inbox.greeting_message.present?
|
|
end
|
|
|
|
def email_collect_was_sent?
|
|
conversation.messages.where(content_type: 'input_email').present?
|
|
end
|
|
|
|
# TODO: we should be able to reduce this logic once we have a toggle for email collect messages
|
|
def should_send_email_collect?
|
|
!contact_has_email? && inbox.web_widget? && !email_collect_was_sent?
|
|
end
|
|
|
|
def contact_has_email?
|
|
contact.email
|
|
end
|
|
|
|
def csat_enabled_conversation?
|
|
return false unless conversation.resolved?
|
|
# should not sent since the link will be public
|
|
return false if conversation.tweet?
|
|
return false unless inbox.csat_survey_enabled?
|
|
|
|
true
|
|
end
|
|
|
|
def should_send_csat_survey?
|
|
return unless csat_enabled_conversation?
|
|
|
|
# only send CSAT once in a conversation
|
|
return if conversation.messages.where(content_type: :input_csat).present?
|
|
|
|
true
|
|
end
|
|
end
|
|
MessageTemplates::HookExecutionService.prepend_mod_with('MessageTemplates::HookExecutionService')
|