Compare commits

...
5 changed files with 157 additions and 14 deletions
@@ -66,4 +66,27 @@ module SlackMessageCreation
:file
end
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.downcase.starts_with?('note:', 'private:')
end
def extract_conversation_id(url)
conversation_id_regex = %r{/conversations/(\d+)}
match_data = url.match(conversation_id_regex)
match_data[1] if match_data
end
def find_conversation_by_id(conversation_id)
Conversation.find_by(display_id: conversation_id)
end
end
@@ -0,0 +1,21 @@
module Slack
module Web
module Api
module Endpoints
module Chat
# TODO: Remove this monkey patch when PR for this issue https://github.com/slack-ruby/slack-ruby-client/issues/388 is merged
def chat_unfurl(options = {})
if (options[:channel].nil? || options[:ts].nil?) && (options[:unfurl_id].nil? || options[:source].nil?)
raise ArgumentError, 'Either a combination of :channel and :ts or :unfurl_id and :source is required'
end
raise ArgumentError, 'Required arguments :unfurls missing' if options[:unfurls].nil?
options = options.merge(channel: conversations_id(options)['channel']['id']) if options[:channel]
post('chat.unfurl', options)
end
end
end
end
end
end
@@ -1,9 +1,11 @@
class Integrations::Slack::IncomingMessageBuilder
include SlackMessageCreation
include Integrations::Slack::LinkUnfurlHelper
attr_reader :params
SUPPORTED_EVENT_TYPES = %w[event_callback url_verification].freeze
SUPPORTED_EVENTS = %w[message].freeze
SUPPORTED_EVENTS = %w[message link_shared].freeze
SUPPORTED_MESSAGE_TYPES = %w[rich_text].freeze
def initialize(params)
@@ -17,6 +19,8 @@ class Integrations::Slack::IncomingMessageBuilder
verify_hook
elsif create_message?
create_message
elsif link_shared?
create_link_shared_message
end
end
@@ -68,6 +72,55 @@ class Integrations::Slack::IncomingMessageBuilder
thread_timestamp_available? && supported_message? && integration_hook
end
def link_shared?
params[:event][:type] == 'link_shared'
end
def create_link_shared_message
conversation = conversation_from_params
return unless conversation
user_name, email, phone_number, company_name = conntact_attributes(conversation).values_at(:user_name, :email, :phone_number, :company_name)
unfurls = generate_unfurls(conversation_url, user_name, email, phone_number, company_name)
send_unfurls(unfurls)
end
def conntact_attributes(conversation)
contact = conversation.contact
user_name = contact&.name.presence || '---'
email = contact&.email.presence || '---'
phone_number = contact&.phone_number.presence || '---'
company_name = contact&.additional_attributes&.dig('company_name').presence || '---'
{
user_name: user_name,
email: email,
phone_number: phone_number,
company_name: company_name
}
end
def conversation_from_params
conversation_id = extract_conversation_id(conversation_url)
return unless conversation_id
find_conversation_by_id(conversation_id)
end
def conversation_url
params.dig(:event, :links, 0, :url)
end
def send_unfurls(unfurls)
slack_service = Integrations::Slack::SendOnSlackService.new(message: nil, hook: integration_hook)
slack_service.link_unfurl(
unfurl_id: params.dig(:event, :unfurl_id),
source: params.dig(:event, :source),
unfurls: JSON.generate(unfurls)
)
end
def message
params[:event][:blocks]&.first
end
@@ -82,19 +135,6 @@ class Integrations::Slack::IncomingMessageBuilder
@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.downcase.starts_with?('note:', 'private:')
end
def slack_client
@slack_client ||= Slack::Web::Client.new(token: @integration_hook.access_token)
end
@@ -0,0 +1,53 @@
module Integrations::Slack::LinkUnfurlHelper
def generate_unfurls(url, user_name, email, phone_number, company_name)
{
url => {
'blocks' => user_info_blocks(user_name, email, phone_number, company_name) +
open_conversation_button(url)
}
}
end
private
def user_info_blocks(user_name, email, phone_number, company_name)
[
{
'type' => 'section',
'fields' => [
user_info_field('Name', user_name),
user_info_field('Email', email),
user_info_field('Phone', phone_number),
user_info_field('Company', company_name)
]
}
]
end
def user_info_field(label, value)
{
'type' => 'mrkdwn',
'text' => "*#{label}:*\n#{value}"
}
end
def open_conversation_button(url)
[
{
'type' => 'actions',
'elements' => [
{
'type' => 'button',
'text' => {
'type' => 'plain_text',
'text' => 'Open conversation',
'emoji' => true
},
'url' => url,
'action_id' => 'button-action'
}
]
}
]
end
end
@@ -14,6 +14,12 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
perform_reply
end
def link_unfurl(event)
slack_client.chat_unfurl(
event
)
end
private
def valid_channel_for_slack?