Quantcast
Channel: ASP.NET Blog
Viewing all articles
Browse latest Browse all 7144

Integrating with Slack Using ASP.NET WebHooks Preview

$
0
0

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:

SlackChannelMini

WebHook Configuration

To setup Slack WebHooks you need a Slack account. Then you configure the WebHook using what they call an Integration as follows:

SlackIntegrations

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

SlackWebHookConfiguration

Configuring Receiver

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:

   1:publicstaticclass WebApiConfig
   2: {
   3:publicstaticvoid Register(HttpConfiguration config)
   4:     {
   5:// Web API configuration and services
   6:  
   7:// Web API routes
   8:         config.MapHttpAttributeRoutes();
   9:  
  10:         config.Routes.MapHttpRoute(
  11:             name: "DefaultApi",
  12:             routeTemplate: "api/{controller}/{id}",
  13:             defaults: new { id = RouteParameter.Optional }
  14:         );
  15:  
  16:// Load Slack receiver
  17:         config.InitializeReceiveSlackWebHooks();
  18:     }
  19: }

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:

AzureAppSettings

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.

   1:publicclass SlackWebHookHandler : WebHookHandler
   2: {
   3:publicoverride Task ExecuteAsync(string generator, WebHookHandlerContext context)
   4:     {
   5:         NameValueCollection nvc;
   6:if (context.TryGetData<NameValueCollection>(out nvc))
   7:         {
   8:string question = nvc["subtext"];
   9:string msg = string.Format("The answer to '{0}' is '{1}'.", question, "Often");
  10:             SlackResponse reply = new SlackResponse(msg);
  11:             context.Response = context.Request.CreateResponse(reply);
  12:         }
  13:return Task.FromResult(true);
  14:     }
  15: }

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:

SlackChannel

This should hopefully be enough to get you started receiving WebHooks from Slack using ASP.NET WebHooks Preview.

Have fun!

Henrik


Viewing all articles
Browse latest Browse all 7144

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>