Expanding Webhook Events
Creating your own webhook events
Introduction
With Umbraco, you can even create your own webhook events.
This documentation guides you through the process of implementing your own webhook events using the WebhookEventBase<TNotification>
base class.
Creating an event with the WebhookEventBase
The WebhookEventBase<TNotification>
class serves as the foundation for creating custom webhook events. Here's a brief overview of its key components:
Alias: The property that must be overridden to provide a unique identifier for your webhook event.
EventName: A property that represents the name of the event. It is automatically set based on the provided alias unless explicitly specified.
EventType: A property that categorizes the event type. It defaults to "Others" but can be customized using the
WebhookEventAttribute
.WebhookSettings: The property containing the current webhook settings.
ProcessWebhooks: The method responsible for processing webhooks for a given notification.
ShouldFireWebhookForNotification: The method determining whether webhooks should be fired for a specific notification.
ConvertNotificationToRequestPayload: An optional method allowing customization of the notification payload before sending it to webhooks.
Creating a Custom Webhook Event
To create a custom webhook event, follow these steps:
Derive from
WebhookEventBase<TNotification>
:Override the Alias Property:
Provide a unique identifier for your event using the
Alias
property:Apply
WebhookEventAttribute
(Optional):You can use the
WebhookEventAttribute
to specify the event name and type. Apply this attribute to your custom event class:Umbraco already has some types as constants, which you can find at
Constants.WebhookEvents.Types
. If you do not specify this attribute, the event name will default to your alias, and the type will default toOther
.Implement Notification Handling:
If needed, customize the handling of the notification in the
HandleAsync
method.Register Your Webhook Event:
Ensure that Umbraco is aware of your custom event by registering it in a composer:
Implement Optional Overrides: Depending on your requirements, you can override methods such as
ConvertNotificationToRequestPayload
andShouldFireWebhookForNotification
to customize the behavior of your webhook event.
Example Implementation
Here's a basic example of a custom webhook event:
Creating an event with the WebhookEventContentBase<TNotification, TEntity>
For scenarios where your webhook event is content-specific, Umbraco provides another base class: WebhookEventContentBase<TNotification, TEntity>
. This class is an extension of the generic WebhookEventBase<TNotification>
and introduces content-related functionalities.
The WebhookEventContentBase<TNotification, TEntity>
class is designed for content-specific webhook events, where TEntity
is expected to be a type that implements the IContentBase
interface.
Usage
To leverage the WebhookEventContentBase<TNotification, TEntity>
class, follow these steps:
Derive from
WebhookEventContentBase<TNotification, TEntity>
:Override the Required Methods:
GetEntitiesFromNotification: Implement this method to extract content entities from the notification.
ConvertEntityToRequestPayload: Implement this method to customize the content entity payload before sending it to webhooks.
If we take a look at the
ContentPublishedWebhookEvent
, we can see how these methods are overriden.ProcessWebhooks Implementation:
The
ProcessWebhooks
method in this class has been enhanced to iterate through content entities obtained from the notification. It checks the content type of each entity against the specified webhook's content type keys, firing webhooks only for matching entities.
Last updated