Created
May 18, 2018 09:36
-
-
Save madc/c7c962a0b7274d53857d34e24a110697 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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