Feature: Inbox greeting message (#927)
Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
@@ -23,7 +23,7 @@ class Facebook::SendReplyService
|
||||
|
||||
def outgoing_message_from_chatwoot?
|
||||
# messages sent directly from chatwoot won't have source_id.
|
||||
message.outgoing? && !message.source_id
|
||||
(message.outgoing? || message.template?) && !message.source_id
|
||||
end
|
||||
|
||||
# def reopen_lock
|
||||
|
||||
@@ -4,6 +4,8 @@ class MessageTemplates::HookExecutionService
|
||||
def perform
|
||||
return if inbox.agent_bot_inbox&.active?
|
||||
|
||||
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
|
||||
|
||||
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if should_send_email_collect?
|
||||
end
|
||||
|
||||
@@ -16,8 +18,16 @@ class MessageTemplates::HookExecutionService
|
||||
conversation.messages.outgoing.count.zero? && conversation.messages.template.count.zero?
|
||||
end
|
||||
|
||||
def should_send_greeting?
|
||||
first_message_from_contact? && conversation.inbox.greeting_enabled?
|
||||
end
|
||||
|
||||
def email_collect_was_sent?
|
||||
conversation.messages.where(content_type: 'input_email').present?
|
||||
end
|
||||
|
||||
def should_send_email_collect?
|
||||
!contact_has_email? && conversation.inbox.web_widget? && first_message_from_contact?
|
||||
!contact_has_email? && conversation.inbox.web_widget? && !email_collect_was_sent?
|
||||
end
|
||||
|
||||
def contact_has_email?
|
||||
|
||||
@@ -3,7 +3,6 @@ class MessageTemplates::Template::EmailCollect
|
||||
|
||||
def perform
|
||||
ActiveRecord::Base.transaction do
|
||||
conversation.messages.create!(typical_reply_message_params)
|
||||
conversation.messages.create!(ways_to_reach_you_message_params)
|
||||
conversation.messages.create!(email_input_box_template_message_params)
|
||||
end
|
||||
@@ -17,21 +16,6 @@ class MessageTemplates::Template::EmailCollect
|
||||
delegate :contact, :account, to: :conversation
|
||||
delegate :inbox, to: :message
|
||||
|
||||
def typical_reply_message_params
|
||||
content = @conversation.inbox&.channel&.agent_away_message
|
||||
if content.blank?
|
||||
content = I18n.t('conversations.templates.typical_reply_message_body',
|
||||
account_name: account.name)
|
||||
end
|
||||
|
||||
{
|
||||
account_id: @conversation.account_id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
message_type: :template,
|
||||
content: content
|
||||
}
|
||||
end
|
||||
|
||||
def ways_to_reach_you_message_params
|
||||
content = I18n.t('conversations.templates.ways_to_reach_you_message_body',
|
||||
account_name: account.name)
|
||||
|
||||
32
app/services/message_templates/template/greeting.rb
Normal file
32
app/services/message_templates/template/greeting.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class MessageTemplates::Template::Greeting
|
||||
pattr_initialize [:conversation!]
|
||||
|
||||
def perform
|
||||
ActiveRecord::Base.transaction do
|
||||
conversation.messages.create!(greeting_message_params)
|
||||
end
|
||||
rescue StandardError => e
|
||||
Raven.capture_exception(e)
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
delegate :contact, :account, to: :conversation
|
||||
delegate :inbox, to: :message
|
||||
|
||||
def greeting_message_params
|
||||
content = @conversation.inbox&.greeting_message
|
||||
if content.blank?
|
||||
content = I18n.t('conversations.templates.greeting_message_body',
|
||||
account_name: account.name)
|
||||
end
|
||||
|
||||
{
|
||||
account_id: @conversation.account_id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
message_type: :template,
|
||||
content: content
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -5,7 +5,7 @@ class Twilio::OutgoingMessageService
|
||||
return if message.private
|
||||
return if message.source_id
|
||||
return if inbox.channel.class.to_s != 'Channel::TwilioSms'
|
||||
return unless message.outgoing?
|
||||
return unless outgoing_message?
|
||||
|
||||
twilio_message = client.messages.create(message_params)
|
||||
message.update!(source_id: twilio_message.sid)
|
||||
@@ -39,6 +39,10 @@ class Twilio::OutgoingMessageService
|
||||
@channel ||= inbox.channel
|
||||
end
|
||||
|
||||
def outgoing_message?
|
||||
message.outgoing? || message.template?
|
||||
end
|
||||
|
||||
def client
|
||||
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
|
||||
end
|
||||
|
||||
@@ -64,7 +64,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
|
||||
end
|
||||
|
||||
def set_conversation
|
||||
@conversation = @contact_inbox.conversations.first
|
||||
@conversation = @contact_inbox.conversations.where("additional_attributes ->> 'type' = 'direct_message'").first
|
||||
return if @conversation
|
||||
|
||||
@conversation = ::Conversation.create!(conversation_params)
|
||||
|
||||
@@ -56,7 +56,7 @@ class Twitter::SendReplyService
|
||||
end
|
||||
|
||||
def outgoing_message_from_chatwoot?
|
||||
message.outgoing?
|
||||
(message.outgoing? || message.template?)
|
||||
end
|
||||
|
||||
delegate :additional_attributes, to: :contact
|
||||
|
||||
Reference in New Issue
Block a user