هل يمكن لإضافة (plugin) إضافة ويب هوك (webhook)؟

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?

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.

 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>

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

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.

Thanks @tgxworld. I’ve seen examples of how to use DiscourseEvents to emulate Discourse native webhooks here and here. 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 or the API to create a webhook, doesn’t Discourse call an internal function I could also use from my plugin.rb file?

Is this a call to the kind of internal function I’ve just mentioned?

If you want to invoke a webhook when certain internal event happens, here it is the example. GitHub - erickguan/discourse-user-created-webhook · GitHub.
If you want to add a new type of webhook, here it is the example. GitHub - erickguan/discourse-webhooks-example · GitHub

Thanks a lot @fantasticfears, I will try this.

إليك ما قمت به في النهاية:

بما أنني أحتاج إلى إشعارات قياسية لإنشاء المواضيع وحذفها، فقد استنتجت أن Discourse تقوم بالفعل بتكديس هذه الإشعارات كجزء من ميزة الويب هوك القياسية. لذا لا داعي للقيام بذلك بنفسي.

ما يتبقى إنجازه، كما اقترح @pfaffman، هو إضافة الويب هوك إلى قاعدة البيانات. ومع ذلك، يفشل الكود المقترح بصمت، ربما بسبب وجود معلمات مفقودة و/أو غير صحيحة.

إليك كودي الكامل والفعال:

#customization:plugin.rb

# إضافة الويب هوك الخاص بنا تلقائيًا، حتى لا يضطر المسؤول إلى القيام بذلك يدويًا
after_initialize do
	# حذف الويب هوك المثبت سابقًا (معرف محجوز = 1000)
	WebHook.where(id: 1000).destroy_all

	# إنشاء الويب هوك (معرف محجوز = 1000)
	hook = WebHook.new(
		id: 1000,							# معرفنا المحجوز						
		payload_url: 'https://hfzeoihifzh/',
		content_type: 1,					# 1 = 'application/json'
		secret: "min12characters",
		active: true,
		verify_certificate: true,
		web_hook_event_type_ids: [1]		# 1 = أحداث المواضيع
	)
	hook.save	
end