Skip to main content

Durée estimée

Thèmes

Demander à la communauté

Build and Deploy Your Bolt App

Get to Know Listener Functions

You created your project. Now, get it to listen to messages and respond. Slack apps typically receive and respond to 1:n (one to many) requests from Slack. For each type of incoming request from Slack, there is a corresponding listener function to handle and respond. 

Here is a subset of the listeners Bolt apps can pass a function to.

Listener

Description

app.event(eventType, fn)

Listens for Events API events. The eventType is a string used to identify the specific event.

app.message([pattern ,], fn)

Convenience listener to handle events of type message. The pattern can be any substring or RegExp expression.

app.action(actionId, fn)

Listens for interactive events from a Block element such as a user interaction with a button. The actionId identifier is a string that matches a block element’s action_id.

app.shortcut(callbackId, fn)

Listens for global and message shortcut invocations. The callbackId is a string or RegExp pattern that matches a shortcut’s callback_id, which is specified in the app configuration.

The full list can be found in the Bolt for JavaScript reference documentation. It’s time to configure your app to listen and respond to events, interactions, and interactivities!

Add an Events Listener

Event subscriptions use the event() listener, which you can find examples of in the Bolt documentation. For now, you use the message() listener to listen and respond to messages.

  1. In the say-hello folder on your computer, open the app.js file in your preferred text editor.
  2. Replace all the code starting from the line that begins with //Listens to incoming messages that contain “hello” with the following. To quickly copy the code to your clipboard, you can click Copy at the top right of the code block. Make sure you save your work after.
// Listens to incoming messages that contain “hello”
app.message(/hello/i, async ({ message, say }) => {
  //say() sends a message to the channel where the event was triggered.
  await say(`Hey there <@${message.user}>`);
});


(async () => {
  // Global middleware to log all incoming requests once the app starts
  app.use(async ({ body, next }) => {
    console.log("⚡️ Received request:", body); //Log app status and the incoming request
    await next(); //Pass to next middleware or listener
  });


  // Start your app
  await app.start(process.env.PORT || 3000);


  app.logger.info("⚡️ Bolt app is running!");
})();

Take a look at what you did. You entered a message() listener that’s looking for the word Hello (1). This is formatted as /hello/i so the user can enter the word in various ways—“Hello”, “hello”, or even “HeLlO”—and the app will respond. The say() parameter exposes the event’s response URL (2) which, unlike calling chat.postMessage(), doesn’t require additional scopes to reply to message events.

You also strengthened your logs with a middleware that lets you know a request was received (the listener heard Hello) (3). It then passes the message on to your final log letting you know that the app is functioning properly (4). This helps you monitor your app and troubleshoot any issues that may occur as you’re building and testing.

code inputted just under //Space for your code with numbers next to the app.message listener, say() parameter, and console.log’s as indicated above.

Deploy Say Hello to Slack

Time to deploy your app via the Slack CLI. You already authenticated the Slack CLI to your Trailhead Slack playground. So it knows exactly where to send the app.

  1. Open Terminal via your applications folder or using command + space.
  2. Move into your project. Enter cd say-hello.
  3. Start hosting. Enter slack run.
  4. The Slack CLI walks you through the process of creating the new Slack app, confirming the team and workspace to which it’s deployed. Confirm each option.
    • Select an app, Create a new app: press return.
    • Choose a team, (your team and workspace): press return.
    • Choose a workspace to grant access, make sure All of them is selected: press return.

Terminal command and response history as mentioned above, with the result bolt-app :lightningbolt: Bolt app is running!

The Slack CLI confirms your app has been deployed and it’s up and running. You just need to approve it in Slack and add it to the right channel.

Add the App to the Right Channel and Test

Now that the app is active in your Slack playground, add it to the onboarding channel.

  1. Head back to your Trailhead Slack playground.
  2. Click on #welcome-new-team-members.
  3. @mention say-hello (local) and press return. Slackbot will notify you that you mentioned the app, but it hasn’t been added to the channel yet. It will give you the option to add the app.
  4. Click Add Them.

#welcome-new-team-members channel open, @say-hello (local) is mentioned and Slackbot giving you the option to add the app to the channel

  1. Send hello from the message field to test the app. The app responds with “Hey there @USER”.

Slack history from when @say-hello (local) is added to the channel to the when the user says “hello” and the app responds

You built and deployed your app using the Slack CLI! It’s a good start. Try something a little more interactive by sending a button element rather than plain text in the next step.

Partagez vos commentaires sur Trailhead dans l'aide Salesforce.

Nous aimerions connaître votre expérience avec Trailhead. Vous pouvez désormais accéder au nouveau formulaire de commentaires à tout moment depuis le site d'aide Salesforce.

En savoir plus Continuer à partager vos commentaires