2020-12-11 04:43:03 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration::Provider::WebexProvider
|
2020-12-11 04:43:03 -05:00
|
|
|
PROVIDER_NAME = "webex".freeze
|
|
|
|
PROVIDER_ENABLED_SETTING = :chat_integration_webex_enabled
|
|
|
|
CHANNEL_PARAMETERS = [
|
|
|
|
{ key: "name", regex: '^\S+$', unique: true },
|
2022-12-29 07:31:05 -05:00
|
|
|
{
|
|
|
|
key: "webhook_url",
|
2020-12-11 04:43:03 -05:00
|
|
|
regex: '^https:\/\/webexapis\.com\/v1\/webhooks\/incoming\/[A-Za-z0-9\-@\/]+\S+$',
|
|
|
|
unique: true,
|
2022-12-29 07:31:05 -05:00
|
|
|
hidden: true,
|
|
|
|
},
|
2020-12-11 04:43:03 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
def self.trigger_notification(post, channel, rule)
|
|
|
|
message = get_message(post)
|
2022-12-29 07:31:05 -05:00
|
|
|
uri = URI(channel.data["webhook_url"])
|
2020-12-11 04:43:03 -05:00
|
|
|
|
2022-11-01 13:36:56 -04:00
|
|
|
http = FinalDestination::HTTP.new(uri.host, uri.port)
|
2022-12-29 07:31:05 -05:00
|
|
|
http.use_ssl = (uri.scheme == "https")
|
2020-12-11 04:43:03 -05:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
|
2020-12-11 04:43:03 -05:00
|
|
|
req.body = message.to_json
|
|
|
|
response = http.request(req)
|
|
|
|
|
|
|
|
unless response.kind_of? Net::HTTPSuccess
|
2022-12-29 07:31:05 -05:00
|
|
|
if response.body.include?("Invalid webhook URL")
|
|
|
|
error_key = "chat_integration.provider.webex.errors.invalid_channel"
|
2020-12-11 04:43:03 -05:00
|
|
|
else
|
|
|
|
error_key = nil
|
|
|
|
end
|
2022-12-29 07:31:05 -05:00
|
|
|
raise ::DiscourseChatIntegration::ProviderError.new info: {
|
|
|
|
error_key: error_key,
|
|
|
|
request: req.body,
|
|
|
|
response_code: response.code,
|
|
|
|
response_body: response.body,
|
|
|
|
}
|
2020-12-11 04:43:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_message(post)
|
2022-05-30 12:13:55 -04:00
|
|
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
2020-12-11 04:43:03 -05:00
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
category = ""
|
2020-12-11 04:43:03 -05:00
|
|
|
if topic.category&.uncategorized?
|
2022-12-29 07:31:05 -05:00
|
|
|
category = "[#{I18n.t("uncategorized_category_name")}]"
|
2020-12-11 04:43:03 -05:00
|
|
|
elsif topic.category
|
2022-12-29 07:31:05 -05:00
|
|
|
category =
|
|
|
|
(
|
|
|
|
if (topic.category.parent_category)
|
|
|
|
"[#{topic.category.parent_category.name}/#{topic.category.name}]"
|
|
|
|
else
|
|
|
|
"[#{topic.category.name}]"
|
|
|
|
end
|
|
|
|
)
|
2020-12-11 04:43:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
markdown = "**#{topic.title}**: #{category}"
|
2022-12-29 07:31:05 -05:00
|
|
|
markdown += " #{topic.tags.map(&:name).join(", ")} " if topic.tags.present?
|
2022-05-30 12:13:55 -04:00
|
|
|
markdown += "(#{post.full_url}) from #{display_name}:\n"
|
2022-12-29 07:31:05 -05:00
|
|
|
markdown +=
|
|
|
|
post.excerpt(
|
|
|
|
SiteSetting.chat_integration_webex_excerpt_length,
|
|
|
|
text_entities: true,
|
|
|
|
strip_links: true,
|
|
|
|
remap_emoji: true,
|
|
|
|
)
|
2020-12-11 04:43:03 -05:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
{ markdown: markdown }
|
2020-12-11 04:43:03 -05:00
|
|
|
end
|
|
|
|
end
|