FEATURE: Introduce Guilded as a provider (#135)
Support for integration with https://www.guilded.gg/
This commit is contained in:
parent
c68fde5d2b
commit
4e73da8856
|
@ -113,6 +113,19 @@ en:
|
||||||
title: Webhook URL
|
title: Webhook URL
|
||||||
help: The webhook URL created in your Discord server settings
|
help: The webhook URL created in your Discord server settings
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
########### GUILDED STRINGS ###########
|
||||||
|
#######################################
|
||||||
|
guilded:
|
||||||
|
title: "Guilded"
|
||||||
|
param:
|
||||||
|
name:
|
||||||
|
title: "Name"
|
||||||
|
help: "A name to describe the channel. It is not used for the connection to Guilded"
|
||||||
|
webhook_url:
|
||||||
|
title: Webhook URL
|
||||||
|
help: The webhook URL created in your Guilded server settings
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
######### MATTERMOST STRINGS ##########
|
######### MATTERMOST STRINGS ##########
|
||||||
#######################################
|
#######################################
|
||||||
|
|
|
@ -34,6 +34,12 @@ en:
|
||||||
chat_integration_discord_message_content: "The message to include above the summary when sending a notification to Discord"
|
chat_integration_discord_message_content: "The message to include above the summary when sending a notification to Discord"
|
||||||
chat_integration_discord_excerpt_length: "Discord post excerpt length"
|
chat_integration_discord_excerpt_length: "Discord post excerpt length"
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
########## GUILDED SETTINGS ###########
|
||||||
|
#######################################
|
||||||
|
chat_integration_guilded_enabled: "Enable the Guilded chat-integration provider"
|
||||||
|
chat_integration_guilded_excerpt_length: "Guilded post excerpt length"
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
######## MATTERMOST SETTINGS ##########
|
######## MATTERMOST SETTINGS ##########
|
||||||
#######################################
|
#######################################
|
||||||
|
|
|
@ -54,6 +54,15 @@ chat_integration:
|
||||||
chat_integration_discord_excerpt_length:
|
chat_integration_discord_excerpt_length:
|
||||||
default: 400
|
default: 400
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
########## GUILDED SETTINGS ###########
|
||||||
|
#######################################
|
||||||
|
chat_integration_guilded_enabled:
|
||||||
|
default: false
|
||||||
|
chat_integration_guilded_excerpt_length:
|
||||||
|
default: 400
|
||||||
|
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
######## MATTERMOST SETTINGS ##########
|
######## MATTERMOST SETTINGS ##########
|
||||||
#######################################
|
#######################################
|
||||||
|
|
|
@ -104,3 +104,4 @@ require_relative "provider/groupme/groupme_provider"
|
||||||
require_relative "provider/teams/teams_provider"
|
require_relative "provider/teams/teams_provider"
|
||||||
require_relative "provider/webex/webex_provider"
|
require_relative "provider/webex/webex_provider"
|
||||||
require_relative "provider/google/google_provider"
|
require_relative "provider/google/google_provider"
|
||||||
|
require_relative "provider/guilded/guilded_provider"
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module DiscourseChatIntegration
|
||||||
|
module Provider
|
||||||
|
module GuildedProvider
|
||||||
|
PROVIDER_NAME = "guilded".freeze
|
||||||
|
PROVIDER_ENABLED_SETTING = :chat_integration_guilded_enabled
|
||||||
|
|
||||||
|
CHANNEL_PARAMETERS = [
|
||||||
|
{ key: "name", regex: '^\S+' },
|
||||||
|
{ key: "webhook_url", regex: '^https:\/\/media\.guilded\.gg\/webhooks\/', unique: true, hidden: true }
|
||||||
|
].freeze
|
||||||
|
|
||||||
|
def self.trigger_notification(post, channel, rule)
|
||||||
|
webhook_url = channel.data['webhook_url']
|
||||||
|
message = generate_guilded_message(post)
|
||||||
|
response = send_message(webhook_url, message)
|
||||||
|
|
||||||
|
if !response.kind_of?(Net::HTTPSuccess)
|
||||||
|
raise ::DiscourseChatIntegration::ProviderError.new(info: {
|
||||||
|
error_key: nil, message: message, response_body: response.body
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.generate_guilded_message(post)
|
||||||
|
topic = post.topic
|
||||||
|
category = ''
|
||||||
|
if topic.category
|
||||||
|
category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]"
|
||||||
|
end
|
||||||
|
display_name = ::DiscourseChatIntegration::Helper.formatted_display_name(post.user)
|
||||||
|
|
||||||
|
icon_url =
|
||||||
|
if (url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)).present?
|
||||||
|
"#{Discourse.base_url}#{url}"
|
||||||
|
end
|
||||||
|
|
||||||
|
message = {
|
||||||
|
embeds: [{
|
||||||
|
title: "#{topic.title} #{(category == '[uncategorized]') ? '' : category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}",
|
||||||
|
url: post.full_url,
|
||||||
|
description: post.excerpt(SiteSetting.chat_integration_guilded_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true),
|
||||||
|
footer: {
|
||||||
|
icon_url: ensure_protocol(post.user.small_avatar_url),
|
||||||
|
text: "#{display_name} | #{post.created_at}"
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
message
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.send_message(url, message)
|
||||||
|
uri = URI(url)
|
||||||
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
|
http.use_ssl = (uri.scheme == 'https')
|
||||||
|
|
||||||
|
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
|
||||||
|
req.body = message.to_json
|
||||||
|
response = http.request(req)
|
||||||
|
|
||||||
|
response
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.ensure_protocol(url)
|
||||||
|
return url if !url.start_with?('//')
|
||||||
|
"http:#{url}"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,30 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe DiscourseChatIntegration::Provider::GuildedProvider do
|
||||||
|
let(:post) { Fabricate(:post) }
|
||||||
|
|
||||||
|
describe '.trigger_notifications' do
|
||||||
|
before do
|
||||||
|
SiteSetting.chat_integration_guilded_enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:chan1) { DiscourseChatIntegration::Channel.create!(provider: 'guilded', data: { name: "Awesome Channel", webhook_url: 'https://media.guilded.gg/webhooks/1234/abcd' }) }
|
||||||
|
|
||||||
|
it 'sends a webhook request' do
|
||||||
|
stub1 = stub_request(:post, 'https://media.guilded.gg/webhooks/1234/abcd').to_return(status: 200)
|
||||||
|
described_class.trigger_notification(post, chan1, nil)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles errors correctly' do
|
||||||
|
stub1 = stub_request(:post, "https://media.guilded.gg/webhooks/1234/abcd").to_return(status: 400)
|
||||||
|
expect(stub1).to have_been_requested.times(0)
|
||||||
|
expect { described_class.trigger_notification(post, chan1, nil) }.to raise_exception(::DiscourseChatIntegration::ProviderError)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue