DEV: Adds post_to_slack scriptable for automation (#108)

This allows for the discourse automation plugin to have a "Send Slack
Message" script.

The script fields are a message, url, and slack channel. This will allow
for a custom slack message to be posted but can link back to an
arbitrary url (hopefully a discourse url) like a list of unanswered
topics instead of strictly only allowing a slack message that links back
to a Discourse Post object.
This commit is contained in:
Blake Erickson 2022-02-25 12:17:20 -07:00 committed by GitHub
parent 1cd745010a
commit 0eebd9f3ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 6 deletions

View File

@ -236,3 +236,14 @@ en:
webhook_url:
title: "Webhook URL"
help: "The URL provided when you create a new webhook"
discourse_automation:
scriptables:
send_slack_message:
title: Send Slack message
fields:
message:
label: Message
url:
label: URL
channel:
label: Channel

View File

@ -104,6 +104,10 @@ en:
chat_integration_google_enabled: "Enable the 'Google Chat' chat integration provider"
chat_integration_google_excerpt_length: "Google Chat post excerpt length"
discourse_automation:
scriptables:
send_slack_message:
title: Send Slack message
chat_integration:
all_categories: "(all categories)"

View File

@ -106,12 +106,15 @@ module DiscourseChatIntegration::Provider::SlackProvider
channel: message[:channel].gsub('#', ''),
attachments: message[:attachments].to_json
}
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
end
req.set_form_data(data)
@ -133,7 +136,7 @@ module DiscourseChatIntegration::Provider::SlackProvider
end
ts = json["ts"]
set_slack_thread_ts(post.topic, channel, ts) if !ts.nil?
set_slack_thread_ts(post.topic, channel, ts) if !ts.nil? && !post.nil?
response
end

View File

@ -40,4 +40,62 @@ after_initialize do
end
DiscourseChatIntegration::Provider.mount_engines
if defined?(DiscourseAutomation)
add_automation_scriptable('send_slack_message') do
field :message, component: :message, required: true, accepts_placeholders: true
field :url, component: :text, required: true
field :channel, component: :text, required: true
version 1
triggerables %i[point_in_time recurring]
script do |context, fields, automation|
sender = Discourse.system_user
content = fields.dig('message', 'value')
url = fields.dig('url', 'value')
full_content = "#{content} - #{url}"
channel_name = fields.dig('channel', 'value')
channel = DiscourseChatIntegration::Channel.new(provider: "slack", data: { identifier: "##{channel_name}" })
icon_url =
if SiteSetting.chat_integration_slack_icon_url.present?
"#{Discourse.base_url}#{SiteSetting.chat_integration_slack_icon_url}"
elsif (url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)).present?
"#{Discourse.base_url}#{url}"
end
slack_username =
if SiteSetting.chat_integration_slack_username.present?
SiteSetting.chat_integration_slack_username
else
SiteSetting.title || "Discourse"
end
message = {
channel: "##{channel_name}",
username: slack_username,
icon_url: icon_url,
attachments: []
}
summary = {
fallback: content.truncate(100),
author_name: sender,
color: nil,
text: full_content,
mrkdwn_in: ["text"],
title: content.truncate(100),
title_link: url,
thumb_url: nil
}
message[:attachments].push(summary)
DiscourseChatIntegration::Provider::SlackProvider.send_via_api(nil, channel, message)
end
end
end
end