index.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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.send('Success! Check "'+ req.body.email +'" for an invite from Slack.');
  26. } else {
  27. res.send('Failed! ' + body.error)
  28. }
  29. });
  30. } else {
  31. res.status(400).send('email is required.');
  32. }
  33. });
  34. module.exports = router;