Skip to main content
With the application scaffold in place, the next step is to build the handling and processing functionality for user events, as well as the handling of slash commands coming from Slack. Each one of these will eventually be passed to a Box API endpoint to perform group and content collaboration tasks. In this step we’ll expand the empty functions we wrote in the last step. These functions will perform the following tasks.
  • Listen for new events and slash commands from Slack.
  • Process those events and commands to route to the appropriate function.
  • Process all Slack users in a channel to be added to a Box group when the bot is first added to a channel.
  • Fetch profile information for a Slack user to get their email.

Listen for Slack events

When the Slack application was configured, it was instructed to send events to our application code for three events.
  • When a user joins a channel.
  • When a user leaves a channel.
  • When a user enters a /boxadd Slash command.
Our application needs to have a public route that listens for those messages from Slack. The payloads of these messages will like something like this.
{
  "token": "cF1PwB1eIMcRHZWwFHJR1tgs",
  "team_id": "T932DQSV12P",
  "team_domain": "slacktest",
  "channel_id": "C078N43MFHU",
  "channel_name": "bottest",
  "user_id": "U016JCDPN56",
  "user_name": "testuser",
  "command": "/boxadd",
  "text": "file 123456",
  "response_url": "https://hooks.slack.com/commands/T541DQSV12P/3977594927231/ankvsRb42WKnKPRp002FeyTx",
  "trigger_id": "1189442196855.1183332180295.cca20c3ca1ea193dab432ad8e9e95431"
}

Process Slack events

Next, we will want to determine what event was received and pass this on to the right part of our application.

Process Slack user

Next, we need to define how user events should be processed. There are three events that we need to account for:
  • The bot was added to the channel.
  • A regular user joined the channel.
  • A regular user left the channel.
The code starts by fetching the Box group ID for the channel, which will be defining in the next step. Once obtained, it processes users as follows.
  • If the user is a bot, it needs to initialize the Box group and add all current users of the channel as Box users in the group. This is to account for the bot being added to existing channels, and this is ignored if the bot is being re-added to a channel that they were already present in previously.
  • If the user joined the channel it needs to add them to the group.
  • If the user left the channel it needs to remove them from the group.

Process Slack channel users

When a bot is first added to a channel, it needs to list all users currently in the channel and create a Box group with those people in order to create a baseline for the channel. This code runs a number of actions in sequence.
  • First, it calls the Slack APIs to fetch all members of the channel. The
  • limit can be adjusted to collect more users in the channel.
  • For every user that is found, it calls getSlackUser to get their profile, allow it to map their email address to a Box user’s email address.
  • Each user is then sent to addGroupUser to add them into the group.

Fetch Slack user profile

The last Slack related function is a utility mechanism used by the other functions. It calls the Slack API to fetch the user profile given the user ID provided by either Slack event / command or when fetching a list of channel users. Since we’re matching Slack users to Box users via their email address, that is the field that we care about from the user profile lookup.
Email addresses in Box are unique and cannot be used for multiple accounts, meaning that they can be used effectively for user account lookup.

Summary

  • You’ve verified incoming events and forwarded them to be processed.
  • You’ve processed events and routed to the appropriate function.
  • You’ve implemented functions for processing all users in a channel and for fetching the Slack profile of a single user.