Skip to content

Commit e64de8a

Browse files
Update to message, rm invite
See for inspiration outsideris#149
1 parent 4d238de commit e64de8a

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
"dependencies": {
99
"body-parser": "^1.18.0",
1010
"cookie-parser": "^1.4.0",
11+
"cross-fetch": "^3.1.4",
1112
"debug": "^3.1.0",
1213
"dotenv": "^5.0.1",
1314
"express": "^4.13.0",
1415
"i18n": "^0.8.3",
1516
"morgan": "^1.6.0",
1617
"pug": "^2.0.0-rc.4",
17-
"request": "^2.62.0",
18-
"serve-favicon": "^2.3.0",
19-
"sanitize": "^2.1.0"
18+
"sanitize": "^2.1.0",
19+
"serve-favicon": "^2.3.0"
2020
}
2121
}

routes/index.js

+33-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const express = require('express');
22
const router = express.Router();
3-
const request = require('request');
3+
const fetch = require('cross-fetch');
44

55
const config = require('../config');
66
const { badge } = require('../lib/badge');
@@ -16,48 +16,45 @@ router.get('/', function(req, res) {
1616

1717
router.post('/invite', function(req, res) {
1818
if (req.body.email && (!config.inviteToken || (!!config.inviteToken && req.body.token === config.inviteToken))) {
19-
function doInvite() {
20-
request.post({
21-
url: 'https://'+ config.slackUrl + '/api/users.admin.invite',
22-
form: {
23-
email: req.body.email,
24-
token: config.slacktoken,
25-
set_active: true
19+
async function doInvite() {
20+
const email = req.body.email;
21+
const channelId = config.slackChannel;
22+
const body = {
23+
"channel": channelId,
24+
"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.'
25+
};
26+
const url = 'https://'+ config.slackUrl + '/api/chat.postMessage';
27+
try {
28+
const response = await fetch(url, {
29+
method: 'POST',
30+
body: JSON.stringify(body),
31+
headers: {
32+
"Content-Type": "application/json;charset=utf-8",
33+
"Authorization": `Bearer ${config.slacktoken}`
2634
}
27-
}, function(err, httpResponse, body) {
28-
// body looks like:
29-
// {"ok":true}
30-
// or
31-
// {"ok":false,"error":"already_invited"}
32-
if (err) { return res.send('Error:' + err); }
33-
body = JSON.parse(body);
34-
if (body.ok) {
35+
});
36+
if(response.ok) {
37+
const data = await response.json();
38+
if (data.ok) {
3539
res.render('result', {
3640
community: config.community,
37-
message: 'Success! Check “'+ req.body.email +'” for an invite from Slack.'
41+
message: "Success! Once an admin approves your request, you will receive an invite to Slack at<br>" + req.body.email
3842
});
3943
} else {
40-
let error = body.error;
41-
if (error === 'already_invited' || error === 'already_in_team') {
42-
res.render('result', {
43-
community: config.community,
44-
message: 'Success! You were already invited.<br>' +
45-
'Visit <a href="https://'+ config.slackUrl +'">'+ config.community +'</a>'
46-
});
47-
return;
48-
} else if (error === 'invalid_email') {
49-
error = 'The email you entered is an invalid email.';
50-
} else if (error === 'invalid_auth') {
51-
error = 'Something has gone wrong. Please contact a system administrator.';
52-
}
53-
54-
res.render('result', {
55-
community: config.community,
56-
message: 'Failed! ' + error,
57-
isFailed: true
58-
});
44+
console.error(data);
45+
throw new Error('Non-ok response')
5946
}
47+
}
48+
} catch(err) {
49+
console.error(err);
50+
error = 'Something has gone wrong. Please try again later or contact [email protected] if the problem persists.';
51+
res.render('result', {
52+
community: config.community,
53+
message: 'Failed! ' + error,
54+
isFailed: true
6055
});
56+
57+
}
6158
}
6259
if (!!config.recaptchaSiteKey && !!config.recaptchaSecretKey) {
6360
request.post({

0 commit comments

Comments
 (0)