We’re now handling and processing events coming from Slack, then obtaining all
information needed to connect with Box users and groups. We now need to
connect that functionality to Box functions.
In this step we’ll expand several functions from the last step to incorporate
new Box features.
- Instantiate a Box client.
- Add a Box user to a Box group.
- Remove a Box user from a Box group.
- Fetch a Box group ID from a group name.
- Add content that is shared to a group.
Instantiate a Box Client
To make calls to the Box APIs, you’ll first need to set up a Box client.
Add a Box user to a group
Let’s add a function that adds a Box user to a group. When a bot is added to a
channel and needs to create a Box group with all users of the channel, or when
a single user joins the channel after that action, this function will perform
that task.
Since we’re matching a Slack user to a Box user via their email address, we
first find a matching Box user using the Slack profile email. If found, a call
is made to add that user to the channel group. The group was created when the
bot was first added.
The Box Get User endpoint only permits user lookup
by user ID. To lookup a user by email address, use the
List Enterprise Users endpoint and set the
filter_term option to the email address you’re searching for.
Remove a Box user to a group
When a user leaves or is removed from a Slack channel, we also want to remove
them from the Box group so that they can no longer access the shared group
content.
This code will take the group ID, which will be the Slack channel ID, and get
all members of the group. If a matching member is found for the person that
left the Slack channel, based on email address, that person is removed from the
group using their membership ID.
Improving performance with a data storeWhile looking up group memberships to obtain a membership ID negates the need
to store membership IDs in a local data store (like a database), this code
can be made more efficient by having a data store that saves the Box
membership ID with the user record.By using a local data store, the membership ID can be retrieved from the data
store rather than having to call the Box API repeatedly to search for the
membership ID.
Fetch a Box group ID for a group name
The next Box function we need has two main purposes.
- Return the Box group ID of an existing group.
- If a group doesn’t exist, create the Box group and return the ID.
The code fetches all the groups in the enterprise, and then tries to match the
Slack channel ID to the group name. If any of the groups matches, the group ID
is returned.
If there are no matches, a new Box group is created and the ID of the group is
returned for use. The group will be named after the Slack channel ID since that
is a constant that is returned with both slash commands and user events, making
it easier to lookup without additional functions.
Add shared content to a group
Finally, the main purpose of our whole application is to allow users to share
files and folders from their own Box accounts with everyone else in the group.
Building upon all previous functionality, the following function performs that
task.
The code starts by capturing the Box group ID for the Slack channel, which
is where content will be shared to.
Since we want to share files and folders from the Box account of the person who
sent the slash command, we next capture their Box user profile based on their
email address.
Lastly, we make a call to collaborate content with the group via the group ID.
Summary
- You’ve instantiated a Box client
- You’ve created Box group user add and remove functions.
- You’ve created a function to share content with the group.