Add HipChat support

This commit is contained in:
David Taylor 2017-07-24 19:22:19 +01:00
parent 681f37bc2b
commit c51e8ec3b5
6 changed files with 165 additions and 4 deletions

View File

@ -82,4 +82,20 @@ en:
help: "A name to describe the channel. It is not used for the connection to Discord."
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
#######################################
########### HIPCHAT STRINGS ###########
#######################################
hipchat:
title: "HipChat"
param:
name:
title: "Name"
help: "A name to describe the channel. It is not used for the connection to HipChat."
webhook_url:
title: Webhook URL
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

View File

@ -31,6 +31,13 @@ en:
#######################################
chat_integration_discord_enabled: "Enable the Discord chat-integration provider"
chat_integration_discord_excerpt_length: "Discord post excerpt length"
#######################################
########## 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_excerpt_length: "Hipchat post excerpt length"
chat_integration:
@ -122,4 +129,10 @@ en:
<b>List rules:</b> <code>/status</code>
<b>Help:</b> <code>/help</code>
<b>Help:</b> <code>/help</code>
#######################################
########### HIPCHAT STRINGS ###########
#######################################
hipchat:
message: <b>%{user}</b> posted in <a href="%{post_url}">%{title}</a>

View File

@ -47,4 +47,14 @@ plugins:
chat_integration_discord_enabled:
default: false
chat_integration_discord_excerpt_length:
default: 400
default: 400
#######################################
########## HIPCHAT SETTINGS ###########
#######################################
chat_integration_hipchat_enabled:
default: false
chat_integration_hipchat_icon_url:
default: ''
chat_integration_hipchat_excerpt_length:
default: 400

View File

@ -91,4 +91,5 @@ end
require_relative "provider/slack/slack_provider.rb"
require_relative "provider/telegram/telegram_provider.rb"
require_relative "provider/discord/discord_provider.rb"
require_relative "provider/discord/discord_provider.rb"
require_relative "provider/hipchat/hipchat_provider.rb"

View File

@ -0,0 +1,93 @@
module DiscourseChat
module Provider
module HipchatProvider
PROVIDER_NAME = "hipchat".freeze
PROVIDER_ENABLED_SETTING = :chat_integration_hipchat_enabled
CHANNEL_PARAMETERS = [
{key: "name", regex: '^\S+'},
{key: "webhook_url", regex: 'hipchat\.com'},
{key: "color", regex: '(yellow|green|red|purple|gray|random)'}
]
def self.send_message(url, message)
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri, 'Content-Type' =>'application/json')
req.body = message.to_json
response = http.request(req)
return response
end
def self.generate_hipchat_message(post)
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
message_text = I18n.t(
"chat_integration.provider.hipchat.message",
user: display_name,
post_url: post.full_url,
title: CGI::escapeHTML(topic.title),
)
icon_url =
if !SiteSetting.chat_integration_hipchat_icon_url.blank?
UrlHelper.absolute(SiteSetting.chat_integration_hipchat_icon_url)
elsif !SiteSetting.logo_small_url.blank?
UrlHelper.absolute(SiteSetting.logo_small_url)
end
message = {
message: message_text, # Fallback for clients that don't support the card markup
notify: true,
card: {
style: "application",
url: post.full_url,
format: "medium",
id: "discoursecard:#{post.id}",
title: topic.title,
description: post.excerpt(SiteSetting.chat_integration_hipchat_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true),
icon: {
url: icon_url,
},
activity: {
html: message_text
}
}
}
return message
end
def self.trigger_notification(post, channel)
webhook_url = channel.data['webhook_url']
message = generate_hipchat_message(post)
message[:color] = channel.data['color']
response = send_message(webhook_url, message)
if not response.kind_of? Net::HTTPSuccess
error_key = nil
raise ::DiscourseChat::ProviderError.new info: {error_key: error_key, message: message, response_body:response.body}
end
end
end
end
end

View File

@ -0,0 +1,28 @@
require 'rails_helper'
RSpec.describe DiscourseChat::Provider::HipchatProvider do
let(:post) { Fabricate(:post) }
describe '.trigger_notifications' do
before do
SiteSetting.chat_integration_hipchat_enabled = true
end
let(:chan1){DiscourseChat::Channel.create!(provider:'hipchat', data:{name: "Awesome Channel", webhook_url: 'https://blah.hipchat.com/abcd', color: "red"})}
it 'sends a webhook request' do
stub1 = stub_request(:post, 'https://blah.hipchat.com/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://blah.hipchat.com/abcd").to_return(status: 400)
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