Slack – How to invite a team member to all channels in Slack

slack

I read the post here to invite all team members to a channel.

On the reverse side, I now need to invite a single team member to all my Slack channels.

Does anyone knows how I can do this?

Best Answer

Found ! The code requires 2 parameters. You need a user that is already invited to all the channels in which you want to invite the new team member (in my case, any collaborator of my team is invited to all channels). You need the user ID of the new team member, and you need the connection token of the previous user which is already in all channels. You can get the user ID of the new team member through slack API using Postman for instance.

Once you have those parameters, replace them in then execute the below code :

var express = require(‘express’);
var router = express.Router();
var rp = require(‘request-promise’);

/* GET home page. */

const ID_USER_TO_INVITE = “USER_ID_IN_SLACK_TEAM”

router.get(‘/’, function(req, res, next) {
    let url = “https://slack.com/api/groups.list”
    var options = {
        method: ‘POST’,
        headers: {‘Content-Type’: ‘application/x-www-form-urlencoded’},
        uri: url,
        form: {
            token: TOKEN_USER_already_in_target_channels,
            exclude_archived: true
        },
        json: true // Automatically stringifies the body to JSON
    };
    rp(options)
    .then(parsedBody => {
        console.log(“OK: “+parsedBody)
        var promised_array = parsedBody.groups.map(item => {
            console.log(item.id)
            var url_invit = “https://slack.com/api/groups.invite”
            var option_invit = {
                method: ‘POST’,
                headers: {‘Content-Type’: ‘application/x-www-form-urlencoded’},
                uri: url_invit,
                form: {
                    token: TOKEN_USER_already_in_target_channels,
                    user: ID_USER_TO_INVITE,
                    channel: item.id
                }
            }
            return rp(option_invit)
        })
        Promise.all(promised_array).then(result => {
            res.send(result)
        })
    })
    .catch(err => {
        console.log(“ERR: “+err)
    })


 // res.render(‘index’, { title: ‘Express’ });
});

module.exports = router;