Feature: Slack - receive messages, create threads, send replies (#974)

Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
Sojan Jose
2020-06-22 13:19:26 +05:30
committed by GitHub
parent aa8a85b8bd
commit 1ef8d03e18
53 changed files with 815 additions and 188 deletions

View File

@@ -6,7 +6,7 @@ class Integrations::Slack::ChannelBuilder
end
def perform
create_channel
find_or_create_channel
update_reference_id
end
@@ -23,11 +23,12 @@ class Integrations::Slack::ChannelBuilder
Slack::Web::Client.new
end
def create_channel
@channel = slack_client.conversations_create(name: params[:channel])
def find_or_create_channel
exisiting_channel = slack_client.conversations_list.channels.find { |channel| channel['name'] == params[:channel] }
@channel = exisiting_channel || slack_client.conversations_create(name: params[:channel])['channel']
end
def update_reference_id
@hook.reference_id = channel['channel']['id']
@hook.update(reference_id: channel['id'])
end
end

View File

@@ -13,7 +13,7 @@ class Integrations::Slack::HookBuilder
status: 'enabled',
inbox_id: params[:inbox_id],
hook_type: hook_type,
app_id: 'cw_slack'
app_id: 'slack'
)
hook.save!
@@ -32,11 +32,12 @@ class Integrations::Slack::HookBuilder
def fetch_access_token
client = Slack::Web::Client.new
client.oauth_access(
slack_access = client.oauth_v2_access(
client_id: ENV.fetch('SLACK_CLIENT_ID', 'TEST_CLIENT_ID'),
client_secret: ENV.fetch('SLACK_CLIENT_SECRET', 'TEST_CLIENT_SECRET'),
code: params[:code]
)['bot']['bot_access_token']
code: params[:code],
redirect_uri: Integrations::App.slack_integration_url
)
slack_access['access_token']
end
end

View File

@@ -34,7 +34,7 @@ class Integrations::Slack::IncomingMessageBuilder
end
def supported_message?
SUPPORTED_MESSAGE_TYPES.include?(message[:type])
SUPPORTED_MESSAGE_TYPES.include?(message[:type]) if message.present?
end
def hook_verification?
@@ -46,7 +46,7 @@ class Integrations::Slack::IncomingMessageBuilder
end
def message
params[:event][:blocks].first
params[:event][:blocks]&.first
end
def verify_hook
@@ -56,23 +56,42 @@ class Integrations::Slack::IncomingMessageBuilder
end
def integration_hook
@integration_hook ||= Integrations::Hook.where(reference_id: params[:event][:channel])
@integration_hook ||= Integrations::Hook.find_by(reference_id: params[:event][:channel])
end
def conversation
@conversation ||= Conversation.where(identifier: params[:event][:thread_ts]).first
end
def sender
user_email = slack_client.users_info(user: params[:event][:user])[:user][:profile][:email]
conversation.account.users.find_by(email: user_email)
end
def private_note?
params[:event][:text].strip.starts_with?('note:', 'private:')
end
def create_message
return unless conversation
conversation.messages.create(
message_type: 0,
message_type: :outgoing,
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
content: message[:elements].first[:elements].first[:text]
content: params[:event][:text],
source_id: "slack_#{params[:event][:ts]}",
private: private_note?,
user: sender
)
{ status: 'success' }
end
def slack_client
Slack.configure do |config|
config.token = integration_hook.access_token
end
Slack::Web::Client.new
end
end

View File

@@ -11,6 +11,8 @@ class Integrations::Slack::OutgoingMessageBuilder
end
def perform
return if message.source_id.present?
send_message
update_reference_id
end
@@ -25,12 +27,32 @@ class Integrations::Slack::OutgoingMessageBuilder
@contact ||= conversation.contact
end
def agent
@agent ||= message.user
end
def message_content
if conversation.identifier.present?
message.content
else
"*Inbox: #{message.inbox.name}* \n\n #{message.content}"
end
end
def avatar_url(sender)
sender.try(:avatar_url) || "#{ENV['FRONTEND_URL']}/admin/avatar_square.png"
end
def send_message
sender = message.outgoing? ? agent : contact
sender_type = sender.class == Contact ? 'Contact' : 'Agent'
@slack_message = slack_client.chat_postMessage(
channel: hook.reference_id,
text: message.content,
username: contact.try(:name),
thread_ts: conversation.identifier
text: message_content,
username: "#{sender_type}: #{sender.try(:name)}",
thread_ts: conversation.identifier,
icon_url: avatar_url(sender)
)
end