Build a Serverless Slackbot with Vercel and Upstash Redis
Slackbots are awesome tools if you use Slack and want to automate some tasks or ease your workflow. However, managing your own server might be a bit of an overhead. That is why, we created this tutorial, enabling for serverless deployment of slackbots.
What We Are Building
We are building a Note Taker slackbot. It will enable the users to:
- Set key-value pairs.
- Create lists with adding to or removing/fetching from features.
Also:
- When mentioned, it mentions the user back in a general channel.
- Posts to the general channel once some new channel is created.
An example use case could be setting some index values for individual tasks or creating To-Do lists or task distribution for different team members, etc.
Getting Started
Some Conventions
- Currently, Vercel supports node v12 and v14. Before starting development, it may be a good idea to switch your node version to 14, especially for testing and local development.
- Since our files will act as api endpoints, all the files mentioned below should be in a directory called
api
. - For deployment in Vercel, there is a naming convention for files:
- File names starting with
_
will not be converted to endpoints.
I.E.
challenge.js
will result in an endpoint /api/challenge. If you don't want that, create the file as_challenge.js
Prepare the Database
We can create our Redis database on Upstash Console. Copy/paste the UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN to the .env
file:
Starting Development
First of all, npm install vercel axios crypto
to install dependencies and Vercel CLI.
Create helper files:
-
Create
_utils.js
:
These will help us customize and ease the management of our slackbot.
-
Create
_validate.js
:
We want to make sure that the requests that are coming to our server comes from Slack itself. For that, we will use some hashing to see whether the request is valid.
-
Create
_constants.js
:
We don't want to hardcode our sensitive data.
token
andsigningSecret
will be given after slack configuration.
Handling Slash Commands
Create a file named note.js
with the code:
Simply tokenize the coming request from the slash commands. Depending on the command, direct the request to relevant handlers.
Create a directory named slash_handlers
and inside:
_set_key.js
:
Using Upstash RESTFUL API, we can simply send a request without the need to create a client to connect to our database. This is one of the strengths of Upstash Redis.
Handling Events
Create a file named events.js
with the code:
Slack sends a challenge for verification. If this is the case, direct request to that handler. Otherwise, check validity of the request and then direct it to relevant handler functions.
Create a directory named events_handlers
and inside:
_challenge.js
:
Simply send back the challenge for Slack verification.
_app_mention.js
:
When our bot is mentioned, this function will be triggered. Simply post a greeting message to any channel with any text you like.
-
Create other handler js files similar to
_app_mention.js
. (For reference, you can go to Github Repo Of the Project) _channel_created.js
After all is done
Folder structure
Your folder structure should look something like this:
Running Locally
Vercel has a CLI that lets you simulate deployment on your local environment. To do so:
If you don't have a static IP address, then you should use a tunnelling service such as ngrok
so that you can show your endpoint to Slack:
./ngrok http 3000
--> Tunnels your localhost:3000
Configure Slack
1. Go to Slack API Apps Page:
- Create new App
- From Scratch
- Name your app & pick a workspace
- Go to Oauth & Permissions
- Add the following scopes
- app_mentions:read
- channels:read
- chat:write
- chat:write.public
- commands
- Install App to workspace
- Basic Information --> Install Your App --> Install To Workspace
- Add the following scopes
2. Note the variables:
SLACK_SIGNING_SECRET
:- Go to Basic Information
- App Credentials --> Signing Secret
- Go to Basic Information
SLACK_BOT_TOKEN
:- Go to OAuth & Permissions
- Bot User OAuth Token
- Go to OAuth & Permissions
3. Go to Slack API Apps Page and choose relevant app:
After deployment, you can use vercel_domain
or ngrok_domain
as <domain>
.
- Go to Slash Commands:
- Create New Command:
- Command :
note
- Request URL :
<domain>/api/note
- Configure the rest however you like.
- Command :
- Create New Command:
- Go to Event Subscribtions:
- Enable Events:
- Request URL:
<domain>/api/events
- Request URL:
- Subscribe to bot events by adding:
- app_mention
- channel_created
- Enable Events:
- After these changes, Slack may require reinstalling of the app.
Congratulations!
You now have a functioning serverless Slackbot! Feel free to customize it however you like.
After you are satisfied with the results, simply:
vercel deploy
for testing on Vercel servers before deployment.vercel --prod
for final deployment on Vercel servers.
You can now use the domains provided from Vercel on your Slack configurations.