index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. channels: config.channels,
  15. token: config.slacktoken,
  16. set_active: true
  17. }
  18. }, function(err, httpResponse, body) {
  19. // body looks like:
  20. // {"ok":true}
  21. // or
  22. // {"ok":false,"error":"channel_not_found"}
  23. // or
  24. // {"ok":false,"error":"already_invited"}
  25. if (err) { return res.send('Error:' + err); }
  26. body = JSON.parse(body);
  27. if (body.ok) {
  28. res.send('Success! Check "'+ req.body.email +'" for an invite from Slack.');
  29. } else {
  30. res.send('Failed! ' + body.error)
  31. }
  32. });
  33. } else {
  34. res.status(400).send('email is required.');
  35. }
  36. });
  37. module.exports = router;