This article will discuss Umbraco publish events and give a look at how to use components and composers. We'll build an on publish event to send a message to a discord server.
Firstly, we need to create the webhook in discord (but wherever you want to post to will have a place to create a webhook, like slack, etc). In discord, you can rght click on channel and edit, you can add a webhook here (if you have permissions!):
I always go to Postman first, and test the webhook works before I try to then use it in code. I do this whenever I integrate with a new API.
and here we see it in our Discord channel:
So now we need to hook this up to an Umbraco event that is called on publish of a node. You can read more about that here. I'd recommend reading this before continuing as this explains the concepts we'll use.
I have created a Component to set this up as an on publish event, this is done in the initialize method.
Then in the "ContentService_Published" method, we should write the logic to be called on publish. For us, this is a message to our Discord webhook.
Finally, to tell Umbraco to set this component up, we need to add a Composer. Composers are used to register our custom code or our code that is subscribing to events (like our on publish code). You can read more here. This is my example:
Now, when we publish a node, we get this wee message in Discord! This may not be super useful to notify with every publish, but we could make this only be specific to certain page types or user groups, for example.
You can see the full code example here.
--------