Receiving Slack Events

With this Slack App configuration, your Fusebit Integration will be able to post messages as a bot (or on behalf of users if you configured that functionality). But you will likely also want to get notified when users respond to your messages OR when they post certain keywords or commands. We can achieve that by configuring Slack’s Event API and connecting it to Fusebit

Configure your Connector

  1. In Slack, navigate to the App Credentials page for your Slack App and copy the Signing Secret
24682468
  1. In your Connector, paste the Signing Secret and note the Events API Request URL as you will need it in the next step. Go to the bottom of the page and click save
27482748
  1. In your Slack App configuration, go to Features > Event Subscriptions:
    1. Toggle Enable Events
    2. In the Request URL field, paste the URL you copied from Fusebit in the previous step
26962696
  1. You need to decide what event types you would like to listen to, and ensure your app is registered for those events and has requested the relevant scopes. In this example, we will subscribe to the message.channels Bot User event which requires the channels:history scope.
    1. In Features > Event Subscriptions under Subscribe to bot events (or Subscribe to events on behalf of users) add the event type message.channels
    2. In Features > OAuth & Permissions > Scopes > Bot Token Scopes, add channels:history
    3. In your Connector, make sure you add the channels:history scope in the Bot User Token Scopes field.
26962696

Set up your Integration

Once you have configured Slack Events API support in your Connector, you can receive events in your Integration. The Connector will route all events from the workspace (team) that your Integration was authorized for when it was created.

  1. Depending on the permissions you requested, your Bot User may need to be a member of a channel to receive events for activity in that channel. In this example, given the message.channels event type we subscribed to and the associated channels:history permission we requested, you need to add the bot to the channel. You can do that by going to the channel details and selecting More > Add apps
788788
  1. At the bottom of integration.js edit the following handler, which will echo any messages sent to the channel.
integration.event.on('/:componentName/webhook/event_callback', async (ctx) => {
  const slackClient = await integration.service.getSdk(ctx, ctx.params.componentName, ctx.req.body.installIds[0]);
  const messagingUser = ctx.req.body.data.event.user;
  const authorizedListeningUser = ctx.req.body.data.authorizations[0].user_id;

  // Don't echo our bot's own messages
  if (messagingUser !== authorizedListeningUser) {
    const text = ctx.req.body.data.event.text;
    slackClient.chat.postMessage({
      text: `User <@${messagingUser}> sent message: \n\n "${text}"`,
      channel: ctx.req.body.data.event.channel,
    });
  }
});
  1. Type in a message in the channel where the bot was added, you should see your message echoed back in the channel.

👍

Good Job!

Now your Slack App can listen for and respond to events on your user's behalf!

In addition to leveraging your app to respond to events from Slack, you can also leverage Fusebit's Event handler to respond to 'User-triggered Interactions' as well. In our next sections, we will configure this app to respond to Slash Commands.


What’s Next