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. 


Adapter Pattern in play:

Details:

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:


Sitecore Receiver role:


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:


You could deploy the project to a local virtual directory and access the http url as follows for debugging + you can view the Sitecore event payload:


Note that all Sitecore listed event payloads will be captured in the above page, if they are added to the Sitecore webhook event handler' selected events list.

The Url in Sitecore Webhook event handler in above case will look like this: http://sitecorereceiver/api/webhooks/incoming/sitecorejson?code=93999ec7c1d794c0c780e49a5c72972590571fb9

Now, over to Slack Webhook setup:

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:



In the channel:



Comments

Popular Posts