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
|
|
|
|
update_reference_id
|
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
|
|
|
|
@slack_message = slack_client.chat_postMessage(
|
|
|
|
channel: hook.reference_id,
|
2020-06-22 07:49:26 +00:00
|
|
|
text: 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
|
|
|
)
|
2020-10-27 18:52:57 +00:00
|
|
|
rescue Slack::Web::Api::Errors::AccountInactive => e
|
|
|
|
Rails.logger.info e
|
|
|
|
hook.authorization_error!
|
|
|
|
hook.disable if hook.enabled?
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|