Make room for higher paying customers and crew in Slack
Slack channel overbooked? Make room for higher paying customers and crew in Slack.
© 2017 Jonathan Ballands
In the Slack channel you want to free up space in, invoke this slash command:
/united
This will call up the authorities to remove a random user from the Slack channel,
freeing up one spot. No need to feel bad about it! You’re just doing your job.
This repo is code for two AWS Lambda functions.
If you want to integrate this into your Slack team, follow the following steps:
The following diagram illustrates from a high-level what we are trying to accomplish:
united
will be a simple Slack app that utilizes a slash command. We start by
creating the app and setting the correct permissions.
Note
You won’t be able to useunited
unless you are in a private channel if you
are not an admin for your team. If you would like to use this in a public
channel, ask your admin to perform this step.
In the Slack API, choose “Your Apps”, then “Create
New App”. Name it something sensible (like “United”), choose a team, then choose
“Create App”.
In the side bar, choose “OAuth & Permissions”. Under “Permission Scopes”, select
the following permissions, then choose “Save Changes”:
(channels:read)
(channels:write)
(groups:read)
(groups:write)
The next step is to take the code in this repository and upload it to AWS Lambda.
We will create two lambda functions, one called united-alert
and one calledunited-kick
(but the names don’t exactly matter).
In AWS,
under “Compute”, choose “Lambda”.
Choose “Create a Lambda Function”. You will do the next steps twice, once for
each Lambda function:
In the side bar, choose “Configure function”. Name your function something
sensible (like united-alert
or united-kick
, as long as you can distinguish
between the alert Lambda and the kicking Lambda). Ensure the runtime is set to
“Node.js 6.10”.
Under “Lambda function code”, in the “Code entry type” dropdown, choose
“Upload a .ZIP file”. Choose “Upload”, and select the .zip file found in this
repo from the file picker. If you would like, you can zip the code yourself by
running the following Bash command within this repository:
$ yarn && zip -r ./united.zip *
Under “Lambda function handler and role”, choose an appropriate role that
has at least the following policies (if you don’t have any AWS IAM roles,
you’ll need to create one in AWS IAM that has these policies attached):
If you don’t have any AWS IAM roles, you’ll need to create one in AWS IAM that
has these policies attached.
Uncollapse “Advanced settings”. Under “Timeout”, if this Lambda will be
equivalent to united-alert
, choose “5 secs”, and if this Lambda will be
equivalent to united-kick
, choose “15 secs”.
Choose “Next”. Review your settings, then choose “Create function”.
When someone invokes /united
in Slack, Slack will send a POST request to
whatever backend you specify and expect a result. Here, we will use AWS API
Gateway to forward POST requests to your Lambda.
In AWS,
under “Application Services”, choose “API Gateway”.
Choose “Create API”. Name it something sensible that you can use for all of
your future Lambda microservices, then choose “Create API”.
Choose the root of the API. Create resources under the root until you have a
structure resembling /slack/united
:
/slack
resource, and create another resource called /united
.Choose the /united
resource, choose “Actions”, then “Create Method”.
Choose “POST” from the dropdown, then choose the tick.
Ensure “Integration type” is set to “Lambda Function”, and choose the region
your united-alert
Lambda function is located from the dropdown (it’s usuallyus-east-1
). A new text box will appear. Type the name of the united-alert
Lambda function, then choose “Save”. Your resource tree should look like this:
When Slack sends a POST request, it’s going to do so usingapplication/x-www-form-urlencoded
, which is something API Gateway can’t handle
out of the box. Let’s fix this:
Under “Content-Type”, choose “Add mapping template”. Paste the following
code into the text field (this will allow you to access the url encoded arguments
via postBody
in your Lambda function, where you can use snazzy Node libraries
to parse it):
{
"postBody" : $input.json("$")
}
Choose “Save”.
Choose the /
resource, then choose “Actions”, then choose “Deploy API”.
Under “Deployment stage”, choose “[New Stage]”. Name the new stage something
sensible (like “prod”), then choose “Deploy”.
You’ll be taken to the “Stages” tab in the side bar. Note the URL. This is the
URL you’ll use to hit your Lambda function.
Your access token is a special string of characters that allows access to your app
using the permissions we described above. In this step, we will configure an
environment variable for your Lambda functions that contain the encrypted access
token.
WARNING
Never share your access token with anyone. Never commit or hardcode access
tokens. Always encrypt access tokens and secrets, even ones stored in an
environment variable.
In the Slack API, choose “Slash Commands”. Choose
“Create New Command”.
Type “/united” for the new command. If you followed my steps perfectly, your
URL will be the base that was given to you at the last step of the last section,
plus /slack/united
. Adjust this to how you set it up.
Give a good description (I like “Make room in an overbooked Slack channel for
higher paying customers and crew.”), then choose “Save”.
In the side bar, choose “Install App”, then choose “Install App”. This will
perform the OAuth within Slack. Authorize the app.
You will be given an access token. Copy it to your clipboard.
In AWS, go
to AWS Lambda and for both Lambda functions:
Under the “Code” tab, under “Environment variables”, create a new variable
whose key is ACCESS_TOKEN
and whose value is your access token.
Tick the “Enable encryption helpers” box. Under “Encryption key”, choose an
appropriate KWS key, then choose “Encrypt”. If you don’t have a key, you will
need to access AWS IAM to make one.
If this is the united-alert
Lambda function, create a second variable
whose key is KICK_LAMBDA_NAME
and whose value is the ARN of your united-kick
Lambda function.
Choose “Save”.
You should now be able to use /united
in a Slack channel and watch it work.
Congrats! If it doesn’t work, here are some common reasons:
You didn’t encrypt your access token correctly.
You must encrypt your access token for both Lambda functions using a KWS key from
IAM.
You didn’t set up your API correctly in API Gateway.
Ensure the method for your endpoint is set to POST and that the you used the
mapping template described in this README.
Your Slack permissions aren’t correct.
Use the Slack permissions described in this README.
Your IAM role has the wrong policies attached.
Use only the policy templates described in this README.