forked from rauchg/slackin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-invite.js
43 lines (38 loc) · 1.44 KB
/
slack-invite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import request from 'superagent'
export default function invite ({ org, token, email, channel }, fn){
let data = { email, token }
if (channel) {
data.channels = channel
data.ultra_restricted = 1
data.set_active = true
}
request
.post(`https://${org}.slack.com/api/users.admin.invite`)
.type('form')
.send(data)
.end(function (err, res){
if (err) return fn(err)
if (200 != res.status) {
fn(new Error(`Invalid response ${res.status}.`))
return
}
// If the account that owns the token is not admin, Slack will oddly
// return `200 OK`, and provide other information in the body. So we
// need to check for the correct account scope and call the callback
// with an error if it's not high enough.
let {ok, error: providedError, needed} = res.body
if (!ok) {
if (providedError === 'missing_scope' && needed === 'admin') {
fn(new Error(`Missing admin scope: The token you provided is for an account that is not an admin. You must provide a token from an admin account in order to invite users through the Slack API.`))
} else if (providedError === 'already_invited') {
fn(new Error('You have already been invited to Slack. Check for an email from [email protected].'))
} else if (providedError === 'already_in_team') {
fn(new Error(`Sending you to Slack...`))
} else {
fn(new Error(providedError))
}
return
}
fn(null)
})
}