2020-06-25 18:05:16 +00:00
|
|
|
class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
|
|
|
|
pattr_initialize [:message!, :hook!]
|
2020-06-12 17:42:47 +00:00
|
|
|
|
|
|
|
def perform
|
2020-06-25 18:05:16 +00:00
|
|
|
# overriding the base class logic since the validations are different in this case.
|
|
|
|
# FIXME: for now we will only send messages from widget to slack
|
2020-08-28 20:09:41 +00:00
|
|
|
return unless valid_channel_for_slack?
|
2020-06-25 18:05:16 +00:00
|
|
|
# we don't want message loop in slack
|
2020-08-28 20:09:41 +00:00
|
|
|
return if message.external_source_id_slack.present?
|
2020-06-25 18:05:16 +00:00
|
|
|
# we don't want to start slack thread from agent conversation as of now
|
|
|
|
return if message.outgoing? && conversation.identifier.blank?
|
2020-06-22 07:49:26 +00:00
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
perform_reply
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-28 20:09:41 +00:00
|
|
|
def valid_channel_for_slack?
|
2020-09-12 17:39:55 +00:00
|
|
|
# slack wouldn't be an ideal interface to reply to tweets, hence disabling that case
|
2020-08-28 20:09:41 +00:00
|
|
|
return false if channel.is_a?(Channel::TwitterProfile) && conversation.additional_attributes['type'] == 'tweet'
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
def perform_reply
|
|
|
|
send_message
|
2020-12-10 17:23:49 +00:00
|
|
|
|
|
|
|
return unless @slack_message
|
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
update_reference_id
|
2020-12-10 17:23:49 +00:00
|
|
|
update_external_source_id_slack
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
2020-06-22 07:49:26 +00:00
|
|
|
def message_content
|
2020-06-25 18:05:16 +00:00
|
|
|
private_indicator = message.private? ? 'private: ' : ''
|
2020-06-22 07:49:26 +00:00
|
|
|
if conversation.identifier.present?
|
2020-06-25 18:05:16 +00:00
|
|
|
"#{private_indicator}#{message.content}"
|
2020-06-22 07:49:26 +00:00
|
|
|
else
|
2020-09-04 13:43:47 +00:00
|
|
|
"*Inbox: #{message.inbox.name} [#{message.inbox.inbox_type}]* \n\n #{message.content}"
|
2020-06-22 07:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_url(sender)
|
|
|
|
sender.try(:avatar_url) || "#{ENV['FRONTEND_URL']}/admin/avatar_square.png"
|
|
|
|
end
|
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
def send_message
|
2021-09-17 19:19:01 +00:00
|
|
|
post_message if message_content.present?
|
|
|
|
upload_file if message.attachments.any?
|
|
|
|
rescue Slack::Web::Api::Errors::AccountInactive => e
|
|
|
|
Rails.logger.info e
|
|
|
|
hook.authorization_error!
|
|
|
|
hook.disable if hook.enabled?
|
|
|
|
end
|
2021-09-15 12:42:56 +00:00
|
|
|
|
2021-09-17 19:19:01 +00:00
|
|
|
def post_message
|
2020-06-12 17:42:47 +00:00
|
|
|
@slack_message = slack_client.chat_postMessage(
|
|
|
|
channel: hook.reference_id,
|
2021-11-01 06:38:10 +00:00
|
|
|
text: ActionView::Base.full_sanitizer.sanitize(message_content),
|
2020-10-27 18:52:57 +00:00
|
|
|
username: sender_name(message.sender),
|
2020-06-22 07:49:26 +00:00
|
|
|
thread_ts: conversation.identifier,
|
2020-10-27 18:52:57 +00:00
|
|
|
icon_url: avatar_url(message.sender)
|
2020-06-12 17:42:47 +00:00
|
|
|
)
|
2021-09-17 19:19:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_file
|
|
|
|
result = slack_client.files_upload({
|
|
|
|
channels: hook.reference_id,
|
|
|
|
initial_comment: 'Attached File!',
|
|
|
|
thread_ts: conversation.identifier
|
|
|
|
}.merge(file_information))
|
|
|
|
Rails.logger.info(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_type
|
|
|
|
File.extname(message.attachments.first.file_url).strip.downcase[1..]
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_information
|
|
|
|
{
|
|
|
|
filename: message.attachments.first.file.filename,
|
|
|
|
filetype: file_type,
|
|
|
|
content: message.attachments.first.file.download,
|
|
|
|
title: message.attachments.first.file.filename
|
|
|
|
}
|
2020-10-27 18:52:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def sender_name(sender)
|
|
|
|
sender.try(:name) ? "#{sender_type(sender)}: #{sender.try(:name)}" : sender_type(sender)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender_type(sender)
|
2020-11-16 14:11:52 +00:00
|
|
|
sender.instance_of?(Contact) ? 'Contact' : 'Agent'
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_reference_id
|
|
|
|
return if conversation.identifier
|
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
conversation.update!(identifier: @slack_message['ts'])
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
|
2020-12-10 17:23:49 +00:00
|
|
|
def update_external_source_id_slack
|
|
|
|
return unless @slack_message['message']
|
|
|
|
|
|
|
|
message.update!(external_source_id_slack: "cw-origin-#{@slack_message['message']['ts']}")
|
|
|
|
end
|
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
def slack_client
|
2020-06-25 18:05:16 +00:00
|
|
|
@slack_client ||= Slack::Web::Client.new(token: hook.access_token)
|
2020-06-12 17:42:47 +00:00
|
|
|
end
|
|
|
|
end
|