diff --git a/app/models/concerns/slack_message_creation.rb b/app/models/concerns/slack_message_creation.rb index a6a59ccf7..c7c11a10d 100644 --- a/app/models/concerns/slack_message_creation.rb +++ b/app/models/concerns/slack_message_creation.rb @@ -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 diff --git a/lib/integrations/slack/incoming_message_builder.rb b/lib/integrations/slack/incoming_message_builder.rb index 2e98a7244..1752d1349 100644 --- a/lib/integrations/slack/incoming_message_builder.rb +++ b/lib/integrations/slack/incoming_message_builder.rb @@ -1,5 +1,7 @@ class Integrations::Slack::IncomingMessageBuilder include SlackMessageCreation + include Integrations::Slack::LinkUnfurlHelper + attr_reader :params SUPPORTED_EVENT_TYPES = %w[event_callback url_verification].freeze @@ -96,61 +98,6 @@ class Integrations::Slack::IncomingMessageBuilder ) 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 - - def generate_unfurls(url, user_name, email, phone_number, company_name) - { - url => { - 'blocks' => [ - { - :type => 'section', - 'fields' => [ - { - 'type' => 'mrkdwn', - 'text' => "*Name:*\n#{user_name}" - }, - { - 'type' => 'mrkdwn', - 'text' => "*Email:*\n#{email}" - }, - { - 'type' => 'mrkdwn', - 'text' => "*Phone:*\n#{phone_number}" - }, - { - 'type' => 'mrkdwn', - 'text' => "*Company:*\n#{company_name}" - } - ] - }, - { - 'type' => 'actions', - 'elements' => [ - { - 'type' => 'button', - 'text' => { - 'type' => 'plain_text', - 'text' => 'Open conversation', - 'emoji' => true - }, - 'url' => url, - 'action_id' => 'button-action' - } - ] - } - ] - } - } - end - def message params[:event][:blocks]&.first end @@ -165,19 +112,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 diff --git a/lib/integrations/slack/link_unfurl_helper.rb b/lib/integrations/slack/link_unfurl_helper.rb new file mode 100644 index 000000000..bcb9a782c --- /dev/null +++ b/lib/integrations/slack/link_unfurl_helper.rb @@ -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