Building a Slack Slash Command with Azure Logic Apps
Have you used Slack slash commands like this…
…and wanted to build your own?
If so, this article will show you how to create a custom Slack slash command by utilizing Azure Logic Apps.
Documentation followed: https://api.slack.com/interactivity/slash-commands
Part 1: Create Your Slack Application
Step 1: Go to https://api.slack.com/apps (you may have to select your workspace if not already in it).
Step 2: Create new app.
It will give you a couple options for how to create your app. I typically choose the From scratch option.
Give your app a name, choose the workspace for it, and then click Create App.
You will come back to this Slack App later.
Part 2: Create Your Azure Logic App
Step 1: Create new Logic App.
Navigate to the Azure Portal, and search for Logic Apps. Select Add to create a new one, and complete all the settings, like Subscription, Resource Group, Logic App name, etc. How you configure the settings will depend on your organization.
Step 2: Navigate to your newly created Logic App, and open up the Logic App Designer from the left menu.
Step 3: Select how you would like to start building your Logic App.
I typically choose Blank Logic App to start from scratch, but you can also choose When a HTTP request is received to start off with the trigger that you will be using. In this example, I selected Blank Logic App, but starting with selecting the trigger would save you an extra step.
With the blank template created, search for Request in the Search connectors and triggers bar. Choose Request > When a HTTP request is received. Click Save on your Logic App, and you should now have a URL generated where your requests can be sent to trigger the Logic App.
Step 4: Copy the HTTP POST URL in your HTTP trigger as you will need it for your slash command settings.
Part 3: Create Your Slack Slash Command
Step 1: From the main screen of your Slack app you just created, you may see two options for how to navigate to slash commands. Either should work, although I typically choose it from the menu on the left.
You should now see options for Slash Commands. Choose Create New Command.
Step 2: Fill out the information about the Slash command.
- *Command: Pick your command name carefully
- Request URL: the URL that was generated in the HTTP Trigger of your Logic App that you copied in Part 2, Step 4
- Short Description: the short description that pops up in Slack when you see the slash command
- Usage Hint: the popup that lets you know how to use the slash command, including which parameter(s) to pass
*Additional note about command: Slack documentation has this to say about naming your slash command:
Consider your command’s name carefully. Slash Commands are not namespaced.
This means multiple commands may occupy the same name. If it happens and a user tries to invoke the command, Slack will always invoke the one that was installed most recently. It’s an important thing to consider especially if you’re planning to distribute your app.
So when you’re picking a command name, you will want to avoid terms that are generic and therefore likely to be duplicated. On the other hand, you don’t want the command to be too complicated for users to easily remember.
In essence, a great command is descriptive and simple but also unique. Naming it after your service is often a good idea.
Once you’ve created a truly unforgettable command, any channel or workspace where your app is installed will immediately be able to start using it, so let’s learn what to do when a user types one of your app’s commands.
In this example, I am creating a basic command that refreshes a Power BI dataset.
Step 3: Install your Slack app to your workspace.
This step is what will allow your slash command to become available in your organization’s workspace. This is found under Basic Information > Install your app > Install to Workspace.
Step 4: You can now see that your slash command is available in any channel in your workspace
Part 4: Build Out the Rest of the Azure Logic App
Step 1: Review the incoming data in the Logic App.
First, run the slash command to trigger your Logic App:
You can see the run by navigating to your Logic App in the Azure Portal, going to Overview > Runs History and selecting the latest run. If you click into the When HTTP request is received trigger, you will see the JSON output from the incoming request. It looks something like this (I’ve redacted the information from my example, but yours should have actual values instead of redacted):
{
"$content-type": "application/x-www-form-urlencoded",
"$content": "redacted",
"$formdata": [
{
"key": "token",
"value": "redacted"
},
{
"key": "team_id",
"value": "redacted"
},
{
"key": "team_domain",
"value": "redacted"
},
{
"key": "channel_id",
"value": "redacted"
},
{
"key": "channel_name",
"value": "redacted"
},
{
"key": "user_id",
"value": "redacted"
},
{
"key": "user_name",
"value": "redacted"
},
{
"key": "command",
"value": "/refresh_dataset"
},
{
"key": "text",
"value": "TestDataset"
},
{
"key": "api_app_id",
"value": "redacted"
},
{
"key": "is_enterprise_install",
"value": "false"
},
{
"key": "response_url",
"value": "redacted"
},
{
"key": "trigger_id",
"value": "redacted"
}
]
}
As you can see, your trigger output includes information about the slash command: the user who ran it, the channel it came from, the text that accompanied the command, and several other fields.
Step 2: Send back response to Slack API.
Slack’s documentation says that a response must be sent back within 3000 milliseconds (3 seconds) or the command will fail:
Your app can do this simply by sending back an empty HTTP 200 response to the original request.
If you don’t do this, the user will be shown an error message that indicates that the slash command didn’t work — not a great experience for the user, so you should always acknowledge receipt (unless of course you didn’t receive the command, but then you wouldn’t know not to respond, and now we’ve fallen into a logical paradox).
This confirmation must be received by Slack within 3000 milliseconds of the original request being sent, otherwise a
Timeout was reached
will be displayed to the user. If you couldn't verify the request payload, your app should return an error instead and ignore the request.
You can accomplish this by returning to the Logic App Designer view, and selecting Add New Step under the HTTP trigger. Search for Response, and choose the Response Request action. Here, you will simply make sure you are returning a 200 response immediately to the HTTP trigger, ensuring your response is made within the allotted three seconds.
Step 3: Validate the request.
One of the drawbacks of these slash commands is that they are enabled for the whole workspace. So any user in any channel could run the command. That may be the behavior you prefer, but in certain cases, you are probably going to want some sort of validation that the request came from an approved user in an approved channel. For this example, I’ll show channel validation.
Add a new step, and search for Initialize Variable. Here is where you will set the acceptable Channel IDs where this slash command can be run. I named mine Valid Channels, chose Array as the type, and for the value, I selected Add dynamic content, and used the createArray function to set multiple values. (Note: you can find the Channel ID of your Slack channels in the About section of the channel.)
Add another new step, and search for Control. Select Control > Condition to select the Condition action that operates similar to an IF > AND/OR > ELSE function.
For the first part of your condition, you want to reference the array variable just created by selecting Add dynamic content and choosing the Valid Channels variable.
The operator you choose should be contains, because you are checking if your Valid Channels array contains the channel_id from the request. Then for the last value, you want to reference the channel_id from our incoming HTTP request. This value can be extracted by using another custom function in Logic Apps Expressions.
You can use the triggerFormDataValue function, and pass in the key name from the HTTP trigger output in Part 4, Step 1. This function extracts the value from the $formdata field in the HTTP trigger output, which will be useful to you in building out more robust solutions and referencing more of the fields in the incoming request.
Step 4: Refresh the dataset.
Your condition action in the previous step gave TRUE and FALSE options. You can add actions to these, depending on if the request came from a valid channel or not.
In the TRUE condition, add an action and search for Refresh a dataset. You should then be able to select the Power BI: Refresh a dataset action. Here you can choose the workspace and then add dynamic content to use the dataset name that you passed in the slash command. You can use the same triggerFormDataValue method, but using the text field this time instead of channel_id like in Step 3. The text field contains the value you passed in Slack after your slash command.
Step 5: That’s it!
You would probably want to send back a message to the channel letting the user know that the refresh began, or send a message back in the False condition if the request came from an invalid channel. The URL to send messages back to the user who initiated the slash command is found in the response_url value in the HTTP trigger output. Just like in previous steps, that URL can be retrieved using the triggerFormDataValue function.
There is much more that can be done with Slack slash commands and Azure Logic Apps. As mentioned earlier, you will probably want to do more validation than just channel validation. Also, there are many more connectors and actions in Logic Apps than just refreshing a Power BI dataset, so there are many more possibilities and automation you can accomplish with this type of workflow.
My goal for this was to show you how to set it up and get it working, and then you can use your creativity to build a solution that will fit your needs.
Thanks for reading!