├── LICENSE ├── README.md ├── config ├── locales │ ├── client.en.yml │ └── server.en.yml └── settings.yml └── plugin.rb /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ryan Fox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Discourse-Webhooks 2 | Add the ability to make HTTP requests from Discourse in response to certain events. 3 | 4 | # How To Use 5 | 6 | 1. See: [How to install Discourse plugins](https://meta.discourse.org/t/install-a-plugin/19157) 7 | 8 | 2. Create a route on your site for handling POST requests with JSON data. You should accept the event names as part of the URL, or in a query string. 9 | 10 | * Events are triggered with variable amounts of parameters, with no real definition of what event gets what parameters. The JSON data will contain an array of the parameters, in the order they were given. 11 | * If you choose to include the API key as part of the request, it will always be in the first value. 12 | 13 | 3. In the Discourse admin settings, under the "Webhooks" section, modify the "webhooks url format" to point at the route from step 2. 14 | 15 | 4. If you need to change the list of events that are registered, you'll need to restart Discourse to pick up those changes. There's currently no way to unregister events at run-time, and no way to determine which events were already registered by this plugin. 16 | 17 | 5. There are no formal event definitions. To get a list of possible events, check the [Discourse source code](https://github.com/discourse/discourse/search?q=DiscourseEvent.trigger+path%3A%2Flib+path%3A%2Fapp&type=Code) 18 | 19 | 6. Make sure to check the "webhooks enabled" box to start sending HTTP requests for events. 20 | -------------------------------------------------------------------------------- /config/locales/client.en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | admin_js: 3 | admin: 4 | site_settings: 5 | categories: 6 | webhooks: 'Webhooks' 7 | -------------------------------------------------------------------------------- /config/locales/server.en.yml: -------------------------------------------------------------------------------- 1 | en: 2 | site_settings: 3 | webhooks_enabled: 'Check this to enable the plugin. Otherwise, no HTTP requests will be sent in response to the events.' 4 | webhooks_registered_events: 'You will need to restart Discourse to pick up changes to this list. For a list of possible events and their parameters, see: https://github.com/discourse/discourse/search?q=DiscourseEvent.trigger+path%3A%2Flib+path%3A%2Fapp&type=Code' 5 | webhooks_url_format: 'Customizable URL to request upon receiving an event. Use %{event_name} to insert the name of the event, as show in the list above.' 6 | webhooks_verify_ssl: 'Uncheck this to disable SSL verification for webhook HTTPS requests. NOT RECOMMENDED.' 7 | webhooks_include_api_key: 'Include the "All Users" API key as a parameter to allow verification of the source of the webhook.' 8 | -------------------------------------------------------------------------------- /config/settings.yml: -------------------------------------------------------------------------------- 1 | webhooks: 2 | webhooks_enabled: 3 | client: true 4 | default: false 5 | webhooks_registered_events: 6 | client: true 7 | default: 'topic_created|post_created' 8 | type: list 9 | webhooks_url_format: 10 | client: true 11 | default: 'https://example.com/forum-hook/%{event_name}/' 12 | webhooks_verify_ssl: 13 | client: true 14 | default: true 15 | webhooks_include_api_key: 16 | client: true 17 | default: true 18 | -------------------------------------------------------------------------------- /plugin.rb: -------------------------------------------------------------------------------- 1 | # name: webhooks 2 | # about: Make HTTP requests when certain events occur 3 | # version: 0.1 4 | # authors: Ryan Fox 5 | # url: https://github.com/rcfox/Discourse-Webhooks 6 | 7 | after_initialize do 8 | 9 | SYSTEM_GUARDIAN = Guardian.new(User.find_by(id: -1)) 10 | 11 | # Include the SSO record with all User data for staff requests so that you 12 | # can figure out which of your users triggered the current webhook request. 13 | UserSerializer.class_eval do 14 | staff_attributes :single_sign_on_record 15 | end 16 | 17 | User.class_eval do 18 | # User.as_json seems to be broken due to no User::View being defined? 19 | # I don't really know what that's about. Let's just hack around it! 20 | def as_json(options = nil) 21 | user_serializer = UserSerializer.new(self, scope: SYSTEM_GUARDIAN) 22 | user_serializer.serializable_hash 23 | end 24 | end 25 | 26 | def build_event_url(url_template, event_name) 27 | url = String.new(url_template) 28 | url.gsub!("%{event_name}", event_name) 29 | return url 30 | end 31 | 32 | SiteSetting.webhooks_registered_events.split('|').each do |event_name| 33 | 34 | DiscourseEvent.on(event_name.to_sym) do |*params| 35 | 36 | next unless SiteSetting.webhooks_enabled 37 | 38 | if SiteSetting.webhooks_include_api_key 39 | api_key = ApiKey.find_by(user_id: nil) 40 | if not api_key 41 | Rails.logger.warn('Webhooks configured to include the "All User" API key, but it does not exist.') 42 | else 43 | params.unshift(api_key.key) 44 | end 45 | end 46 | 47 | uri = URI.parse(build_event_url(SiteSetting.webhooks_url_format, event_name)) 48 | http = Net::HTTP.new(uri.host, uri.port) 49 | http.use_ssl = true if uri.scheme == 'https' 50 | http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless SiteSetting.webhooks_verify_ssl 51 | 52 | request = Net::HTTP::Post.new(uri.path) 53 | request.add_field('Content-Type', 'application/json') 54 | request.body = params.to_json 55 | 56 | response = http.request(request) 57 | case response 58 | when Net::HTTPSuccess then 59 | # nothing 60 | else 61 | Rails.logger.error("#{uri}: #{response.code} - #{response.message}") 62 | end 63 | end 64 | 65 | end 66 | 67 | end 68 | --------------------------------------------------------------------------------