mirror of
https://github.com/discourse/discourse-chat-integration.git
synced 2025-07-04 13:12:10 +00:00
Add HipChat support
This commit is contained in:
parent
681f37bc2b
commit
c51e8ec3b5
@ -83,3 +83,19 @@ en:
|
|||||||
webhook_url:
|
webhook_url:
|
||||||
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
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
########### 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
|
@ -32,6 +32,13 @@ en:
|
|||||||
chat_integration_discord_enabled: "Enable the Discord chat-integration provider"
|
chat_integration_discord_enabled: "Enable the Discord chat-integration provider"
|
||||||
chat_integration_discord_excerpt_length: "Discord post excerpt length"
|
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:
|
chat_integration:
|
||||||
|
|
||||||
all_categories: "(all categories)"
|
all_categories: "(all categories)"
|
||||||
@ -123,3 +130,9 @@ en:
|
|||||||
<b>List rules:</b> <code>/status</code>
|
<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>
|
||||||
|
@ -48,3 +48,13 @@ plugins:
|
|||||||
default: false
|
default: false
|
||||||
chat_integration_discord_excerpt_length:
|
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
|
||||||
|
@ -92,3 +92,4 @@ end
|
|||||||
require_relative "provider/slack/slack_provider.rb"
|
require_relative "provider/slack/slack_provider.rb"
|
||||||
require_relative "provider/telegram/telegram_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"
|
||||||
|
93
lib/discourse_chat/provider/hipchat/hipchat_provider.rb
Normal file
93
lib/discourse_chat/provider/hipchat/hipchat_provider.rb
Normal 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
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user