Developer, tech enthusiast, and gamer.

VP of Technology, Authenticom

Fun with Slack Commands using Azure API and Function Apps

Fun with Slack Commands using Azure API and Function Apps

Let me start by saying that there are many different ways to accomplish the task of consuming and sending Slack webhooks. However, since I've used and written about using azure mobile services in the past to do just that, I figured it was time to update those using the latest round of Azure services. In this example, I'm using an Azure API app with Easy API's enabled (formerly mobile services...but better) and the preview version of the Azure Function app. My original intention was to use the Function for both ends, but given it's preview nature, I ran into quite a few issues trying to work with Service Bus.

Create a new Slash Command

Under custom integration in your Slack apps directory, you can Create a new Slack Command. You simply give it a name like /whatsuponlunch and then you have a shiny new command you can use and pass in a plain text argument. For example /whatsuponlunch find me tacos would trigger your hard working process for finding a taco place within a 4 block radius.

Consume the command payload in your API App

In your Azure API app, just create a new 'Easy API' called lunchcommand and make sure it can accept public POST requests. Once that's done, you'll need to make a few more tweaks to the default Express setup so that the request.body of that POST request can be properly understood. You need to install a couple of packages and add a few lines to the app.js so that the request.body changes from undefined to the payload you expect from the command.

Now the Express api is ready to parse out the form urlencoded POST that Slack will send with each command execution. Setting up the Express middleware was new to me, but this exposed configuration is a welcome change from the previous iteration of Node Mobile Services.

This next bit of code processes the payload and sends a message to an Azure Service Bus Queue. After the brokered message is sent, it then responds with a message for the user who initiated the request.

Use a Function to process the message and respond to the user

Azure Functions are in preview, which means new features are coming all of the time and use them at your own risk. I found this out first hand when testing some of the features myself. What I did have some success with, was creating a new C# function that was triggered to run when a new message was received in a Service Bus queue. So each time the above api generates a new message, this function executes. The Portal wizard is the fastest way to create the function, but you can also do everything through code by following the developer reference documentation (shown below).

Now that the Function and trigger are set up, let's get into the function setup. First, you'll need to ensure that your nuget dependencies are installed and available to the function. You'll need to add a project.json file in your function subdirectory to accomplish this...it should look very familiar if you played with the new project system shipping with .NET Core. Best way add that quickly is to fire up Visual Studio Online from the Portal.

You've got your packages installed, now for the code. It starts by parsing the Service Bus message body, then doing some longer running work, and finally sending the results back to slack via the private webhook link from the message.

The user just got a private message in the channel they initiated the command from with all of that useful information. See, I told you it was fun! All this without a server deployed, a service scaled, or even a desktop code editor opened.

Leveraging technology to improve your people management

Leveraging technology to improve your people management

My takeaways from BUILD 2016

My takeaways from BUILD 2016