2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration::Provider::SlackProvider
|
2021-12-02 09:29:06 -05:00
|
|
|
PROVIDER_NAME = "slack"
|
|
|
|
THREAD_CUSTOM_FIELD_PREFIX = "slack_thread_id_"
|
|
|
|
|
|
|
|
# In the past, only one thread_ts was stored for each topic.
|
|
|
|
# Now, we store one thread_ts per Slack channel.
|
|
|
|
# Data will be automatically migrated when the next message is sent to the channel
|
|
|
|
# This logic could be removed after 2022-12 - it's unlikely people will care about
|
|
|
|
# threading messages to more-than-1-year-old Slack threads.
|
|
|
|
THREAD_LEGACY = "thread"
|
2017-06-30 08:09:36 -04:00
|
|
|
|
2017-07-03 10:53:26 -04:00
|
|
|
PROVIDER_ENABLED_SETTING = :chat_integration_slack_enabled
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
CHANNEL_PARAMETERS = [{ key: "identifier", regex: '^[@#]?\S*$', unique: true }]
|
2017-07-03 19:14:01 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
require_dependency "topic"
|
|
|
|
::Topic.register_custom_field_type(
|
|
|
|
DiscourseChatIntegration::Provider::SlackProvider::THREAD_LEGACY,
|
|
|
|
:string,
|
|
|
|
)
|
2020-06-15 11:45:25 -04:00
|
|
|
|
2017-07-03 10:53:26 -04:00
|
|
|
def self.excerpt(post, max_length = SiteSetting.chat_integration_slack_excerpt_length)
|
2022-12-29 07:31:05 -05:00
|
|
|
doc =
|
|
|
|
Nokogiri::HTML5.fragment(
|
|
|
|
post.excerpt(max_length, remap_emoji: true, keep_onebox_source: true),
|
|
|
|
)
|
2017-06-30 08:09:36 -04:00
|
|
|
|
|
|
|
SlackMessageFormatter.format(doc.to_html)
|
|
|
|
end
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
def self.slack_message(post, channel, filter)
|
2022-05-30 12:13:55 -04:00
|
|
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
2017-06-30 08:09:36 -04:00
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
category = ""
|
2019-06-04 13:47:32 -04:00
|
|
|
if topic.category&.uncategorized?
|
2022-12-29 07:31:05 -05:00
|
|
|
category = "[#{I18n.t("uncategorized_category_name")}]"
|
2019-06-04 13:47:32 -04:00
|
|
|
elsif topic.category
|
2022-12-29 07:31:05 -05:00
|
|
|
category =
|
|
|
|
(
|
|
|
|
if (topic.category.parent_category)
|
|
|
|
"[#{topic.category.parent_category.name}/#{topic.category.name}]"
|
|
|
|
else
|
|
|
|
"[#{topic.category.name}]"
|
|
|
|
end
|
|
|
|
)
|
2017-07-04 18:35:45 -04:00
|
|
|
end
|
2017-06-30 08:09:36 -04:00
|
|
|
|
|
|
|
icon_url =
|
2018-11-14 02:17:44 -05:00
|
|
|
if SiteSetting.chat_integration_slack_icon_url.present?
|
2017-07-03 06:08:14 -04:00
|
|
|
"#{Discourse.base_url}#{SiteSetting.chat_integration_slack_icon_url}"
|
2018-11-14 02:17:44 -05:00
|
|
|
elsif (url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)).present?
|
|
|
|
"#{Discourse.base_url}#{url}"
|
2017-06-30 08:09:36 -04:00
|
|
|
end
|
|
|
|
|
2019-09-11 10:08:40 -04:00
|
|
|
slack_username =
|
|
|
|
if SiteSetting.chat_integration_slack_username.present?
|
|
|
|
SiteSetting.chat_integration_slack_username
|
|
|
|
else
|
|
|
|
SiteSetting.title || "Discourse"
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
message = { channel: channel, username: slack_username, icon_url: icon_url, attachments: [] }
|
2017-06-30 08:09:36 -04:00
|
|
|
|
2021-12-02 09:29:06 -05:00
|
|
|
if filter == "thread" && thread_ts = get_slack_thread_ts(topic, channel)
|
|
|
|
message[:thread_ts] = thread_ts
|
2020-06-15 11:45:25 -04:00
|
|
|
end
|
|
|
|
|
2017-06-30 08:09:36 -04:00
|
|
|
summary = {
|
|
|
|
fallback: "#{topic.title} - #{display_name}",
|
|
|
|
author_name: display_name,
|
|
|
|
author_icon: post.user.small_avatar_url,
|
2017-07-04 18:35:45 -04:00
|
|
|
color: topic.category ? "##{topic.category.color}" : nil,
|
2017-07-12 06:58:19 -04:00
|
|
|
text: excerpt(post),
|
2017-08-21 10:28:37 -04:00
|
|
|
mrkdwn_in: ["text"],
|
2022-12-29 07:31:05 -05:00
|
|
|
title:
|
|
|
|
"#{topic.title} #{category} #{topic.tags.present? ? topic.tags.map(&:name).join(", ") : ""}",
|
2017-09-09 18:08:45 -04:00
|
|
|
title_link: post.full_url,
|
2022-12-29 07:31:05 -05:00
|
|
|
thumb_url: post.full_url,
|
2017-06-30 08:09:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
message[:attachments].push(summary)
|
2017-08-21 10:28:37 -04:00
|
|
|
|
2019-11-14 15:03:49 -05:00
|
|
|
message
|
2017-06-30 08:09:36 -04:00
|
|
|
end
|
|
|
|
|
2017-06-30 11:39:19 -04:00
|
|
|
def self.send_via_api(post, channel, message)
|
2021-04-22 13:50:11 -04:00
|
|
|
http = slack_api_http
|
2017-08-01 15:53:39 -04:00
|
|
|
|
|
|
|
response = nil
|
2017-06-30 10:28:20 -04:00
|
|
|
uri = ""
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
# <!--SLACK_CHANNEL_ID=#{@channel_id};SLACK_TS=#{@requested_thread_ts}-->
|
2021-12-02 09:29:06 -05:00
|
|
|
slack_thread_regex = /<!--SLACK_CHANNEL_ID=([^;.]+);SLACK_TS=([0-9]{10}.[0-9]{6})-->/
|
2017-06-30 10:28:20 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
req = Net::HTTP::Post.new(URI("https://slack.com/api/chat.postMessage"))
|
2017-08-21 10:28:37 -04:00
|
|
|
|
|
|
|
data = {
|
|
|
|
token: SiteSetting.chat_integration_slack_access_token,
|
|
|
|
username: message[:username],
|
|
|
|
icon_url: message[:icon_url],
|
2022-12-29 07:31:05 -05:00
|
|
|
channel: message[:channel].gsub("#", ""),
|
|
|
|
attachments: message[:attachments].to_json,
|
2017-08-21 10:28:37 -04:00
|
|
|
}
|
2022-02-25 14:17:20 -05:00
|
|
|
|
|
|
|
if post
|
|
|
|
if message.key?(:thread_ts)
|
|
|
|
data[:thread_ts] = message[:thread_ts]
|
|
|
|
elsif (match = slack_thread_regex.match(post.raw)) && match.captures[0] == channel
|
|
|
|
data[:thread_ts] = match.captures[1]
|
|
|
|
set_slack_thread_ts(post.topic, channel, match.captures[1])
|
|
|
|
end
|
2020-06-15 11:45:25 -04:00
|
|
|
end
|
2017-08-21 10:28:37 -04:00
|
|
|
|
|
|
|
req.set_form_data(data)
|
|
|
|
|
|
|
|
response = http.request(req)
|
2017-06-30 10:28:20 -04:00
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
unless response.kind_of? Net::HTTPSuccess
|
2022-12-29 07:31:05 -05:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: {
|
|
|
|
request: uri,
|
|
|
|
response_code: response.code,
|
|
|
|
response_body: response.body,
|
|
|
|
}
|
2017-07-07 06:23:25 -04:00
|
|
|
end
|
|
|
|
|
2020-09-25 15:08:28 -04:00
|
|
|
json = JSON.parse(response.body)
|
2017-07-07 06:23:25 -04:00
|
|
|
|
|
|
|
unless json["ok"] == true
|
2022-12-29 07:31:05 -05:00
|
|
|
if json.key?("error") &&
|
|
|
|
(json["error"] == ("channel_not_found") || json["error"] == ("is_archived"))
|
|
|
|
error_key = "chat_integration.provider.slack.errors.channel_not_found"
|
2017-07-07 06:23:25 -04:00
|
|
|
else
|
2017-08-21 10:28:37 -04:00
|
|
|
error_key = nil
|
2017-07-07 06:23:25 -04:00
|
|
|
end
|
2022-12-29 07:31:05 -05:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: {
|
|
|
|
error_key: error_key,
|
|
|
|
request: uri,
|
|
|
|
response_code: response.code,
|
|
|
|
response_body: response.body,
|
|
|
|
}
|
2017-07-07 06:23:25 -04:00
|
|
|
end
|
|
|
|
|
2023-01-26 07:18:48 -05:00
|
|
|
ts = json.dig("message", "thread_ts") || json["ts"]
|
2022-02-25 14:17:20 -05:00
|
|
|
set_slack_thread_ts(post.topic, channel, ts) if !ts.nil? && !post.nil?
|
2020-06-15 11:45:25 -04:00
|
|
|
|
2017-06-30 11:39:19 -04:00
|
|
|
response
|
2017-06-30 10:28:20 -04:00
|
|
|
end
|
2017-06-30 08:09:36 -04:00
|
|
|
|
2017-06-30 10:28:20 -04:00
|
|
|
def self.send_via_webhook(message)
|
2022-11-01 13:36:56 -04:00
|
|
|
http = FinalDestination::HTTP.new("hooks.slack.com", 443)
|
2017-06-30 10:28:20 -04:00
|
|
|
http.use_ssl = true
|
2022-12-29 07:31:05 -05:00
|
|
|
req =
|
|
|
|
Net::HTTP::Post.new(
|
|
|
|
URI(SiteSetting.chat_integration_slack_outbound_webhook_url),
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
)
|
2017-06-30 08:09:36 -04:00
|
|
|
req.body = message.to_json
|
|
|
|
response = http.request(req)
|
2017-07-04 14:37:56 -04:00
|
|
|
|
|
|
|
unless response.kind_of? Net::HTTPSuccess
|
2022-12-29 07:31:05 -05:00
|
|
|
if response.code.to_s == "403"
|
|
|
|
error_key = "chat_integration.provider.slack.errors.action_prohibited"
|
|
|
|
elsif response.body == ("channel_not_found") || response.body == ("channel_is_archived")
|
|
|
|
error_key = "chat_integration.provider.slack.errors.channel_not_found"
|
2017-07-04 14:37:56 -04:00
|
|
|
else
|
|
|
|
error_key = nil
|
|
|
|
end
|
2022-12-29 07:31:05 -05:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: {
|
|
|
|
error_key: error_key,
|
|
|
|
request: req.body,
|
|
|
|
response_code: response.code,
|
|
|
|
response_body: response.body,
|
|
|
|
}
|
2017-07-04 14:37:56 -04:00
|
|
|
end
|
2017-06-30 10:28:20 -04:00
|
|
|
end
|
|
|
|
|
2020-06-15 11:45:25 -04:00
|
|
|
def self.trigger_notification(post, channel, rule)
|
2022-12-29 07:31:05 -05:00
|
|
|
channel_id = channel.data["identifier"]
|
2020-06-15 11:45:25 -04:00
|
|
|
filter = rule.nil? ? "" : rule.filter
|
|
|
|
message = slack_message(post, channel_id, filter)
|
2017-06-30 10:28:20 -04:00
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
if SiteSetting.chat_integration_slack_access_token.empty?
|
|
|
|
self.send_via_webhook(message)
|
|
|
|
else
|
|
|
|
self.send_via_api(post, channel_id, message)
|
|
|
|
end
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
2021-04-22 13:50:11 -04:00
|
|
|
|
|
|
|
def self.slack_api_http
|
2022-11-01 13:36:56 -04:00
|
|
|
http = FinalDestination::HTTP.new("slack.com", 443)
|
2021-04-22 13:50:11 -04:00
|
|
|
http.use_ssl = true
|
|
|
|
http.read_timeout = 5 # seconds
|
|
|
|
http
|
|
|
|
end
|
2021-12-02 09:29:06 -05:00
|
|
|
|
|
|
|
def self.get_slack_thread_ts(topic, channel)
|
|
|
|
field = TopicCustomField.where(topic: topic, name: "#{THREAD_CUSTOM_FIELD_PREFIX}#{channel}")
|
|
|
|
field.pluck_first(:value) || topic.custom_fields[THREAD_LEGACY]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_slack_thread_ts(topic, channel, value)
|
2022-12-29 07:31:05 -05:00
|
|
|
TopicCustomField.upsert(
|
|
|
|
{
|
2021-12-02 09:29:06 -05:00
|
|
|
topic_id: topic.id,
|
|
|
|
name: "#{THREAD_CUSTOM_FIELD_PREFIX}#{channel}",
|
|
|
|
value: value,
|
|
|
|
created_at: Time.zone.now,
|
2022-12-29 07:31:05 -05:00
|
|
|
updated_at: Time.zone.now,
|
|
|
|
},
|
|
|
|
unique_by: %i[topic_id name],
|
|
|
|
)
|
2021-12-02 09:29:06 -05:00
|
|
|
end
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
|
2017-10-10 00:54:10 -04:00
|
|
|
require_relative "slack_message_formatter"
|
|
|
|
require_relative "slack_transcript"
|
|
|
|
require_relative "slack_message"
|
|
|
|
require_relative "slack_command_controller"
|