2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-03 06:08:14 -04:00
|
|
|
# name: discourse-chat-integration
|
2023-11-27 07:04:54 -05:00
|
|
|
# about: Allows integration with several external chat system providers
|
2023-11-08 14:27:54 -05:00
|
|
|
# meta_topic_id: 66522
|
2017-06-26 10:08:06 -04:00
|
|
|
# version: 0.1
|
2017-07-03 06:08:14 -04:00
|
|
|
# url: https://github.com/discourse/discourse-chat-integration
|
2017-07-13 08:32:11 -04:00
|
|
|
# author: David Taylor
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2017-07-03 06:08:14 -04:00
|
|
|
enabled_site_setting :chat_integration_enabled
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2023-12-11 14:00:34 -05:00
|
|
|
register_asset "stylesheets/chat-integration.scss"
|
2017-07-03 17:11:17 -04:00
|
|
|
|
2018-11-01 18:03:39 -04:00
|
|
|
register_svg_icon "rocket" if respond_to?(:register_svg_icon)
|
2020-06-15 11:45:25 -04:00
|
|
|
register_svg_icon "fa-arrow-circle-o-right" if respond_to?(:register_svg_icon)
|
2018-11-01 18:03:39 -04:00
|
|
|
|
2017-07-03 11:28:26 -04:00
|
|
|
# Site setting validators must be loaded before initialize
|
2021-07-13 15:36:16 -04:00
|
|
|
require_relative "lib/discourse_chat_integration/provider/slack/slack_enabled_setting_validator"
|
2024-09-11 09:42:52 -04:00
|
|
|
require_relative "lib/discourse_chat_integration/chat_integration_reference_post"
|
2017-06-29 12:50:54 -04:00
|
|
|
|
2017-06-26 10:08:06 -04:00
|
|
|
after_initialize do
|
2021-07-13 15:36:16 -04:00
|
|
|
require_relative "app/initializers/discourse_chat_integration"
|
2024-04-03 16:51:19 -04:00
|
|
|
|
2024-03-15 01:21:11 -04:00
|
|
|
require_relative "app/services/problem_check/channel_errors"
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2024-04-03 16:51:19 -04:00
|
|
|
register_problem_check ProblemCheck::ChannelErrors
|
|
|
|
|
2024-03-04 10:01:07 -05:00
|
|
|
on(:site_setting_changed) do |setting_name, old_value, new_value|
|
|
|
|
is_enabled_setting = setting_name == :chat_integration_telegram_enabled
|
|
|
|
is_access_token = setting_name == :chat_integration_telegram_access_token
|
|
|
|
|
|
|
|
if (is_enabled_setting || is_access_token)
|
2024-03-04 10:34:01 -05:00
|
|
|
enabled =
|
|
|
|
is_enabled_setting ? new_value == true : SiteSetting.chat_integration_telegram_enabled
|
2024-03-04 10:01:07 -05:00
|
|
|
|
|
|
|
if enabled && SiteSetting.chat_integration_telegram_access_token.present?
|
|
|
|
Scheduler::Defer.later("Setup Telegram Webhook") do
|
|
|
|
DiscourseChatIntegration::Provider::TelegramProvider.setup_webhook
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-09 22:08:26 -04:00
|
|
|
on(:post_created) do |post|
|
|
|
|
# This will run for every post, even PMs. Don't worry, they're filtered out later.
|
|
|
|
time = SiteSetting.chat_integration_delay_seconds.seconds
|
|
|
|
Jobs.enqueue_in(time, :notify_chats, post_id: post.id)
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
add_admin_route "chat_integration.menu_title", "chat-integration"
|
2017-06-26 14:19:50 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Provider.mount_engines
|
2022-02-25 14:17:20 -05:00
|
|
|
|
|
|
|
if defined?(DiscourseAutomation)
|
2022-12-29 07:31:05 -05:00
|
|
|
add_automation_scriptable("send_slack_message") do
|
2022-02-25 14:17:20 -05:00
|
|
|
field :message, component: :message, required: true, accepts_placeholders: true
|
|
|
|
field :url, component: :text, required: true
|
|
|
|
field :channel, component: :text, required: true
|
|
|
|
|
|
|
|
version 1
|
|
|
|
|
2024-08-13 14:14:35 -04:00
|
|
|
triggerables %i[point_in_time recurring topic_tags_changed]
|
2022-02-25 14:17:20 -05:00
|
|
|
|
|
|
|
script do |context, fields, automation|
|
2022-12-29 07:31:05 -05:00
|
|
|
channel_name = fields.dig("channel", "value")
|
|
|
|
channel =
|
|
|
|
DiscourseChatIntegration::Channel.new(
|
|
|
|
provider: "slack",
|
|
|
|
data: {
|
|
|
|
identifier: "##{channel_name}",
|
|
|
|
},
|
|
|
|
)
|
2022-02-25 14:17:20 -05:00
|
|
|
|
2024-08-13 14:14:35 -04:00
|
|
|
begin
|
|
|
|
message =
|
|
|
|
DiscourseChatIntegration::Provider::SlackProvider.create_slack_message(
|
|
|
|
context: context,
|
|
|
|
content: fields.dig("message", "value"),
|
|
|
|
url: fields.dig("url", "value"),
|
|
|
|
channel_name: channel_name,
|
|
|
|
)
|
|
|
|
DiscourseChatIntegration::Provider::SlackProvider.send_via_api(nil, channel, message)
|
|
|
|
rescue StandardError => _
|
|
|
|
# StandardError here is when there are no tags but content includes reference to them.
|
|
|
|
end
|
2022-02-25 14:17:20 -05:00
|
|
|
end
|
|
|
|
end
|
2024-09-11 09:42:52 -04:00
|
|
|
|
|
|
|
add_automation_scriptable("send_chat_integration_message") do
|
|
|
|
field :provider,
|
|
|
|
component: :choices,
|
|
|
|
extra: {
|
|
|
|
content:
|
|
|
|
DiscourseChatIntegration::Provider.enabled_provider_names.map do |provider|
|
|
|
|
{ id: provider, name: "chat_integration.provider.#{provider}.title" }
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
required: true
|
|
|
|
field :channel_name, component: :text, required: true
|
2024-10-08 08:22:33 -04:00
|
|
|
field :trigger_with_pms, component: :boolean
|
2024-09-11 09:42:52 -04:00
|
|
|
|
|
|
|
version 1
|
|
|
|
|
|
|
|
triggerables %i[topic_tags_changed]
|
|
|
|
|
|
|
|
script do |context, fields, automation|
|
2024-10-08 08:22:33 -04:00
|
|
|
if context["topic"].private_message?
|
|
|
|
next unless fields.dig("trigger_with_pms", "value")
|
|
|
|
end
|
2024-10-07 15:49:01 -04:00
|
|
|
|
2024-10-07 15:07:35 -04:00
|
|
|
# DiscourseTagging.tag_topic_by_names runs on topic creation and on tags change
|
|
|
|
# we only want to send a message when tags change
|
|
|
|
next if context["topic"].new_record?
|
|
|
|
|
2024-09-11 09:42:52 -04:00
|
|
|
provider = fields.dig("provider", "value")
|
|
|
|
channel_name = fields.dig("channel_name", "value")
|
|
|
|
|
|
|
|
post =
|
|
|
|
DiscourseChatIntegration::ChatIntegrationReferencePost.new(
|
|
|
|
user: context["user"],
|
|
|
|
topic: context["topic"],
|
|
|
|
kind: context["kind"],
|
|
|
|
context: {
|
|
|
|
"added_tags" => context["added_tags"],
|
|
|
|
"removed_tags" => context["removed_tags"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
provider = DiscourseChatIntegration::Provider.get_by_name(provider)
|
|
|
|
|
|
|
|
channel = provider.get_channel_by_name(channel_name) # user must have created a channel in /admin/plugins/chat-integration/<provider> page
|
|
|
|
|
|
|
|
if channel.nil?
|
|
|
|
Rails.logger.warn "[discourse-automation] Channel not found. Automation ID: #{automation.id}"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
provider.trigger_notification(post, channel, nil)
|
2024-09-12 09:44:39 -04:00
|
|
|
rescue StandardError => e
|
2024-09-11 09:42:52 -04:00
|
|
|
Rails.logger.warn "[discourse-automation] Error while sending chat integration message. Automation ID: #{automation.id}"
|
2024-09-12 09:44:39 -04:00
|
|
|
Rails.logger.warn "[discourse-chat-integration] Error: #{e.inspect}"
|
|
|
|
Rails.logger.warn "[discourse-chat-integration] Channel: #{channel_name}"
|
|
|
|
Rails.logger.warn "[discourse-chat-integration] Reference post: #{post.inspect}"
|
2024-09-11 09:42:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-02-25 14:17:20 -05:00
|
|
|
end
|
2017-06-26 10:08:06 -04:00
|
|
|
end
|