# Can a plugin add a webhook?

**URL:** https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361
**Category:** Development
**Created:** [February 23, 2018, 10:10pm UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361 "2018-02-23T22:10:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [February 23, 2018, 10:10pm UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/1 "2018-02-23T22:10:51Z")

</div>

I’m working on a plugin that communicates with an external web app. The web app is also informed of changes in Discourse through the use of webhooks.  
Right now, installing the plugin is complicated, because it requires to also manually set up the webhooks. Is there a way my plugin could set up the webhooks itself?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 23, 2018, 11:31pm UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/2 "2018-02-23T23:31:48Z")

</div>

I’m not much of a plugin author, but I’m pretty sure that you can call something like

```
hook = WebHook.new(payload_url: "https://sefsdf", secret: "lkjlkjlkj", active: true)
hook.save

```

Here are the other things in a webhook that you may or may not need to set.

```plaintext
 id: nil,
 payload_url: nil,
 content_type: 1,
 last_delivery_status: 1,
 status: 1,
 secret: "",
 wildcard_web_hook: false,
 verify_certificate: true,
 active: false,
 created_at: nil,
 updated_at: nil>

```

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [February 24, 2018, 2:19am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/3 "2018-02-24T02:19:50Z")

</div>

Ah that would only create a web hook record in the database but doesn’t enqueue any jobs.

> [@jack2](#):
>
> I’m working on a plugin that communicates with an external web app.

What kind of custom fields/webhook are you sending to your web app? You could subscribe to certain `DiscourseEvents` and add a custom Sidekiq job which calls an API on your web app.

---

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [February 24, 2018, 11:19am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/4 "2018-02-24T11:19:14Z")

</div>

Thanks @tgxworld. I’ve seen examples of how to use `DiscourseEvents` to emulate Discourse native webhooks [here](https://github.com/rcfox/Discourse-Webhooks/blob/master/plugin.rb#L34) and [here](https://github.com/bernd/discourse-slack-plugin/blob/master/plugin.rb#L8). I will try to do that if there’s no other choice.

But this seems complex. I would love to have a one-liner, like what @pfaffman proposed. [When you use the UI](https://meta.discourse.org/t/setting-up-webhooks/49045) or [the API](http://docs.discourse.org/#tag/Web-Hooks%2Fpaths%2F~1admin~1api~1web_hooks%2Fpost) to create a webhook, doesn’t Discourse call an internal function I could also use from my `plugin.rb` file?

---

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [February 24, 2018, 11:25am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/5 "2018-02-24T11:25:37Z")

</div>

Is [this](https://github.com/discourse/discourse/blob/cf9607a0cb5753bdae1c756752021aeb2df3f264/spec/controllers/admin/web_hooks_controller_spec.rb#L13) a call to the kind of internal function I’ve just mentioned?

---

<div class="post-metadata">

### Author: ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)
#### Post date: [February 26, 2018, 9:23am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/6 "2018-02-26T09:23:54Z")

</div>

If you want to invoke a webhook when certain internal event happens, here it is the example. [GitHub - erickguan/discourse-user-created-webhook · GitHub](https://github.com/fantasticfears/discourse-user-created-webhook).  
If you want to add a new type of webhook, here it is the example. [GitHub - erickguan/discourse-webhooks-example · GitHub](https://github.com/fantasticfears/discourse-webhooks-example)

---

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [February 26, 2018, 11:32am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/7 "2018-02-26T11:32:39Z")

</div>

Thanks a lot @fantasticfears, I will try this.

---

<div class="post-metadata">

### Author: ![jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@jack2](https://meta.discourse.org/u/jack2)
#### Post date: [February 27, 2018, 11:27am UTC](https://meta.discourse.org/t/can-a-plugin-add-a-webhook/81361/8 "2018-02-27T11:27:33Z")

</div>

Here is what I did eventually:

Since I need standard notifications for topic creation and deletion, I figured out Discourse already enqueues those, as part of the standard webhook feature. So there’s no need to do it myself.

What remains to be done, as @pfaffman suggested, is to add the webhook to the database. However, the proposed code fails silently, probably because of missing and/or incorrect parameters.

Here is my full working code:

```ruby
#customization:plugin.rb

# Add our webhook automatically, so that admin doesn't have to do it manually
after_initialize do
	# Delete our previously installed webhook (reserved id = 1000)
	WebHook.where(id: 1000).destroy_all

	# Create the webhook (reserved id = 1000)
	hook = WebHook.new(
		id: 1000, # Our reserved id						
		payload_url: 'https://hfzeoihifzh/',
		content_type: 1, # 1 = 'application/json'
		secret: "min12characters",
		active: true,
		verify_certificate: true,
		web_hook_event_type_ids: [1] # 1 = topic events
	)
	hook.save	
end

```
