Skip to content

Instantly share code, notes, and snippets.

@norganna
Last active May 2, 2024 05:22
Show Gist options
  • Save norganna/12686901c0d802ed112d911ee24bc68c to your computer and use it in GitHub Desktop.
Save norganna/12686901c0d802ed112d911ee24bc68c to your computer and use it in GitHub Desktop.
A slack bot to invite users from one channel to another.

Using:

Installing the script

Make a new directory and put the run.js script into it.

Run npm in that same directory to install the slack web-api module:

npm install @slack/web-api

Creating a bot user

This script will run as a bot user on your server.

To begin using this bot you will need to add an App on your slack workspace.

I'm not sure exactly what roles you'll need to add, but these worked for me:

  • channels:join Join public channels in the workspace
  • channels:manage Manage public channels that inviter has been added to and create new ones
  • channels:read View basic information about public channels in the workspace
  • users:read View people in the workspace

Then once added to your workspace, copy the bot token and replace it in the token script variable.

You'll also need to invite your bot to the channel you wish to have it invite people to. The easiest way to do it is @ mention it in the channel.

Then simply replace the from_name and to_name variables with the names of your channels to copy from and to respectively.

Running the script.

From the directory you installed run.js in and installed the slack web-api module in, and after having replaced the values of the token, from_name and to_name variables in, run the script:

node run

You will see output like:

Inviting users from channel #my-existing-channel (C9A79B5K2) to #my-new-channel (C015RC2D62J)
Getting member list from #my-existing-channel...
Found 1530 members
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 100 members...
Inviting 30 members...
Done!
const { WebClient } = require('@slack/web-api');
const token = 'xoxb-123456789012-1234567890123-123456789012345678923456';
const from_name = 'my-existing-channel';
const to_name = 'my-new-channel';
// Initialize
const web = new WebClient(token);
(async function() {
let from_id, to_id;
find_channels:
for await (const list of web.paginate('conversations.list', {type: 'public_channel'})) {
for (const chan of list.channels) {
if (chan.name == from_name) {
from_id = chan.id;
}
if (chan.name == to_name) {
to_id = chan.id;
}
if (from_id && to_id) {
break find_channels;
}
}
}
if (!from_id) { console.log(`Cannot find the ${from_name} channel`); return; }
if (!to_id) { console.log(`Cannot find the ${to_name} channel`); return; }
console.log(`Inviting users from channel #${from_name} (${from_id}) to #${to_name} (${to_id})`);
let member_ids = [];
console.log(`Getting member list from #${from_name}...`);
for await (const list of web.paginate('conversations.members', {channel: from_id})) {
for (const member of list.members) {
member_ids.push(member);
}
}
console.log(`Found ${member_ids.length} members`);
const chunk = 100;
for (let i = 0; i < member_ids.length; i += chunk) {
const invite_list = member_ids.slice(i, i + chunk);
console.log(`Inviting ${invite_list.length} members...`);
await web.conversations.invite({
channel: to_id,
users: invite_list.join(',')
});
}
console.log('Done!');
})();
@Samuel-bartels
Copy link

How could I just invite a list of people not everyone all at once?

@norganna
Copy link
Author

You could read in a list of users from a file and then iterate over users.list (instead of conversations.members) to find them all. In which case you wouldn't need any of the from_name/from_id stuff.

@srivatsangf
Copy link

How to get the bot token. Can you please explain that part in detail.
TIA

@jaipandya
Copy link

If you are looking for a no-code solution, you can use Channel Tools app to invite users in bulk from one channel to another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment