Add All Users to a Slack Channel
If you've ever tried to create a new default Slack channel within an existing
workspace, you know that while it's easy to create a channel that new members
are added to automatically,
adding existing members to that channel is not so simple. This app solves that
problem - installing it into your Slack workspace allows you to add all users
currently in the workspace into a given channel with a simple slash command!

How It Works
Your app adds a slash command called /cmd invite-all
that triggers the
functions/events/slack/command/invite-all.js
endpoint in this project. The
endpoint first fetches all the existing users in the #general
channel 1000 at
a time using the members method of the
slack.conversations
API. It handles pagination for large workspaces as shown below:
let generalChannel = await lib.slack.channels['@0.7.2'].retrieve({
channel: `#general`
});
let generalChannelMembers = [];
let currentCursor = null;
do {
let membersResponse = await lib.slack.conversations['@0.2.14'].members({
channel: generalChannel.id,
limit: 1000,
cursor: currentCursor
});
currentCursor = membersResponse.response_metadata.next_cursor;
generalChannelMembers = generalChannelMembers.concat(membersResponse.members);
} while (!!currentCursor);
Next, it fetches all the users in the invoked channel in a similar fashion, then
dedupes the users (Slack throws an error when inviting a user who is already in
a channel) and invites them in batches with the invite method of the
slack.conversations API:
let inviteableMembers = generalChannelMembers.filter((generalChannelMember) => {
return !newChannelMembers.includes(generalChannelMember);
});
for (let i = 0; i < Math.ceil(inviteableMembers.length / 1000); i++) {
await lib.slack.conversations['@0.2.14'].invite({
channel: event.channel_id,
users: inviteableMembers.slice(i * 1000, (i + 1) * 1000),
as_user: true
});
}
It caps it off with an ephemeral message to the command invoker announcing how
many members the bot added to the channel.
That's it!
Thanks for checking out this Source! Follow the Autocode team on Twitter
@AutocodeHQ for updates. If you have any
questions, please join our community Slack channel - you can get an invite from
under the community tab in the topbar of this page.