Introduction to Bolt
Explore and set up Slack's Bolt framework.
Overview
The Bolt framework from Slack allows us to focus less on the setup and more on the functionality of our application. The Bolt framework offers the following advantages:
- It has a built-in web server.
- It helps manage authorization by handling
.OAuth Open Authorization allows external entities to share your information without revealing your password. - It has a clean and straightforward interface.
- It offers built-in token validation and rate-limiting logic.
The framework allows a cleaner coding experience to build and maintain an application with less effort. Under the hood, the framework utilizes the same API we’ve been studying so far, so all the functionality we’ve looked at is implemented using the framework.
Set up the Bolt application
To set up the Bolt application, we need to follow the steps below:
- Get the application token.
- Enable socket mode.
- Grant the application event permissions.
Assumption: We’ll be using the same API token we generated at the beginning of the course.
Get the application token
- On the Slack API homepage, ensure your Slack application is selected, and then go to the “Basic Information” tab on the left-hand sidebar.
- Scroll down to the “App Credentials” section and copy the signing secret. Paste it into the widget below in the
SIGNING_SECRET
field. - Scroll down to the “App-Level Tokens” section and click the “Generate Token and Scopes” button.
- You will be shown a pop-up. Add a name for the token in the name field, and add the
connections:write
scope. - Click “Generate,” copy the generated token in the same pop-up, and paste it into the widget below in the
APP_TOKEN
field.
Enable socket mode
- Go to the “Socket Mode” tab on the left-hand sidebar.
- By toggling the “Enable Socket Mode” section, we enable the socket mode.
Grant event permissions
- On the left-hand sidebar, go to the “Event Subscriptions” tab.
- Switch on the toggle for “Enable Events.”
- Open the dropdown for the “Subscribe to bot events.”
- Add the following events:
message.channels
,message.groups
,message.mpim
, andmessage.im
. - Don’t forget to save the changes by clicking the “Save Changes.”
The executable code below helps us verify if everything works correctly.
Click the “Run” button to start the application. Once it’s running, you can go to Slack and send the message “hello” in a channel where the application is added. The application should respond to your message with Hey there @<your username>!
{ "name": "first-bolt-app", "version": "1.0.0", "description": "Introductory Bolt application", "main": "index.js", "scripts": { "start": "node app.js", "test": "echo \"No test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "@slack/bolt": "^3.12.1" } }
Let’s take a closer look at the code above:
- Lines 3–8: We initialize the app in socket mode, and provide the app and bot tokens and the signing secret.
- Line 11: We use the
message
method to create a listener for messages containing “hello.” - Line 13: We use the
say
method to send a message to the same channel where the “hello” message was sent. - Lines 16–20: We start our Bolt application.