Sitecore Webhook Event Handler that sends Slack notification
In this blog post I cover the steps I followed to build a webhook handler that in turn sends Sitecore item delete notification to a Slack channel.
This blog article can be broken down into following parts:
- Setup a Sitecore webhook event handler that triggers for different Sitecore item events
- Write an asp.net receiver that's invoked on Sitecore event fire and does further processing
- Deploy to Azure App Service
- Create a Slack channel and setup webhook to receive message and display
- Write an asp.net mvc page that displays Sitecore item events with associated data
- Test end-to-end flow where user deletes a Sitecore item and users are notified in the Slack channel
a. Setup a Sitecore webhook event handler that triggers for different Sitecore item events
- Create the handler under /sitecore/system/Webhooks/
- Give a proper name, description for your event handler
- The following aspects are important
1. What event(s) must fire this handler - Event(s) selection
2. Under what condition must it fire - Rules
3. Should the event handler fire? - Enabled
4. What should be invoked? - Receiver Url
In the screen shot above, although I have selected multiple events, the purpose of this blog article is to fire the event only during item delete so, just selecting item:deleting should be enough.
b. Write an asp.net receiver that's invoked on Sitecore event fire and does further processing
This is the important part since this is where we decide what must be done if the Sitecore event fires.
This code covers the following parts:
1. Microsoft.AspNet.WebHooks.Receivers.Sitecore project:
This is based on Microsoft asp.net webhook receiver github project. The following are the important changes:
- Use Microsoft.Aspnet.Webhooks.Common and Microsoft.Aspnet.Webhooks.Receivers packages
- SitecoreJsonWebHookReceiver inherits from WebHookReceiver
- The ReceiveAsync override method passes the code from SitecoreReceiver project to base class for validation
- Invokes ExecuteWebHookAsync method passing the request from Sitecore
2. SiecoreReceiver project
- Use Microsoft.Aspnet.Webhooks.Common and Microsoft.Aspnet.Webhooks.Receivers packages
- Reference Microsoft.AspNet.WebHooks.Receivers.Sitecore project
- The crux in this project is SitecoreJsonWebHookHandler
The ExecuteAsync override method in SitecoreJsonWebHookHandler class does the job of invoking the Slack webhook passing the Sitecore item event details.
Another important part in the SitecoreReceiver project is this code used for validation while invoking the receiver, adjust the code secret as needed:
Since the events are added to a list, there is the controller that invokes the view to display the list of events in the current session. This can be built over by adding EF/SQL Server to the stack. Deployed to Azure:
Create a Slack Workspace / App:
Enable an Incoming webhook in Slack:
Slack Webhook ready to invoke:
This dirty code invokes the Slack webhook for SitecoreJsonWebHookHandler.cs:
///////////////////////////////////////////////////////////////////////////
var webhookUrl = "https://hooks.slack.com/services/Tttttt/vvvvv/dfdfdsf4sWHa8E22";//this is bogus, ensure this is correct
var client = new HttpClient();
var messageString = "Item deleted by user from Sitecore; Item id: " + data.Item.Id + " Item Name: " + data.Item.Name;
var jsonString = "{\"text\": \"" + messageString + "\"}";
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var result = client.PostAsync(webhookUrl, content).Result;
///////////////////////////////////////////////////////////////////////////
Note that there is a switch-case that can handle different events and also a C# object that can hold the event data.
C# Models:
Slack notification on item deletion:
Comments
Post a Comment