forked from rladies/slack-invite-automation
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
36 additions
and
39 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const request = require('request'); | ||
const fetch = require('cross-fetch'); | ||
|
||
const config = require('../config'); | ||
const { badge } = require('../lib/badge'); | ||
|
@@ -16,48 +16,45 @@ router.get('/', function(req, res) { | |
|
||
router.post('/invite', function(req, res) { | ||
if (req.body.email && (!config.inviteToken || (!!config.inviteToken && req.body.token === config.inviteToken))) { | ||
function doInvite() { | ||
request.post({ | ||
url: 'https://'+ config.slackUrl + '/api/users.admin.invite', | ||
form: { | ||
email: req.body.email, | ||
token: config.slacktoken, | ||
set_active: true | ||
async function doInvite() { | ||
const email = req.body.email; | ||
const channelId = config.slackChannel; | ||
const body = { | ||
"channel": channelId, | ||
"text": req.body.email + ' requested to join R-Ladies Seattle Slack. Please add them (see channel descriptions for instructions) and mark this message when complete.' | ||
}; | ||
const url = 'https://'+ config.slackUrl + '/api/chat.postMessage'; | ||
try { | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
headers: { | ||
"Content-Type": "application/json;charset=utf-8", | ||
"Authorization": `Bearer ${config.slacktoken}` | ||
} | ||
}, function(err, httpResponse, body) { | ||
// body looks like: | ||
// {"ok":true} | ||
// or | ||
// {"ok":false,"error":"already_invited"} | ||
if (err) { return res.send('Error:' + err); } | ||
body = JSON.parse(body); | ||
if (body.ok) { | ||
}); | ||
if(response.ok) { | ||
const data = await response.json(); | ||
if (data.ok) { | ||
res.render('result', { | ||
community: config.community, | ||
message: 'Success! Check “'+ req.body.email +'” for an invite from Slack.' | ||
message: "Success! Once an admin approves your request, you will receive an invite to Slack at<br>" + req.body.email | ||
}); | ||
} else { | ||
let error = body.error; | ||
if (error === 'already_invited' || error === 'already_in_team') { | ||
res.render('result', { | ||
community: config.community, | ||
message: 'Success! You were already invited.<br>' + | ||
'Visit <a href="https://'+ config.slackUrl +'">'+ config.community +'</a>' | ||
}); | ||
return; | ||
} else if (error === 'invalid_email') { | ||
error = 'The email you entered is an invalid email.'; | ||
} else if (error === 'invalid_auth') { | ||
error = 'Something has gone wrong. Please contact a system administrator.'; | ||
} | ||
|
||
res.render('result', { | ||
community: config.community, | ||
message: 'Failed! ' + error, | ||
isFailed: true | ||
}); | ||
console.error(data); | ||
throw new Error('Non-ok response') | ||
} | ||
} | ||
} catch(err) { | ||
console.error(err); | ||
error = 'Something has gone wrong. Please try again later or contact [email protected] if the problem persists.'; | ||
res.render('result', { | ||
community: config.community, | ||
message: 'Failed! ' + error, | ||
isFailed: true | ||
}); | ||
|
||
} | ||
} | ||
if (!!config.recaptchaSiteKey && !!config.recaptchaSecretKey) { | ||
request.post({ | ||
|