Skip to content

Commit

Permalink
Update to message, rm invite
Browse files Browse the repository at this point in the history
See for inspiration outsideris#149
  • Loading branch information
kdillmcfarland committed Oct 4, 2022
1 parent 4d238de commit e64de8a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"dependencies": {
"body-parser": "^1.18.0",
"cookie-parser": "^1.4.0",
"cross-fetch": "^3.1.4",
"debug": "^3.1.0",
"dotenv": "^5.0.1",
"express": "^4.13.0",
"i18n": "^0.8.3",
"morgan": "^1.6.0",
"pug": "^2.0.0-rc.4",
"request": "^2.62.0",
"serve-favicon": "^2.3.0",
"sanitize": "^2.1.0"
"sanitize": "^2.1.0",
"serve-favicon": "^2.3.0"
}
}
69 changes: 33 additions & 36 deletions routes/index.js
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');
Expand All @@ -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({
Expand Down

0 comments on commit e64de8a

Please sign in to comment.