Bolt for JavaScript
repository·main·Indexed 25 days ago
https://github.com/slackapi/bolt-jsA framework for building Slack apps quickly using the latest Slack platform features, including Block Kit, Events API, and Socket Mode. The @slack/bolt package (version 5.0.0) provides tools for implementing custom receivers for 3rd party web frameworks, deploying to AWS Lambda and Heroku, and managing OAuth flows and Message Metadata events.
What's inside bolt-js
- You can find pre-built apps that are ready for use with AI features in the Slack Marketplace.
Configure environment variables for OAuth
mainThe OAuth Express Receiver example requires several environment variables. You can retrieve most of these from your Slack app configuration.
SLACK_STATE_SECRETis a custom secret of your choosing used for state validation.export SLACK_CLIENT_ID=YOUR_SLACK_CLIENT_ID export SLACK_CLIENT_SECRET=YOUR_SLACK_CLIENT_SECRET export SLACK_SIGNING_SECRET=YOUR_SLACK_SIGNING_SECRET export SLACK_STATE_SECRET=YOUR_SLACK_STATE_SECRETEnable automatic token rotation with built-in OAuth
mainAs of v3.5.0, Bolt for JavaScript supports token rotation following the OAuth V2 RFC. When enabled, access tokens expire and are replaced by new ones using a long-lived refresh token.
If you use the built-in OAuth functionality in Bolt, the framework will automatically rotate tokens in response to incoming events.
Enable Interactivity for Buttons and Modals
mainTo use interactive features like buttons, select menus, date pickers, or modals, you must enable interactivity in your Slack App settings.
For Socket Mode
Interactivity is enabled by default. No additional configuration is required in the Slack App dashboard.
For HTTP Mode
- Navigate to your app's settings page in Slack.
- Click on Interactivity & Shortcuts in the left sidebar.
- In the Request URL box, enter the same URL you use for events (e.g.,
https://your-domain.com/slack/events). - Click Save Changes.
Get started with Bolt for JavaScript
mainBolt for JavaScript is a framework designed to build Slack apps using the latest Slack platform features. To set up and run your first application, follow the Quickstart Guide.Call the Web API using the listener client
mainTo call Slack Web API methods within a listener function, use the
clientobject provided in the listener's arguments. Thisclientis an instance ofWebClientthat is automatically configured with the appropriate token:- The token specified during Bolt app initialization.
- Or, the token returned from the
authorizefunction during an OAuth flow (handled automatically by Bolt's built-in OAuth support).
Using this
clientensures that the API call is made with the correct context for the specific request being handled.Add custom HTTP routes to the App
mainSince
v3.7.0, you can add custom HTTP routes by passing acustomRoutesarray to theAppconstructor. Each object in the array must include:path: The URL path for the route.method: A string or an array of strings representing the HTTP methods (e.g.,'GET',['POST', 'PUT']).handler: A function to handle the request, receivingreqandresobjects.
Starting from
v3.13.0, the defaultHTTPReceiverandSocketModeReceiversupport dynamic route parameters (e.g.,/music/:genre), allowing you to access URL values viareq.params.You can specify the port for these routes using
installerOptions.portin theAppconstructor. The default port is3000.const { App } = require('@slack/bolt'); // Initialize the Bolt app with custom routes const app = new App({ token: process.env.SLACK_BOT_TOKEN, signingSecret: process.env.SLACK_SIGNING_SECRET, customRoutes: [ { path: '/health-check', method: ['GET'], handler: (req, res) => { res.writeHead(200); res.end(`Things are going just fine at ${req.headers.host}!`); }, }, { path: '/music/:genre', method: ['GET'], handler: (req, res) => { res.writeHead(200); res.end(`Oh? ${req.params.genre}? That slaps!`); }, }, ], installerOptions: { port: 3001, }, }); (async () => { await app.start(); app.logger.info('⚡️ Bolt app started'); })();Configure Bot Token Scopes and Install App
mainTo allow your app to perform actions (like posting messages), you must configure OAuth scopes and install the app to your workspace.
- In App Settings, go to OAuth & Permissions.
- Under Bot Token Scopes, click Add an OAuth Scope.
- Add the
chat:writescope to allow the app to post messages. - Scroll to the top and click Install to Team.
- Authorize the installation in the Slack OAuth UI.
- Copy the generated Bot User OAuth Access Token (starts with
xoxb).
Security Note: Never check tokens into version control. Use environment variables instead.
$ export SLACK_BOT_TOKEN=xoxb-<your-bot-token>Implement OAuth flow in Bolt for JavaScript
mainTo distribute a Slack app, you must implement the OAuth flow. Bolt handles routing,
stateparameter validation, and passing installation information to your app.To enable OAuth, you must provide:
clientId,clientSecret,stateSecret,scopes(Required)installationStore(Highly recommended for production to save and retrieve installation information)
For development and testing, you can use
FileInstallationStorefrom@slack/oauth, but it is not recommended for production. In production, you should implement a custominstallationStoreusing a real database.const { App } = require('@slack/bolt'); const { FileInstallationStore } = require('@slack/oauth'); const app = new App({ signingSecret: process.env.SLACK_SIGNING_SECRET, clientId: process.env.SLACK_CLIENT_ID, clientSecret: process.env.SLACK_CLIENT_SECRET, stateSecret: process.env.SLACK_STATE_SECRET, scopes: ['channels:history', 'chat:write', 'commands'], installationStore: new FileInstallationStore(), });Subscribe to message events in Slack
mainTo receive messages, you must configure your app in the Slack app settings:
- Go to Event Subscriptions and ensure Enable Events is toggled on.
- Under Subscribe to Bot Events, select the relevant message events:
message.channels: Messages in public channels.message.groups: Messages in private channels.message.im: Direct messages with the app.message.mpim: Messages in multi-person direct messages.
- Click Save Changes.
- Important: You must reinstall the app to your workspace to apply the new scopes.
Add routes using ExpressReceiver.router
mainIf you are using the built-in
ExpressReceiver, you can add custom routes or middleware by accessing therouterproperty. Thereceiver.routerexposes the internal ExpressRouterinstance used by theApp, allowing you to use standard Express methods like.use(),.get(),.post(), etc., to handle web requests outside of Slack's event system.const { App, ExpressReceiver } = require('@slack/bolt'); // Explicitly create the Bolt Receiver const receiver = new ExpressReceiver({ signingSecret: process.env.SLACK_SIGNING_SECRET }); // Create the App using this receiver const app = new App({ token: process.env.SLACK_BOT_TOKEN, receiver }); // Define Slack-specific logic using App methods app.event('message', async ({ event, client }) => { // Do some slack-specific stuff here await client.chat.postMessage(...); }); // Use receiver.router to add middleware receiver.router.use((req, res, next) => { app.logger.info(`Request time: ${Date.now()}`); next(); }); // Use receiver.router to define other web request handlers receiver.router.post('/secret-page', (req, res) => { // Handle standard Express request and response res.send('yay!'); }); (async () => { await app.start(); app.logger.info('⚡️ Bolt app started'); })();Send simple messages with say()
mainWithin a listener function (like
app.messageorapp.event), you can use thesay()utility to post a message to the conversation that triggered the listener.say()accepts a string for simple text messages.If you need to send a message outside of a listener or require advanced error handling, use the
chat.postMessagemethod via the client attached to your Bolt instance.// Listens for messages containing "knock knock" and responds with an italicized "who's there?" app.message('knock knock', async ({ message, say }) => { await say(`_Who's there?_`); });