Add Mattermost support
This commit is contained in:
parent
18aa827686
commit
61a4fd2c23
|
@ -98,4 +98,16 @@ en:
|
|||
help: The webhook URL created in your HipChat integration
|
||||
color:
|
||||
title: Color
|
||||
help: The colour of the message in HipChat. Must be one of yellow,green,red,purple,gray,random
|
||||
help: The colour of the message in HipChat. Must be one of yellow,green,red,purple,gray,random
|
||||
|
||||
#######################################
|
||||
######### MATTERMOST STRINGS ##########
|
||||
#######################################
|
||||
mattermost:
|
||||
title: "Mattermost"
|
||||
param:
|
||||
identifier:
|
||||
title: Channel
|
||||
help: "e.g. #channel, @username."
|
||||
errors:
|
||||
channel_not_found: "The specified channel does not exist on Mattermost"
|
||||
|
|
|
@ -36,8 +36,16 @@ en:
|
|||
########## HIPCHAT SETTINGS ###########
|
||||
#######################################
|
||||
chat_integration_hipchat_enabled: "Enable the Hipchat chat-integration provider"
|
||||
chat_integration_icon_url: "Icon for posts to hipchat (defaults to forum logo)"
|
||||
chat_integration_hipchat_icon_url: "Icon for posts to hipchat (defaults to forum logo)"
|
||||
chat_integration_hipchat_excerpt_length: "Hipchat post excerpt length"
|
||||
|
||||
#######################################
|
||||
######## MATTERMOST SETTINGS ##########
|
||||
#######################################
|
||||
chat_integration_mattermost_enabled: "Enable the Mattermost chat-integration provider"
|
||||
chat_integration_mattermost_webhook_url: 'URL for the Mattermost webhook'
|
||||
chat_integration_mattermost_icon_url: "Icon for posts to mattermost (defaults to forum logo)"
|
||||
chat_integration_mattermost_excerpt_length: "Mattermost post excerpt length"
|
||||
|
||||
chat_integration:
|
||||
|
||||
|
|
|
@ -58,3 +58,15 @@ plugins:
|
|||
default: ''
|
||||
chat_integration_hipchat_excerpt_length:
|
||||
default: 400
|
||||
|
||||
#######################################
|
||||
######## MATTERMOST SETTINGS ##########
|
||||
#######################################
|
||||
chat_integration_mattermost_enabled:
|
||||
default: false
|
||||
chat_integration_mattermost_webhook_url:
|
||||
default: ''
|
||||
chat_integration_mattermost_icon_url:
|
||||
default: ''
|
||||
chat_integration_mattermost_excerpt_length:
|
||||
default: 400
|
||||
|
|
|
@ -93,3 +93,4 @@ require_relative "provider/slack/slack_provider.rb"
|
|||
require_relative "provider/telegram/telegram_provider.rb"
|
||||
require_relative "provider/discord/discord_provider.rb"
|
||||
require_relative "provider/hipchat/hipchat_provider.rb"
|
||||
require_relative "provider/mattermost/mattermost_provider.rb"
|
||||
|
|
|
@ -47,6 +47,7 @@ module DiscourseChat
|
|||
end
|
||||
|
||||
def self.trigger_notification(post, channel)
|
||||
# Adding ?wait=true means that we actually get a success/failure response, rather than returning asynchronously
|
||||
webhook_url = channel.data['webhook_url']+'?wait=true'
|
||||
|
||||
message = generate_discord_message(post)
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
module DiscourseChat
|
||||
module Provider
|
||||
module MattermostProvider
|
||||
PROVIDER_NAME = "mattermost".freeze
|
||||
PROVIDER_ENABLED_SETTING = :chat_integration_mattermost_enabled
|
||||
CHANNEL_PARAMETERS = [
|
||||
{key: "identifier", regex: '^[@#]\S*$'}
|
||||
]
|
||||
|
||||
def self.send_via_webhook(message)
|
||||
|
||||
uri = URI(SiteSetting.chat_integration_mattermost_webhook_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)
|
||||
|
||||
unless response.kind_of? Net::HTTPSuccess
|
||||
if response.body.include? "Couldn't find the channel"
|
||||
error_key = 'chat_integration.provider.mattermost.errors.channel_not_found'
|
||||
else
|
||||
error_key = nil
|
||||
end
|
||||
raise ::DiscourseChat::ProviderError.new info: {error_key: error_key, request: req.body, response_code:response.code, response_body:response.body}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def self.mattermost_message(post, channel)
|
||||
display_name = "@#{post.user.username}"
|
||||
full_name = post.user.name || ""
|
||||
|
||||
if !(full_name.strip.empty?) && (full_name.strip.gsub(' ', '_').casecmp(post.user.username) != 0) && (full_name.strip.gsub(' ', '').casecmp(post.user.username) != 0)
|
||||
display_name = "#{full_name} @#{post.user.username}"
|
||||
end
|
||||
|
||||
topic = post.topic
|
||||
|
||||
category = ''
|
||||
if topic.category
|
||||
category = (topic.category.parent_category) ? "[#{topic.category.parent_category.name}/#{topic.category.name}]": "[#{topic.category.name}]"
|
||||
end
|
||||
|
||||
icon_url =
|
||||
if !SiteSetting.chat_integration_slack_icon_url.blank?
|
||||
UrlHelper.absolute(SiteSetting.chat_integration_mattermost_icon_url)
|
||||
elsif !SiteSetting.logo_small_url.blank?
|
||||
UrlHelper.absolute(SiteSetting.logo_small_url)
|
||||
end
|
||||
|
||||
message = {
|
||||
channel: channel,
|
||||
username: SiteSetting.title || "Discourse",
|
||||
icon_url: icon_url,
|
||||
attachments: []
|
||||
}
|
||||
|
||||
summary = {
|
||||
fallback: "#{topic.title} - #{display_name}",
|
||||
author_name: display_name,
|
||||
author_icon: post.user.small_avatar_url,
|
||||
color: topic.category ? "##{topic.category.color}" : nil,
|
||||
text: post.excerpt(SiteSetting.chat_integration_mattermost_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true),
|
||||
title: "#{topic.title} #{(category == '[uncategorized]')? '' : category} #{topic.tags.present? ? topic.tags.map(&:name).join(', ') : ''}",
|
||||
title_link: post.full_url,
|
||||
thumb_url: post.full_url,
|
||||
}
|
||||
|
||||
message[:attachments].push(summary)
|
||||
message
|
||||
end
|
||||
|
||||
def self.trigger_notification(post, channel)
|
||||
channel_id = channel.data['identifier']
|
||||
message = mattermost_message(post, channel_id)
|
||||
|
||||
self.send_via_webhook(message)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe DiscourseChat::Provider::MattermostProvider do
|
||||
let(:post) { Fabricate(:post) }
|
||||
|
||||
describe '.trigger_notifications' do
|
||||
before do
|
||||
SiteSetting.chat_integration_mattermost_enabled = true
|
||||
SiteSetting.chat_integration_mattermost_webhook_url = "https://mattermost.blah/hook/abcd"
|
||||
end
|
||||
|
||||
let(:chan1){DiscourseChat::Channel.create!(provider:'mattermost', data:{identifier: "#awesomechannel"})}
|
||||
|
||||
it 'sends a webhook request' do
|
||||
stub1 = stub_request(:post, 'https://mattermost.blah/hook/abcd').to_return(status: 200)
|
||||
described_class.trigger_notification(post, chan1)
|
||||
expect(stub1).to have_been_requested.once
|
||||
end
|
||||
|
||||
it 'handles errors correctly' do
|
||||
stub1 = stub_request(:post, "https://mattermost.blah/hook/abcd").to_return(status: 500, body: "error")
|
||||
expect(stub1).to have_been_requested.times(0)
|
||||
expect{described_class.trigger_notification(post, chan1)}.to raise_exception(::DiscourseChat::ProviderError)
|
||||
expect(stub1).to have_been_requested.once
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue