Introduction to Bolt

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:

  1. It has a built-in web server.
  2. It helps manage authorization by handling OAuthOpen Authorization allows external entities to share your information without revealing your password..
  3. It has a clean and straightforward interface.
  4. 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:

  1. Get the application token.
  2. Enable socket mode.
  3. 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

  1. On the Slack API homepage, ensure your Slack application is selected, and then go to the “Basic Information” tab on the left-hand sidebar.
  2. Scroll down to the “App Credentials” section and copy the signing secret. Paste it into the widget below in the SIGNING_SECRET field.
  3. Scroll down to the “App-Level Tokens” section and click the “Generate Token and Scopes” button.
  4. You will be shown a pop-up. Add a name for the token in the name field, and add the connections:write scope.
  5. 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

  1. Go to the “Socket Mode” tab on the left-hand sidebar.
  2. By toggling the “Enable Socket Mode” section, we enable the socket mode.

Grant event permissions

  1. On the left-hand sidebar, go to the “Event Subscriptions” tab.
  2. Switch on the toggle for “Enable Events.”
  3. Open the dropdown for the “Subscribe to bot events.”
  4. Add the following events: message.channels, message.groups, message.mpim, and message.im.
  5. 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>!

The expected output in the Slack application
{
    "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"
    }
}
Test the configuration

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.