In the blog Introducing Microsoft ASP.NET WebHooks Preview, we gave an overview of how to work with Microsoft ASP.NET WebHooks. Slack provides an interesting model where you can set up a WebHook to be fired when a certain trigger word is used in one of their messaging channels. In addition, it is possible for the WebHook to send data back to the channel which is very useful. For example, if you have a trigger word AskMe: then you can get the WebHook to respond like this:
To setup Slack WebHooks you need a Slack account. Then you configure the WebHook using what they call an Integration as follows:
Pick the Outgoing WebHooks integration, select Add Outgoing WebHooks Integration and look for the fields Trigger Words, URLs, and Token. Fill in whatever trigger words and WebHook URI, and copy the Token for use later. The URI must have the form https://<host>/api/webhooks/incoming/slack
The last part is to configure the WebHook receiver. After having installed the Microsoft.AspNet.WebHooks.Receivers.Slack Nuget package into your ASP.NET application, this happens exactly like show in the blog Introducing Microsoft ASP.NET WebHooks Preview:
The first part of the configuration is done in WebApiConfig.cs where you add line 17 like this:
The second part is to set the secret token so that it can be verified by the receiver. This is done by setting the MS_WebHookReceiverSecret_Slack app setting for your Web Application to the value of the secret Token obtained from before. As stated in the blog Introducing Microsoft ASP.NET WebHooks Preview, the preferred way to do this is to set it in the Azure Portal:
Defining a Handler
Slack sends WebHook data in the form of HTML Form data so we read it as a NameValueCollection. Further, as the Slack WebHook can return data, we use the Response property on the WebHookHandlerContext to define the data we want to return. The SlackResponse is just a helper formatting the response as expected by Slack.
Trying it Out
Once deployed, we are ready to try this out: In a Slack channel write something like this AskMe: You there? You should now be called in your handler and see a response on the Slack channel like this:
This should hopefully be enough to get you started receiving WebHooks from Slack using ASP.NET WebHooks Preview.