Custom Slash Commands in Mattermost with AWS Lambda and API Gateway

Thu, Aug 24, 2017

Mattermost comes with a number of built-in slash commands, such as /join to join a channel, or /away to set your status to away. It also supports Slack-compatible custom slash commands. These offer a simple way to add custom commands that help to make your team’s workflow fast, efficient and enjoyable.

Custom slash commands are actually very easy to build. You just need to set up an HTTP endpoint which will receive the command payload, processes it and respond with the message which will be posted. Then add the custom slash command to the server and point it at your HTTP endpoint and you are done.

The only inconvenient part of building a custom slash command is making the HTTP endpoint available. The obvious way to solve this is to set up a webserver somewhere to host it, but this is a lot of overhead for an endpoint which might only triggered a few times a day. However, this is somewhere where serverless execution environments like AWS Lambda really shine, making it simple to set up, without needing a web server, and costing a few fractions of nothing for a lightly used endpoint.

In this post, we will walk through setting up a Mattermost Custom Slash Command hosted as an AWS Lambda function in combination with an AWS API Gateway endpoint.

The Lambda function we write is going to be written in Python 3, although you can use any of the other supported languages if you prefer. As an example, we’re going to use the /standup command the Mattermost Core Team use to post our progress update in our channels each day. If you’d like to see what this command looks like now, jump ahead to the [See it in Action] section below.

Setup in AWS

We’ll start by setting up the Lambda function and API Gateway endpoint in AWS.

Log in to the AWS console and go to the Lambda section, and click on Create a Lambda function.

On the Select blueprint page, click the Author from Scratch button.

On the next screen, select API Gateway as the trigger.

Fill in the new set of settings which appear:

Pick an API Name (e.g. MattermostSlashCommands)

You can leave the deployment stage as prod.

Select Open for the security. This means your API can be invoked over HTTP without any credentials.

On the next page, fill in these settings too:

Provide a name for your function. We’ll use SlashStandup.

Choose Python 3.6 for the runtime.

Select Edit code inline.

For the function code, copy paste the code in the [Implementing our Slash Command] section.

For the Lambda Function Handler, create a new role from template, and give it a name, e.g. runLambdaFunctionsRole.

For RAM and Timeout, select 128mb and 30s respectively.

Advance to the review page, and click Create Function.

On the success page, you should see the URL for your new API endpoint.

Implementing our Slash Command

The full Python 3.6 code for the /standup Custom Slash Command is as follows:

Next, we check that the token parameter in the request payload matches the one we have hardcoded in the code. By hardcoding the token provided by Mattermost when setting up the custom slash command, we can ensure that our API endpoint rejects any requests that do not come from our Mattermost server, returning a HTTP Unauthorized response in this case.

Finally, we build the response object to send back to the Mattermost server in the JSON format it expects, setting the response_type to in_channel so the message appears in the channel for everyone to see. Then we dump it to a string and return the response object for API Gateway to process and return to our Mattermost server.

Configuring Mattermost

The final part of the process is to add a new Custom Slash Command in Mattermost. Depending on the policy settings on your Mattermost server, you might need to be a System or Team Administrator to do this.

In the Mattermost web app main menu, select Integrations, then Slash Command then Add Slash Command.

Fill in the form as per the screenshot below (making any changes you would like). The most important are:

The Command Trigger Word which dictates what /word you use to trigger invoke the slash command.

The Request URL, which should be the URL for the API endpoint on AWS API Gateway/Lambda we created earlier.

The Request Method, which should be POST.

Once that form is filled in, click Add and you you will see the Token for the slash command. Use this in the Lambda function code described earlier to ensure that only your Mattermost server can call the lambda function.

See it in Action

We’ve built the Slash command on AWS, and added it in Mattermost. Now all that remains is to try it out.

Start by typing /standup into the message box of Mattermost:

Finish typing the command name, or click on the autocomplete suggestion to finish it for you. Then type in the rest of your status update:

When you are done, press return, and if everything is set up correctly, you’ll see your status update post appear in the channel:

And that’s it really. You can customise the slash command to do whatever you want by changing this code. You can see a list of parameters that are sent by the Mattermost server when calling your API endpoint as well as a list of parameters it accepts in the response for more inspiration in the Mattermost custom slash command documentation.