From 9b229b63f9b2117f6f0f5f9ae0d58b326f13396e Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 23 Aug 2017 12:36:11 +0100 Subject: [PATCH] Use generic messages API for Zulip It is now available to bots, so there is no need for the custom discourse endpoint any more :) --- config/locales/server.en.yml | 1 + config/settings.yml | 2 ++ lib/discourse_chat/provider/zulip/zulip_provider.rb | 12 +++++++----- .../provider/zulip/zulip_provider_spec.rb | 5 +++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 14ff433..6bfce67 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -61,6 +61,7 @@ en: ####################################### chat_integration_zulip_enabled: "Enable the Matrix chat integration provider" chat_integration_zulip_server: "The base URL for your Zulip server. Make sure to include http(s)://" + chat_integration_zulip_bot_email_address: "The email address associated with your Zulip bot" chat_integration_zulip_bot_api_key: "The API key for your Zulip bot" chat_integration_zulip_excerpt_length: "Zulip post excerpt length" diff --git a/config/settings.yml b/config/settings.yml index 0e87e8b..f1ac881 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -92,6 +92,8 @@ plugins: default: false chat_integration_zulip_server: default: '' + chat_integration_zulip_bot_email_address: + default: '' chat_integration_zulip_bot_api_key: default: '' chat_integration_zulip_excerpt_length: diff --git a/lib/discourse_chat/provider/zulip/zulip_provider.rb b/lib/discourse_chat/provider/zulip/zulip_provider.rb index d115bb8..2679a08 100644 --- a/lib/discourse_chat/provider/zulip/zulip_provider.rb +++ b/lib/discourse_chat/provider/zulip/zulip_provider.rb @@ -9,13 +9,14 @@ module DiscourseChat ] def self.send_message(message) - uri = URI("#{SiteSetting.chat_integration_zulip_server}/api/v1/external/discourse?api_key=#{SiteSetting.chat_integration_zulip_bot_api_key}") + uri = URI("#{SiteSetting.chat_integration_zulip_server}/api/v1/messages") 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 + req = Net::HTTP::Post.new(uri) + req.basic_auth(SiteSetting.chat_integration_zulip_bot_email_address, SiteSetting.chat_integration_zulip_bot_api_key) + req.set_form_data(message) response = http.request(req) @@ -36,9 +37,10 @@ module DiscourseChat excerpt: post.excerpt(SiteSetting.chat_integration_zulip_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true)) data = { - stream: stream, + type: 'stream', + to: stream, subject: subject, - message: message + content: message } end diff --git a/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb b/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb index 88205d0..4d3fd1d 100644 --- a/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb +++ b/spec/lib/discourse_chat/provider/zulip/zulip_provider_spec.rb @@ -7,19 +7,20 @@ RSpec.describe DiscourseChat::Provider::ZulipProvider do before do SiteSetting.chat_integration_zulip_enabled = true SiteSetting.chat_integration_zulip_server = "https://hello.world" + SiteSetting.chat_integration_zulip_bot_email_address = "some_bot@example.com" SiteSetting.chat_integration_zulip_bot_api_key = "secret" end let(:chan1) { DiscourseChat::Channel.create!(provider: 'zulip', data: { stream: "general", subject: "Discourse Notifications" }) } it 'sends a webhook request' do - stub1 = stub_request(:post, 'https://hello.world/api/v1/external/discourse?api_key=secret').to_return(status: 200) + stub1 = stub_request(:post, 'https://hello.world/api/v1/messages').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://hello.world/api/v1/external/discourse?api_key=secret').to_return(status: 400, body: '{}') + stub1 = stub_request(:post, 'https://hello.world/api/v1/messages').to_return(status: 400, body: '{}') 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