Skip to content

Commit

Permalink
Add an example script demonstrating channel creation plus member invi…
Browse files Browse the repository at this point in the history
…tations
  • Loading branch information
seratch committed Aug 18, 2021
1 parent 2c16c1e commit 4b44424
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions prod-server-integration-tests/scripts/conversations_invite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Simple example script demonstrating how to create a new channel and invite members using a bot token
// This example is originally created to verify if the web-api works without any issues for https://github.com/slackapi/bolt-js/issues/973 but the code should be useful for general purposes.

const { WebClient } = require('@slack/web-api');

const client = new WebClient(
token = process.env.SLACK_SDK_TEST_BOT_TOKEN,
{ logLevel: 'debug' },
);

(async () => {
let channelId = null;
try {
// Create a new public channel
const teamId = (await client.auth.test()).team_id;
const creation = await client.conversations.create({
name: `test-channel-${Date.now()}`,
team_id: teamId,
})
console.log(JSON.stringify(creation, null, 2));

channelId = creation.channel.id;

// Randomly pick up human user IDs to invite
const userIds = (await client.users.list({
limit: 100
})).members.filter(m =>
m.id !== 'USLACKBOT' && // USLACKBOT is a special user ID for @SlackBot
!m.is_bot &&
!m.is_invited_user &&
!m.is_restricted &&
!m.is_ultra_restricted &&
!m.deleted &&
!m.is_workflow_bot
).map(m => m.id).slice(0, 5);

// Invite the users to the channel
const invitations = await client.conversations.invite({
channel: channelId,
users: userIds.join(","),
})
console.log(JSON.stringify(invitations, null, 2));

// Verify if the channel has members as expected
const members = await client.conversations.members({ channel: channelId })
console.log(JSON.stringify(members, null, 2));

} finally {
// Archive the channel if you want to remove the channel from your Slack client view
if (channelId !== null) {
const cleanup = await client.conversations.archive({
channel: channelId,
})
console.log(JSON.stringify(cleanup, null, 2));
}
};
})();

0 comments on commit 4b44424

Please sign in to comment.