Serverless Slack Chatbot
I’ve been the mode of exploring bot framework and found that slack has a cool node module that supports a lot of native main features.
Let’s get our hands dirty!!
Creating an app on slack
- Go to the slack app https://api.slack.com/apps
- Create a slack app and choose where you want to keep your app in the workspace then click Create App
3. Copy App Credentials to use on the backend
Creating a backend slack app using Serverless framework
- Install Serverless framework on your local machine
npm install serverless -g
2. Create a new project called serverless-slack-chatbot then you will get a message on your terminal that your project is created.
serverless create --template aws-nodejs --path serverless-slack-chatbot
3. run the command below to init the project
npm init
4. Install the slack module as command below
npm i @slack/bolt @slack/web-api @slack/webhook serverless-http
5. Clone a project to your local computer
6. Rename .env.example to .env and replace Slack app credential from your slack app
SLACK_CLIENT_ID=abc
SLACK_API_TOKEN=abc
SLACK_CLIENT_SECRET=abc
SLACK_BOT_TOKEN=xoxb-
SLACK_SIGNING_SECRET=abc
7. let’s deploy our backend
sls deploy -v
8. After deployed your backend then you will get an endpoint as image below. We will use this to config our slack app.
Config the slack app
- Go to Event Subscriptions and turn it on then pass endpoint on Request URL. In this case, we are using an endpoint that end with slack/events. if everything is right, you should get a green text verified.
2. Adding message.channels & message.im on the subscribe to bot events section and then click save button.
3. Go to OAuth & Permissions page and add chat:write to Bot Token Scopes. After that slack will notify you to reinstall app again.
4. Install our app by go to Basic Information page then click Install your app to workspace.
5. Update our OAuth Access Token & Bot User OAuth Access Token on serverless.yml file then redeploy backend again.
6. Our bot is live!!
Slash commands
- Go to slash commands and click create a new command
- add command that you’d like to use, request url is the same as event subscritpions and short description.
- hit save button and reinstall your app
4. Let’s try our slash command by type /echo how are you?
Check out the code here and give me a clap if you’d like this article.
Thank you for reading and leave me a comment if you have any questions.