Skip to content

Instantly share code, notes, and snippets.

@madc
Created May 18, 2018 09:36
Show Gist options
  • Save madc/c7c962a0b7274d53857d34e24a110697 to your computer and use it in GitHub Desktop.
Save madc/c7c962a0b7274d53857d34e24a110697 to your computer and use it in GitHub Desktop.
/** Automate Slack invitations
* See https://github.com/ErikKalkoken/slackApiDoc/blob/master/users.admin.invite.md
*/
const inviteToSlack =
(token, email) => new Promise((resolve, reject) => {
const data = querystring.stringify({
resend: true,
token,
email,
});
const request = https.request({
method: 'POST',
host: '<your-workspace>.slack.com',
path: '/api/users.admin.invite',
port: 443,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
},
},
response => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(response.statusCode);
}
const body = [];
response.setEncoding('utf8');
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve(
JSON.parse(body.join(''))
));
});
request.on('error', (err) => reject(err));
request.write(data);
request.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment