There are a few nice guides on the internet describing how to create a Slash Command using serverless technologies.
My requirements were very specific. I was trying to create a Slash Command which replaced the original message (the one that triggered the bot), vs simply appending a new message to the chat log.
The guides linked to above, as much as they helped get 90% of it all working, didn't dig into the specifics of how to make that happen.
Therefore, here we are.
If you're trying to write your own Slash Command, then go ahead and follow one of the above guides, preferably the second one, then come back here.
After setting up your API Gateway POST endpoint as per the original guide, we'll need to update the Integration Request to proxy the full request to Lambda.
In Amazon's API Gateway, select your API, then Resources. Choose your POST endpoint, then click on Integration Request.
Check the box next to Use Lambda Proxy integration, then re-deploy that API!
Checking that Use Lambda Proxy integration box means that the full original request will be proxied from API Gateway to Lambda. Without it, API Gateway grabs the original request, handles it, and sends a different request (with stripped down data) over to Lambda.
That's not going to work for our use case. We need all the request data in order to correctly craft our responses back to Slack.
Slack asks us to perform a specific workflow during the request/response lifecycle in order to support the functionality we're looking for.
Specifically, we want to send a "delayed response":
As with immediate response messages, you can include the response_type field. However, when you use a value of in_channel with this delayed method, the original text that invoked the command is not included.
This isn't exactly intuitive, considering we just want to replace the original message, but it's just how Slack works ¯\_(ツ)_/¯
Here's what your Lambda code should look like:
This example code takes the user's message and applies the MOcKinG SpOnGeBOB MeME to the text. Character-casing logic happens in the spongebob function.
This guide is meant as a compliment to existing guides which go over the meat of setting up your serverless infrastructure, but don't go deep enough into the specifics of covering the use case of replacing the original message.
If you'd like to make a serverless Slack bot that responds to a slash command which replaces the original message text, it requires knowledge of some specific "gotchas" in the API Gateway, Lambda, and Slack stacks, which this guide covers.
Go forth and troll your teammates.
We are always interested in connecting with people who want to fund, innovate
or work in the open financial system.