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
- In Slack, navigate to the App Credentials page for your Slack App and copy the Signing Secret


- 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


- In your Slack App configuration, go to Features > Event Subscriptions:
- Toggle Enable Events
- In the Request URL field, paste the URL you copied from Fusebit in the previous step


- 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 thechannels:history
scope.- In Features > Event Subscriptions under Subscribe to bot events (or Subscribe to events on behalf of users) add the event type
message.channels
- In Features > OAuth & Permissions > Scopes > Bot Token Scopes, add
channels:history
- In your Connector, make sure you add the
channels:history
scope in the Bot User Token Scopes field.
- In Features > Event Subscriptions under Subscribe to bot events (or Subscribe to events on behalf of users) add the event type


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.
- 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 associatedchannels: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


- 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,
});
}
});
- 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.
Updated 19 days ago