index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var express = require('express');
  2. var router = express.Router();
  3. var request = require('request');
  4. var config = require('../config');
  5. router.get('/', function(req, res) {
  6. res.render('index', { community: config.community });
  7. });
  8. router.post('/invite', function(req, res) {
  9. if (req.body.email) {
  10. request.post({
  11. url: 'https://'+ config.slackUrl + '/api/users.admin.invite',
  12. form: {
  13. email: req.body.email,
  14. token: config.slacktoken,
  15. set_active: true
  16. }
  17. }, function(err, httpResponse, body) {
  18. // body looks like:
  19. // {"ok":true}
  20. // or
  21. // {"ok":false,"error":"already_invited"}
  22. if (err) { return res.send('Error:' + err); }
  23. body = JSON.parse(body);
  24. if (body.ok) {
  25. res.render('result', {
  26. community: config.community,
  27. message: 'Success! Check "'+ req.body.email +'" for an invite from Slack.'
  28. });
  29. } else {
  30. var error = body.error;
  31. if (error === 'already_invited') {
  32. error = 'You are already invited.'
  33. } else if (error === 'invalid_email') {
  34. error = 'The email you entered is an invalid email.'
  35. }
  36. res.render('result', {
  37. community: config.community,
  38. message: 'Failed! ' + error
  39. });
  40. }
  41. });
  42. } else {
  43. res.status(400).send('email is required.');
  44. }
  45. });
  46. module.exports = router;