2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration::Provider::SlackProvider
|
|
|
|
class SlackCommandController < DiscourseChatIntegration::Provider::HookController
|
|
|
|
requires_provider ::DiscourseChatIntegration::Provider::SlackProvider::PROVIDER_NAME
|
2017-07-05 10:03:02 -04:00
|
|
|
|
2017-09-03 22:44:32 -04:00
|
|
|
before_action :slack_token_valid?, only: :command
|
|
|
|
before_action :slack_payload_token_valid?, only: :interactive
|
2017-07-05 10:03:02 -04:00
|
|
|
|
2017-09-03 22:44:32 -04:00
|
|
|
skip_before_action :check_xhr,
|
2017-07-05 18:01:46 -04:00
|
|
|
:preload_json,
|
|
|
|
:verify_authenticity_token,
|
|
|
|
:redirect_to_login_if_required,
|
2022-12-29 07:31:05 -05:00
|
|
|
only: %i[command interactive]
|
2017-07-05 18:01:46 -04:00
|
|
|
|
|
|
|
def command
|
2017-08-03 10:48:33 -04:00
|
|
|
message = process_command(params)
|
2017-07-05 18:01:46 -04:00
|
|
|
|
2017-08-03 10:48:33 -04:00
|
|
|
render json: message
|
2017-07-05 18:01:46 -04:00
|
|
|
end
|
|
|
|
|
2021-03-01 14:07:31 -05:00
|
|
|
def interactive
|
|
|
|
json = JSON.parse(params[:payload], symbolize_names: true)
|
|
|
|
process_interactive(json)
|
2021-04-22 13:50:11 -04:00
|
|
|
head :ok
|
2021-03-01 14:07:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-07-05 18:01:46 -04:00
|
|
|
def process_command(params)
|
|
|
|
tokens = params[:text].split(" ")
|
|
|
|
|
|
|
|
# channel name fix
|
2017-07-13 18:21:15 -04:00
|
|
|
channel_id =
|
2017-07-05 18:01:46 -04:00
|
|
|
case params[:channel_name]
|
2022-12-29 07:31:05 -05:00
|
|
|
when "directmessage"
|
2017-07-05 18:01:46 -04:00
|
|
|
"@#{params[:user_name]}"
|
2022-12-29 07:31:05 -05:00
|
|
|
when "privategroup"
|
2019-09-30 08:27:35 -04:00
|
|
|
params[:channel_id]
|
2017-07-05 18:01:46 -04:00
|
|
|
else
|
|
|
|
"##{params[:channel_name]}"
|
|
|
|
end
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
provider = DiscourseChatIntegration::Provider::SlackProvider::PROVIDER_NAME
|
2017-07-06 16:42:37 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
channel =
|
|
|
|
DiscourseChatIntegration::Channel
|
|
|
|
.with_provider(provider)
|
|
|
|
.with_data_value("identifier", channel_id)
|
|
|
|
.first
|
|
|
|
|
|
|
|
channel ||=
|
|
|
|
DiscourseChatIntegration::Channel.create!(
|
|
|
|
provider: provider,
|
|
|
|
data: {
|
|
|
|
identifier: channel_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if tokens[0] == "post"
|
|
|
|
process_post_request(
|
|
|
|
channel,
|
|
|
|
tokens,
|
|
|
|
params[:channel_id],
|
|
|
|
channel_id,
|
|
|
|
params[:response_url],
|
|
|
|
)
|
2017-10-09 22:08:26 -04:00
|
|
|
else
|
2021-07-13 15:36:16 -04:00
|
|
|
{ text: ::DiscourseChatIntegration::Helper.process_command(channel, tokens) }
|
2017-07-24 11:43:37 -04:00
|
|
|
end
|
2017-07-05 18:01:46 -04:00
|
|
|
end
|
|
|
|
|
2017-09-15 18:12:02 -04:00
|
|
|
def process_post_request(channel, tokens, slack_channel_id, channel_name, response_url)
|
2017-07-24 11:43:37 -04:00
|
|
|
if SiteSetting.chat_integration_slack_access_token.empty?
|
2017-08-03 19:54:11 -04:00
|
|
|
return { text: I18n.t("chat_integration.provider.slack.transcript.api_required") }
|
2017-07-24 11:43:37 -04:00
|
|
|
end
|
|
|
|
|
2017-09-15 18:12:02 -04:00
|
|
|
Scheduler::Defer.later "Processing slack transcript request" do
|
2022-12-29 07:31:05 -05:00
|
|
|
response =
|
|
|
|
build_post_request_response(channel, tokens, slack_channel_id, channel_name, response_url)
|
2021-07-13 15:36:16 -04:00
|
|
|
http = DiscourseChatIntegration::Provider::SlackProvider.slack_api_http
|
2022-12-29 07:31:05 -05:00
|
|
|
req = Net::HTTP::Post.new(URI(response_url), "Content-Type" => "application/json")
|
2021-03-01 14:07:31 -05:00
|
|
|
req.body = response.to_json
|
|
|
|
http.request(req)
|
2017-08-15 03:26:03 -04:00
|
|
|
end
|
2017-07-24 11:43:37 -04:00
|
|
|
|
2019-11-14 15:03:49 -05:00
|
|
|
{ text: I18n.t("chat_integration.provider.slack.transcript.loading") }
|
2017-07-24 11:43:37 -04:00
|
|
|
end
|
|
|
|
|
2021-03-01 14:07:31 -05:00
|
|
|
def build_post_request_response(channel, tokens, slack_channel_id, channel_name, response_url)
|
|
|
|
requested_messages = nil
|
|
|
|
first_message_ts = nil
|
|
|
|
requested_thread_ts = nil
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
thread_url_regex =
|
|
|
|
/^https:\/\/\S+\.slack\.com\/archives\/\S+\/p[0-9]{16}\?thread_ts=([0-9]{10}.[0-9]{6})\S*$/
|
2021-03-01 14:07:31 -05:00
|
|
|
slack_url_regex = /^https:\/\/\S+\.slack\.com\/archives\/\S+\/p([0-9]{16})\/?$/
|
|
|
|
|
|
|
|
if tokens.size > 2 && tokens[1] == "thread" && match = slack_url_regex.match(tokens[2])
|
2022-12-29 07:31:05 -05:00
|
|
|
requested_thread_ts = match.captures[0].insert(10, ".")
|
2021-03-01 14:07:31 -05:00
|
|
|
elsif tokens.size > 1 && match = thread_url_regex.match(tokens[1])
|
|
|
|
requested_thread_ts = match.captures[0]
|
|
|
|
elsif tokens.size > 1 && match = slack_url_regex.match(tokens[1])
|
2022-12-29 07:31:05 -05:00
|
|
|
first_message_ts = match.captures[0].insert(10, ".")
|
2021-03-01 14:07:31 -05:00
|
|
|
elsif tokens.size > 1
|
|
|
|
begin
|
|
|
|
requested_messages = Integer(tokens[1], 10)
|
|
|
|
rescue ArgumentError
|
|
|
|
return { text: I18n.t("chat_integration.provider.slack.parse_error") }
|
|
|
|
end
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2021-03-01 14:07:31 -05:00
|
|
|
error_key = "chat_integration.provider.slack.transcript.error"
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
unless transcript =
|
|
|
|
SlackTranscript.new(
|
|
|
|
channel_name: channel_name,
|
|
|
|
channel_id: slack_channel_id,
|
|
|
|
requested_thread_ts: requested_thread_ts,
|
|
|
|
)
|
|
|
|
return { text: I18n.t(error_key) }
|
|
|
|
end
|
2021-03-01 14:07:31 -05:00
|
|
|
return { text: I18n.t("#{error_key}_users") } unless transcript.load_user_data
|
|
|
|
return { text: I18n.t("#{error_key}_history") } unless transcript.load_chat_history
|
|
|
|
|
|
|
|
if first_message_ts
|
2022-12-29 07:31:05 -05:00
|
|
|
unless transcript.set_first_message_by_ts(first_message_ts)
|
|
|
|
return { text: I18n.t("#{error_key}_ts") }
|
|
|
|
end
|
2021-03-01 14:07:31 -05:00
|
|
|
elsif requested_messages
|
|
|
|
transcript.set_first_message_by_index(-requested_messages)
|
|
|
|
else
|
|
|
|
transcript.set_first_message_by_index(-10) unless transcript.guess_first_message
|
|
|
|
end
|
|
|
|
|
|
|
|
transcript.build_slack_ui
|
2017-08-03 19:47:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_interactive(json)
|
2021-04-22 13:50:11 -04:00
|
|
|
Scheduler::Defer.later "Processing slack transcript update" do
|
2021-07-13 15:36:16 -04:00
|
|
|
http = DiscourseChatIntegration::Provider::SlackProvider.slack_api_http
|
2021-04-22 13:50:11 -04:00
|
|
|
|
|
|
|
if json[:type] == "block_actions" && json[:actions][0][:action_id] == "null_action"
|
|
|
|
# Do nothing
|
|
|
|
elsif json[:type] == "message_action" && json[:message][:thread_ts]
|
|
|
|
# Context menu used on a threaded message
|
2022-12-29 07:31:05 -05:00
|
|
|
transcript =
|
|
|
|
SlackTranscript.new(
|
|
|
|
channel_name: "##{json[:channel][:name]}",
|
|
|
|
channel_id: json[:channel][:id],
|
|
|
|
requested_thread_ts: json[:message][:thread_ts],
|
|
|
|
)
|
2021-04-22 13:50:11 -04:00
|
|
|
|
|
|
|
# Send a loading modal within 3 seconds:
|
2022-12-29 07:31:05 -05:00
|
|
|
req =
|
|
|
|
Net::HTTP::Post.new(
|
|
|
|
"https://slack.com/api/views.open",
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"Authorization" => "Bearer #{SiteSetting.chat_integration_slack_access_token}",
|
|
|
|
)
|
|
|
|
req.body = { trigger_id: json[:trigger_id], view: transcript.build_modal_ui }.to_json
|
2021-04-22 13:50:11 -04:00
|
|
|
response = http.request(req)
|
|
|
|
view_id = JSON.parse(response.body).dig("view", "id")
|
|
|
|
|
|
|
|
# Now load the transcript
|
|
|
|
error_view = generate_error_view("users") unless transcript.load_user_data
|
|
|
|
error_view = generate_error_view("history") unless transcript.load_chat_history
|
|
|
|
|
|
|
|
# Then update the modal with the transcript link:
|
2022-12-29 07:31:05 -05:00
|
|
|
req =
|
|
|
|
Net::HTTP::Post.new(
|
|
|
|
"https://slack.com/api/views.update",
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"Authorization" => "Bearer #{SiteSetting.chat_integration_slack_access_token}",
|
|
|
|
)
|
|
|
|
req.body = { view_id: view_id, view: error_view || transcript.build_modal_ui }.to_json
|
2021-04-22 13:50:11 -04:00
|
|
|
response = http.request(req)
|
|
|
|
else
|
|
|
|
# Button clicked in one of our interactive messages
|
2022-12-29 07:31:05 -05:00
|
|
|
req = Net::HTTP::Post.new(URI(json[:response_url]), "Content-Type" => "application/json")
|
2021-04-22 13:50:11 -04:00
|
|
|
req.body = build_interactive_response(json).to_json
|
|
|
|
response = http.request(req)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2021-04-22 13:50:11 -04:00
|
|
|
def build_interactive_response(json)
|
|
|
|
requested_thread = first_message = last_message = nil
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2021-04-22 13:50:11 -04:00
|
|
|
if json[:type] == "message_action" # Slack "Shortcut" (for non-threaded messages)
|
|
|
|
first_message = json[:message][:ts]
|
|
|
|
else # Clicking buttons in our transcript UI message
|
|
|
|
action_name = json[:actions][0][:name]
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2021-04-22 13:50:11 -04:00
|
|
|
constant_val = json[:callback_id]
|
|
|
|
changed_val = json[:actions][0][:selected_options][0][:value]
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
first_message = (action_name == "first_message") ? changed_val : constant_val
|
|
|
|
last_message = (action_name == "first_message") ? constant_val : changed_val
|
2021-04-22 13:50:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
error_key = "chat_integration.provider.slack.transcript.error"
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
unless transcript =
|
|
|
|
SlackTranscript.new(
|
|
|
|
channel_name: "##{json[:channel][:name]}",
|
|
|
|
channel_id: json[:channel][:id],
|
|
|
|
requested_thread_ts: requested_thread,
|
|
|
|
)
|
|
|
|
return { text: I18n.t(error_key) }
|
|
|
|
end
|
2021-04-22 13:50:11 -04:00
|
|
|
return { text: I18n.t("#{error_key}_users") } unless transcript.load_user_data
|
|
|
|
return { text: I18n.t("#{error_key}_history") } unless transcript.load_chat_history
|
|
|
|
|
|
|
|
if first_message
|
2022-12-29 07:31:05 -05:00
|
|
|
unless transcript.set_first_message_by_ts(first_message)
|
|
|
|
return { text: I18n.t("#{error_key}_ts") }
|
|
|
|
end
|
2021-04-22 13:50:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if last_message
|
2022-12-29 07:31:05 -05:00
|
|
|
unless transcript.set_last_message_by_ts(last_message)
|
|
|
|
return { text: I18n.t("#{error_key}_ts") }
|
|
|
|
end
|
2017-09-15 18:12:02 -04:00
|
|
|
end
|
2021-04-22 13:50:11 -04:00
|
|
|
|
|
|
|
transcript.build_slack_ui
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_error_view(type = nil)
|
|
|
|
error_key = "chat_integration.provider.slack.transcript.error"
|
|
|
|
error_key += "_#{type}" if type
|
|
|
|
|
|
|
|
{
|
|
|
|
type: "modal",
|
|
|
|
title: {
|
|
|
|
type: "plain_text",
|
2022-12-29 07:31:05 -05:00
|
|
|
text: I18n.t("chat_integration.provider.slack.transcript.modal_title"),
|
2021-04-22 13:50:11 -04:00
|
|
|
},
|
|
|
|
blocks: [
|
2022-12-29 07:31:05 -05:00
|
|
|
{ type: "section", text: { type: "mrkdwn", text: ":warning: *#{I18n.t(error_key)}*" } },
|
|
|
|
],
|
2021-04-22 13:50:11 -04:00
|
|
|
}
|
2017-08-03 19:47:04 -04:00
|
|
|
end
|
|
|
|
|
2017-07-05 18:01:46 -04:00
|
|
|
def slack_token_valid?
|
|
|
|
params.require(:token)
|
|
|
|
|
|
|
|
if SiteSetting.chat_integration_slack_incoming_webhook_token.blank? ||
|
2022-12-29 07:31:05 -05:00
|
|
|
SiteSetting.chat_integration_slack_incoming_webhook_token != params[:token]
|
2017-07-05 18:01:46 -04:00
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
|
|
|
def slack_payload_token_valid?
|
|
|
|
params.require(:payload)
|
|
|
|
|
|
|
|
json = JSON.parse(params[:payload], symbolize_names: true)
|
|
|
|
|
|
|
|
if SiteSetting.chat_integration_slack_incoming_webhook_token.blank? ||
|
2022-12-29 07:31:05 -05:00
|
|
|
SiteSetting.chat_integration_slack_incoming_webhook_token != json[:token]
|
2017-08-03 19:47:04 -04:00
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
end
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class SlackEngine < ::Rails::Engine
|
2021-07-13 15:36:16 -04:00
|
|
|
engine_name DiscourseChatIntegration::PLUGIN_NAME + "-slack"
|
|
|
|
isolate_namespace DiscourseChatIntegration::Provider::SlackProvider
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
SlackEngine.routes.draw do
|
2017-07-05 18:01:46 -04:00
|
|
|
post "command" => "slack_command#command"
|
2017-08-03 19:47:04 -04:00
|
|
|
post "interactive" => "slack_command#interactive"
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
end
|