Skip to content

Instantly share code, notes, and snippets.

@miguelperezit
Forked from norganna/README.md
Created February 7, 2021 17:24
Show Gist options
  • Save miguelperezit/ff700505d77c2bf182227ed3deba4ff3 to your computer and use it in GitHub Desktop.
Save miguelperezit/ff700505d77c2bf182227ed3deba4ff3 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!');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment