diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index f53c5c3..9e5c44d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 3b8e923..11f6325 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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: List rules: /status - Help: /help \ No newline at end of file + Help: /help + + ####################################### + ########### HIPCHAT STRINGS ########### + ####################################### + hipchat: + message: %{user} posted in %{title} diff --git a/config/settings.yml b/config/settings.yml index 2158c1a..d75535b 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -47,4 +47,14 @@ plugins: chat_integration_discord_enabled: default: false chat_integration_discord_excerpt_length: - default: 400 \ No newline at end of file + default: 400 + +####################################### +########## HIPCHAT SETTINGS ########### +####################################### + chat_integration_hipchat_enabled: + default: false + chat_integration_hipchat_icon_url: + default: '' + chat_integration_hipchat_excerpt_length: + default: 400 diff --git a/lib/discourse_chat/provider.rb b/lib/discourse_chat/provider.rb index 8c7b920..2dc2b01 100644 --- a/lib/discourse_chat/provider.rb +++ b/lib/discourse_chat/provider.rb @@ -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" \ No newline at end of file +require_relative "provider/discord/discord_provider.rb" +require_relative "provider/hipchat/hipchat_provider.rb" diff --git a/lib/discourse_chat/provider/hipchat/hipchat_provider.rb b/lib/discourse_chat/provider/hipchat/hipchat_provider.rb new file mode 100644 index 0000000..14a9c80 --- /dev/null +++ b/lib/discourse_chat/provider/hipchat/hipchat_provider.rb @@ -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 \ No newline at end of file diff --git a/spec/lib/discourse_chat/provider/hipchat/hipchat_provider_spec.rb b/spec/lib/discourse_chat/provider/hipchat/hipchat_provider_spec.rb new file mode 100644 index 0000000..46c0b71 --- /dev/null +++ b/spec/lib/discourse_chat/provider/hipchat/hipchat_provider_spec.rb @@ -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