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:
parent
1cd745010a
commit
0eebd9f3ed
|
@ -236,3 +236,14 @@ en:
|
||||||
webhook_url:
|
webhook_url:
|
||||||
title: "Webhook URL"
|
title: "Webhook URL"
|
||||||
help: "The URL provided when you create a new webhook"
|
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
|
||||||
|
|
|
@ -104,6 +104,10 @@ en:
|
||||||
chat_integration_google_enabled: "Enable the 'Google Chat' chat integration provider"
|
chat_integration_google_enabled: "Enable the 'Google Chat' chat integration provider"
|
||||||
chat_integration_google_excerpt_length: "Google Chat post excerpt length"
|
chat_integration_google_excerpt_length: "Google Chat post excerpt length"
|
||||||
|
|
||||||
|
discourse_automation:
|
||||||
|
scriptables:
|
||||||
|
send_slack_message:
|
||||||
|
title: Send Slack message
|
||||||
chat_integration:
|
chat_integration:
|
||||||
|
|
||||||
all_categories: "(all categories)"
|
all_categories: "(all categories)"
|
||||||
|
|
|
@ -106,12 +106,15 @@ module DiscourseChatIntegration::Provider::SlackProvider
|
||||||
channel: message[:channel].gsub('#', ''),
|
channel: message[:channel].gsub('#', ''),
|
||||||
attachments: message[:attachments].to_json
|
attachments: message[:attachments].to_json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if post
|
||||||
if message.key?(:thread_ts)
|
if message.key?(:thread_ts)
|
||||||
data[:thread_ts] = message[:thread_ts]
|
data[:thread_ts] = message[:thread_ts]
|
||||||
elsif (match = slack_thread_regex.match(post.raw)) && match.captures[0] == channel
|
elsif (match = slack_thread_regex.match(post.raw)) && match.captures[0] == channel
|
||||||
data[:thread_ts] = match.captures[1]
|
data[:thread_ts] = match.captures[1]
|
||||||
set_slack_thread_ts(post.topic, channel, match.captures[1])
|
set_slack_thread_ts(post.topic, channel, match.captures[1])
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
req.set_form_data(data)
|
req.set_form_data(data)
|
||||||
|
|
||||||
|
@ -133,7 +136,7 @@ module DiscourseChatIntegration::Provider::SlackProvider
|
||||||
end
|
end
|
||||||
|
|
||||||
ts = json["ts"]
|
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
|
response
|
||||||
end
|
end
|
||||||
|
|
58
plugin.rb
58
plugin.rb
|
@ -40,4 +40,62 @@ after_initialize do
|
||||||
end
|
end
|
||||||
|
|
||||||
DiscourseChatIntegration::Provider.mount_engines
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue