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
|
2020-06-27 16:04:53 +00:00
|
|
|
sender = message.sender
|
2020-06-22 07:49:26 +00:00
|
|
|
sender_type = sender.class == Contact ? 'Contact' : 'Agent'
|
2020-09-04 13:43:47 +00:00
|
|
|
sender_name = sender.try(:name) ? "#{sender_type}: #{sender.try(:name)}" : sender_type
|
2020-06-22 07:49:26 +00:00
|
|
|
|
2020-06-12 17:42:47 +00:00
|
|
|
@slack_message = slack_client.chat_postMessage(
|
|
|
|
channel: hook.reference_id,
|
2020-06-22 07:49:26 +00:00
|
|
|
text: message_content,
|
2020-09-04 13:43:47 +00:00
|
|
|
username: sender_name,
|
2020-06-22 07:49:26 +00:00
|
|
|
thread_ts: conversation.identifier,
|
|
|
|
icon_url: avatar_url(sender)
|
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
|