diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 923fa7b..565d0b7 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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 diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index a949d70..f272d92 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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)" diff --git a/lib/discourse_chat_integration/provider/slack/slack_provider.rb b/lib/discourse_chat_integration/provider/slack/slack_provider.rb index be129eb..f7be0d8 100644 --- a/lib/discourse_chat_integration/provider/slack/slack_provider.rb +++ b/lib/discourse_chat_integration/provider/slack/slack_provider.rb @@ -106,11 +106,14 @@ module DiscourseChatIntegration::Provider::SlackProvider channel: message[:channel].gsub('#', ''), attachments: message[:attachments].to_json } - 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]) + + 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 diff --git a/plugin.rb b/plugin.rb index fb4b2ba..b745c28 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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