mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-04 21:02:35 +08:00
Some checks failed
Frontend Lint & Test / test (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Co-authored-by: Aakash Bakhle <48802744+aakashb95@users.noreply.github.com>
63 lines
2.3 KiB
Ruby
63 lines
2.3 KiB
Ruby
class MessageTemplates::HookExecutionService
|
|
pattr_initialize [:message!]
|
|
|
|
def perform
|
|
return if conversation.last_incoming_message.blank?
|
|
return if message.auto_reply_email?
|
|
|
|
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?
|
|
end
|
|
|
|
def should_send_out_of_office_message?
|
|
return false if conversation.campaign.present?
|
|
# 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?
|
|
return false if conversation.campaign.present?
|
|
# 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?
|
|
return false if conversation.campaign.present?
|
|
|
|
!contact_has_email? && inbox.web_widget? && !email_collect_was_sent?
|
|
end
|
|
|
|
def contact_has_email?
|
|
contact.email
|
|
end
|
|
end
|
|
MessageTemplates::HookExecutionService.prepend_mod_with('MessageTemplates::HookExecutionService')
|