I have a Slack account setup with some friends of mine, and we created a #stayfit room to keep each other motivated.
I thought it would be a cool idea to create a bot for the room where we could post our workouts and it would keep track of it. Since it’s going to be a very low volume task, it’s perfect for pay-by-the-request Amazon Web Services.
Here’s the end result:
This code is up on Github here: https://github.com/michaelcox/slack-workouts
There are 4 parts you need to have in place for the full system to work:
- A new Lambda function with your application logic
- An API endpoint using AWS API Gateway - this will point to your Lambda function
- A new blank DynamoDB database. It’s schemaless, so you don’t need to define your data structure, but you do need to define the container
- A custom Slash Command at Slack to POST to your API endpoint
AWS actually gives you a nice template to start from, which helps because the authentication process to get a
kmsEncyptedToken created isn’t terribly straight forward. I won’t copy the instructions here, since they may continue to get updated. Instead, just follow these steps:
- Login to the AWS Management Console
- Click Lambda under the Compute section
- Click Create a Lambda Function
- Search for “slack” from the set of templates and select “slack-echo-command”
- Follow the instructions in the comments to setup your initial test
In Lambda, you define a single function that will execute first, by default named
handler in the Lambda configuration page (but can be named whatever you would like) and needs to be exported with node’s normal export process. Here is the main function you’ll want for this integration, since up front you need to validate the API token sent from Slack before processing the event.
You can then parse the body off the event that is passed. Here I’m using qs to parse the body.
When you have your completed app, zip all the files and upload them to your Lambda function. Remember that you need to include the entire node_modules folder in your upload. Lambda will not do an
npm install for you.