-
Notifications
You must be signed in to change notification settings - Fork 77
/
plugin.rb
143 lines (115 loc) · 5.19 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# frozen_string_literal: true
# name: discourse-chat-integration
# about: Allows integration with several external chat system providers
# meta_topic_id: 66522
# version: 0.1
# url: https://github.com/discourse/discourse-chat-integration
# author: David Taylor
enabled_site_setting :chat_integration_enabled
register_asset "stylesheets/chat-integration.scss"
register_svg_icon "rocket" if respond_to?(:register_svg_icon)
register_svg_icon "arrow-circle-o-right" if respond_to?(:register_svg_icon)
# Site setting validators must be loaded before initialize
require_relative "lib/discourse_chat_integration/provider/slack/slack_enabled_setting_validator"
require_relative "lib/discourse_chat_integration/chat_integration_reference_post"
after_initialize do
require_relative "app/initializers/discourse_chat_integration"
require_relative "app/services/problem_check/channel_errors"
register_problem_check ProblemCheck::ChannelErrors
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)
enabled =
is_enabled_setting ? new_value == true : SiteSetting.chat_integration_telegram_enabled
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
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)
end
add_admin_route "chat_integration.menu_title", "chat-integration"
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 topic_tags_changed]
script do |context, fields, automation|
channel_name = fields.dig("channel", "value")
channel =
DiscourseChatIntegration::Channel.new(
provider: "slack",
data: {
identifier: "##{channel_name}",
},
)
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
end
end
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
version 1
triggerables %i[topic_tags_changed]
script do |context, fields, automation|
# 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?
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)
rescue StandardError => e
Rails.logger.warn "[discourse-automation] Error while sending chat integration message. Automation ID: #{automation.id}"
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}"
end
end
end
end
end