Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d39088cd6 | ||
|
|
c3341b40c7 | ||
|
|
db331e0193 | ||
|
|
034e951473 | ||
|
|
a5c72e44ec |
@@ -343,6 +343,7 @@ class Message < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def execute_message_template_hooks
|
def execute_message_template_hooks
|
||||||
|
Rails.logger.info("[Message][#{id}] Executing message template hooks")
|
||||||
::MessageTemplates::HookExecutionService.new(message: self).perform
|
::MessageTemplates::HookExecutionService.new(message: self).perform
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,17 @@ class MessageTemplates::HookExecutionService
|
|||||||
pattr_initialize [:message!]
|
pattr_initialize [:message!]
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
return if conversation.campaign.present?
|
if conversation.campaign.present?
|
||||||
return if conversation.last_incoming_message.blank?
|
Rails.logger.debug { "[OutOfOffice][#{conversation.id}] Not triggering templates because conversation has a campaign" }
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if conversation.last_incoming_message.blank?
|
||||||
|
Rails.logger.debug { "[OutOfOffice][#{conversation.id}] Not triggering templates because there is no incoming message" }
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
Rails.logger.info "[OutOfOffice][#{conversation.id}] Triggering templates for conversation ##{conversation.id}"
|
||||||
trigger_templates
|
trigger_templates
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -22,14 +30,41 @@ class MessageTemplates::HookExecutionService
|
|||||||
|
|
||||||
def should_send_out_of_office_message?
|
def should_send_out_of_office_message?
|
||||||
# should not send if its a tweet message
|
# should not send if its a tweet message
|
||||||
return false if conversation.tweet?
|
if conversation.tweet?
|
||||||
|
Rails.logger.debug { "[OutOfOffice][#{conversation.id}] Not sending out-of-office message because it's a tweet conversation" }
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
# should not send for outbound messages
|
# should not send for outbound messages
|
||||||
return false unless message.incoming?
|
unless message.incoming?
|
||||||
|
Rails.logger.debug { "[OutOfOffice][#{conversation.id}] Not sending out-of-office message because the message is outgoing" }
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
# prevents sending out-of-office message if an agent has sent a message in last 5 minutes
|
# 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
|
# ensures better UX by not interrupting active conversations at the end of business hours
|
||||||
return false if conversation.messages.outgoing.exists?(['created_at > ?', 5.minutes.ago])
|
if conversation.messages.outgoing.where(private: false).exists?(['created_at > ?', 5.minutes.ago])
|
||||||
|
Rails.logger.debug { "[OutOfOffice][#{conversation.id}] Not sending out-of-office message because an agent responded in the last 5 minutes" }
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present?
|
can_send = inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present?
|
||||||
|
|
||||||
|
if can_send
|
||||||
|
Rails.logger.info "[OutOfOffice][#{conversation.id}] Sending out-of-office message for conversation ##{conversation.id}"
|
||||||
|
else
|
||||||
|
reasons = []
|
||||||
|
reasons << 'inbox not in out-of-office mode' unless inbox.out_of_office?
|
||||||
|
reasons << 'conversation already has a template message today' unless conversation.messages.today.template.empty?
|
||||||
|
reasons << 'inbox has no out-of-office message configured' unless inbox.out_of_office_message.present?
|
||||||
|
Rails.logger.debug { "[OutOfOffice][#{conversation.id}] Not sending out-of-office message because: #{reasons.join(', ')}" }
|
||||||
|
end
|
||||||
|
|
||||||
|
can_send
|
||||||
|
rescue StandardError => e
|
||||||
|
Rails.logger.error("[OutOfOffice][#{conversation.id}] Error triggering out of office message: #{e.message}")
|
||||||
|
ChatwootExceptionTracker.new(e, account: conversation.account).capture_exception
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def first_message_from_contact?
|
def first_message_from_contact?
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ class MessageTemplates::Template::OutOfOffice
|
|||||||
pattr_initialize [:conversation!]
|
pattr_initialize [:conversation!]
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
|
Rails.logger.info("[OutOfOffice][#{conversation.id}] Triggering out of office message")
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
conversation.messages.create!(out_of_office_message_params)
|
conversation.messages.create!(out_of_office_message_params)
|
||||||
end
|
end
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
|
Rails.logger.error("[OutOfOffice][#{conversation.id}] Error triggering out of office message: #{e.message}")
|
||||||
ChatwootExceptionTracker.new(e, account: conversation.account).capture_exception
|
ChatwootExceptionTracker.new(e, account: conversation.account).capture_exception
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user