[#139] Send conversation emails (#442)

* [#139] Delayed emails for conversations

* Added the setex and get methods to Redis wrapper
* Set the priorities for the sidekiq queues
* Was not able to use mailhog for testing email in local, switched back to letter opener and added comments on using the SMTP settings
* Added after create hood in messages to queue the sending of mail after 2 minutes using sidekiq worker and also set the redis key for the conversation to avoid the email sending for every message
* Added the sidekiq worker to send the email and delete the conversation redis key
* Added the mailer and mail template
* mailer sends the last 10 messages along with the new messages from the time it was queued

* Send email only in development or if smtp config is set

* Send email only in development or if smtp config is set
* Set the SMTP_PORT in production variable

* Adding redis to circle CI

* Specs for the conversation email changes

* Added specs for conversation email sidekiq worker
* Added specs for conversation mailer
* Added specs in message model for the after create hook for notify email

* Send emails only when there is a reply from agent

* set development to use mailhog

* Adding comments for using letter opener
This commit is contained in:
Sony Mathew
2020-01-23 23:14:07 +05:45
committed by Sojan Jose
parent 1d3ed016be
commit d4b3ba4baa
18 changed files with 184 additions and 13 deletions

View File

@@ -4,4 +4,8 @@ class ApplicationMailer < ActionMailer::Base
# helpers
helper :frontend_urls
def smtp_config_set_or_development?
ENV.fetch('SMTP_ADDRESS', nil).present? || Rails.env.development?
end
end

View File

@@ -3,7 +3,7 @@ class AssignmentMailer < ApplicationMailer
layout 'mailer'
def conversation_assigned(conversation, agent)
return if ENV.fetch('SMTP_ADDRESS', nil).blank?
return unless smtp_config_set_or_development?
@agent = agent
@conversation = conversation

View File

@@ -0,0 +1,26 @@
class ConversationMailer < ApplicationMailer
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
layout 'mailer'
def new_message(conversation, message_queued_time)
return unless smtp_config_set_or_development?
@conversation = conversation
@contact = @conversation.contact
@agent = @conversation.assignee
recap_messages = @conversation.messages.where('created_at < ?', message_queued_time).order(created_at: :asc).last(10)
new_messages = @conversation.messages.where('created_at >= ?', message_queued_time)
@messages = recap_messages + new_messages
@messages = @messages.select(&:reportable?)
mail(to: @contact&.email, from: @agent&.email, subject: mail_subject(@messages.last))
end
private
def mail_subject(last_message, trim_length = 30)
"[##{@conversation.display_id}] #{last_message.content.truncate(trim_length)}"
end
end

View File

@@ -49,7 +49,8 @@ class Message < ApplicationRecord
after_create :reopen_conversation,
:dispatch_event,
:send_reply,
:execute_message_template_hooks
:execute_message_template_hooks,
:notify_via_mail
def channel_token
@token ||= inbox.channel.try(:page_access_token)
@@ -94,4 +95,17 @@ class Message < ApplicationRecord
def execute_message_template_hooks
::MessageTemplates::HookExecutionService.new(message: self).perform
end
def notify_via_mail
conversation_mail_key = Redis::Alfred::CONVERSATION_MAILER_KEY % conversation.id
if Redis::Alfred.get(conversation_mail_key).nil? && conversation.contact.email? && outgoing?
# set a redis key for the conversation so that we don't need to send email for every
# new message that comes in and we dont enque the delayed sidekiq job for every message
Redis::Alfred.setex(conversation_mail_key, Time.zone.now)
# Since this is live chat, send the email after few minutes so the only one email with
# last few messages coupled together is sent rather than email for each message
ConversationEmailWorker.perform_in(2.minutes, conversation.id, Time.zone.now)
end
end
end

View File

@@ -0,0 +1,16 @@
<p>Hi <%= @contact.name %>,</p>
<p>You have new messages on your conversation.</p>
<table>
<% @messages.each do |message| %>
<tr>
<td>
<b><%= message.incoming? ? 'You' : message.user.name %></b>
</td>
<td>: <%= message.content %></td>
</tr>
<% end %>
</table>
<p>Click <%= link_to 'here', app_conversation_url(id: @conversation.display_id) %> to get back to the conversation. </p>

View File

@@ -0,0 +1,15 @@
class ConversationEmailWorker
include Sidekiq::Worker
sidekiq_options queue: :mailers
def perform(conversation_id, queued_time)
@conversation = Conversation.find(conversation_id)
# send the email
ConversationMailer.new_message(@conversation, queued_time).deliver_later
# delete the redis set from the first new message on the conversation
conversation_mail_key = Redis::Alfred::CONVERSATION_MAILER_KEY % @conversation.id
Redis::Alfred.delete(conversation_mail_key)
end
end