3.17.1 • Published 1 day ago

@slack/bolt v3.17.1

Weekly downloads
16,197
License
MIT
Repository
github
Last release
1 day ago

Bolt Bolt logo for JavaScript

codecov Node.js CI

A JavaScript framework to build Slack apps in a flash with the latest platform features. Read the getting started guide to set-up and run your first Bolt app.

Read the documentation to explore the basic and advanced concepts of Bolt for JavaScript.

Setup

npm install @slack/bolt

Initialization

Create an app by calling the constructor, which is a top-level export.

const { App } = require('@slack/bolt');

const app = new App({
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  token: process.env.SLACK_BOT_TOKEN,
});

/* Add functionality here */

(async () => {
  // Start the app
  await app.start(process.env.PORT || 3000);

  console.log('⚡️ Bolt app is running!');
})();

⚙️ By default, Bolt listens to the /slack/events endpoint for all incoming requests (whether shortcuts, events, or actions payloads). When configuring Request URLs in your app configuration, they should all have /slack/events appended. The default behavior be modified using the constructor's endpoints parameter.

Listening for events

Apps typically react to a collection of incoming events, which can correspond Events API events, actions, shortcuts, slash commands or options requests. For each type of request, there's a method to build a listener function.

// Listen for an event from the Events API
app.event(eventType, fn);

// Convenience method to listen to only `message` events using a string or RegExp
app.message([pattern ,] fn);

// Listen for an action from a Block Kit element (buttons, select menus, date pickers, etc)
app.action(actionId, fn);

// Listen for dialog submissions
app.action({ callback_id: callbackId }, fn);

// Listen for a global or message shortcuts
app.shortcut(callbackId, fn);

// Listen for slash commands
app.command(commandName, fn);

// Listen for view_submission modal events
app.view(callbackId, fn);

// Listen for options requests (from select menus with an external data source)
app.options(actionId, fn);

Making things happen

Most of the app's functionality will be inside listener functions (the fn parameters above). These functions are called with a set of arguments.

ArgumentDescription
payloadContents of the incoming event. The payload structure depends on the listener. For example, for an Events API event, payload will be the event type structure. For a block action, it will be the action from within the actions array. The payload object is also accessible via the alias corresponding to the listener (message, event, action, shortcut, view, command, or options). For example, if you were building a message() listener, you could use the payload and message arguments interchangably. An easy way to understand what's in a payload is to log it, or use TypeScript.
sayFunction to send a message to the channel associated with the incoming event. This argument is only available when the listener is triggered for events that contain a channel_id (the most common being message events). say accepts simple strings (for plain-text messages) and objects (for messages containing blocks). say returns a promise that will resolve with a chat.postMessage response.
ackFunction that must be called to acknowledge that an incoming event was received by your app. ack exists for all actions, shortcuts, view, slash command and options requests. ack returns a promise that resolves when complete. Read more in Acknowledging events
clientWeb API client that uses the token associated with that event. For single-workspace installations, the token is provided to the constructor. For multi-workspace installations, the token is returned by the authorize function.
respondFunction that responds to an incoming event if it contains a response_url (shortcuts, actions, and slash commands). respond returns a promise that resolves with the results of responding using the response_url.
contextEvent context. This object contains data about the event and the app, such as the botId. Middleware can add additional context before the event is passed to listeners.
bodyObject that contains the entire body of the request (superset of payload). Some accessory data is only available outside of the payload (such as trigger_id and authorizations).

The arguments are grouped into properties of one object, so that it's easier to pick just the ones your listener needs (using object destructuring). Here is an example where the app sends a simple response, so there's no need for most of these arguments:

// Reverse all messages the app can hear
app.message(async ({ message, say }) => {
  const reversedText = [...message.text].reverse().join("");
  await say(reversedText);
});

Calling the Web API

In addition to the client property passed to listeners, each app has a top-level client that can be used to call methods. Unlike the client passed to listeners, the top-level client must be passed a token. Read the documentation for more details.

Acknowledging events

Some types of events need to be acknowledged in order to ensure a consistent user experience inside the Slack client (web, mobile, and desktop apps). This includes all action, shortcut, view, command, and options requests. Listeners for these events need to call the ack() function, which is passed in as an argument.

In general, the Slack platform expects an acknowledgement within 3 seconds, so listeners should call this function as soon as possible.

Depending on the type of incoming event a listener is meant for, ack() should be called with a parameter:

  • Block actions, global shortcuts, and message shortcuts: Call ack() with no parameters.

  • View submissions: Call ack() with no parameters or with a response action.

  • Options requests: Call ack() with an object containing the options for the user to see.

  • Legacy message button clicks, menu selections, and slash commands: Either call ack() with no parameters, a string to to update the message with a simple message, or an object to replace it with a complex message. Replacing the message to remove the interactive elements is a best practice for any action that should only be performed once.

  • Events API events do not need an ack() function since they are automatically acknowledged by your app.

Getting Help

The documentation has more information on basic and advanced concepts for Bolt for JavaScript.

If you otherwise get stuck, we're here to help. The following are the best ways to get assistance working through your issue:

  • Issue Tracker for questions, bug reports, feature requests, and general discussion related to Bolt for JavaScript. Try searching for an existing issue before creating a new one.
  • Email our developer support team: support@slack.com
page-metrics-root@matchesfashion/botifyhrbotklikni-jadi-api@acceleratxr/core@everything-registry/sub-chunk-842@hypedevs/reaction-projecte-bundleamplify-slack-app@7chairs/circles-shared@adamkac/nestjs-slack-bolt@alru/slack@torontopubliclibrary/slack-helper@seratch_/bolt-fastify@seratch_/bolt-http-runner@seratch_/bolt-koa@truesparrow/slackmin@sfindustriesdemo/slack-force-core@zalastax/nolb-_sla@bcpros/nestjs-slack-boltask-code-bot@cognosis/platform@composer-js/corebuild-servicedg-action-toolsdeesha-slacknode-red-contrib-chatbotnode-red-contrib-chatbot-bildicontentstacknestjs-boltnative-boltnestjs-slack-boltdiscord-slack-gatewaymy-friendly-slackbotpassport-botpm2-slack-pluspino-slack-boltwallflow@retsoksirhc/slackbot@slack-wrench/blocks-to-image@slack-wrench/bolt-storage-file@slack-wrench/jest-bolt-receiver@spotify/backstage-plugin-slack-node@stepsize/shared-types@stepsize/tywin@team-supercharge/jarvis-slack-notifier@tomcorp/tinybot@michelo11/botjs@contentstack/managementbolt-oauthrooibok@headless-architecture/node-red-contrib-slack-bolt@eventual/integrations-slackcodeceptjs-slack-reporterez-pr-lib@egorvas/node-red-contrib-chatboteli-corereshuffle-slack-connector@emitbase/emitbase-core@juicyllama/app-slackslack-block-kit-builderslack-bolt-fastifyslack-bolt-http-runnerslack-bolt-koaslack-karma-botslack-king-of-timeslack-notification0.1.0slack-qna-nodeslparsesmarter_ai_api@jobscale/slack@lskjs/bots-provider-slack@nadir2k/nestjs-slack-bolt@lightness/release-it-slack-notification-plugin@lonocloud/cljs-apistracking-shared@nivinjoseph/n-loginstazapinsta-redditinnapsisusername-mapper-slack-bot@plgworks/slackmin@qte/nest-slack-bolt
3.17.2-rc.1

1 day ago

3.17.1

3 months ago

3.17.0

4 months ago

3.15.0

5 months ago

3.13.2

9 months ago

3.13.3

9 months ago

3.14.0

7 months ago

3.16.0

5 months ago

3.13.1

12 months ago

3.13.0

1 year ago

4.0.0-nextGen.7

2 years ago

4.0.0-nextGen.8

2 years ago

4.0.0-nextGen.9

1 year ago

3.12.2

1 year ago

4.0.0-nextGen.6

2 years ago

4.0.0-nextGen.1

2 years ago

4.0.0-nextGen.2

2 years ago

4.0.0-nextGen.3

2 years ago

4.0.0-nextGen.4

2 years ago

4.0.0-nextGen.5

2 years ago

3.11.3

2 years ago

3.12.1

2 years ago

3.12.0

2 years ago

3.11.2

2 years ago

3.11.1

2 years ago

3.11.0

2 years ago

3.9.0

2 years ago

3.10.0

2 years ago

3.8.0

2 years ago

3.8.1

2 years ago

3.7.0

3 years ago

3.6.0

3 years ago

3.5.0

3 years ago

3.4.1

3 years ago

3.4.0

3 years ago

3.4.0-rc.0

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.7.0

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.3

4 years ago

2.2.1

4 years ago

2.2.2

4 years ago

2.2.0

4 years ago

1.8.1

4 years ago

2.1.1-beta.2

4 years ago

2.1.1-beta.1

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago