-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (58 loc) · 1.69 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
var request = require('request');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
// config
const port = process.env.PORT || 8000;
// utils
////////
function msg(channel, text, callback) {
var hookURL = process.env.SLACK_INCOMING_WEBHOOK;
if (!hookURL) {
// throw
return;
}
request.post(
hookURL,
{
json: true,
body: {
username: 'lmgtfy',
icon_emoji: ':ghost:',
// heroku's node doesn't do default params
channel: channel || '#tobot-debug',
text
}
},
callback
)
}
// routes
/////////
// LMGTFY
//
// A specialized incoming webhook for dealing with custom slash commands.
// https://api.slack.com/slash-commands
app.post('/lmgtfy', upload.array(), (req, res) => {
console.log(req.body);
// token, team_id, channel_id, channel_name, user_id, team_domain, user_name, command, text
// TODO assert req.body.token == process.env.SLACK_TOKEN
// TODO assert req.body.text is not null
const base = 'http://letmegooglethatforyou.com/?l=1&q=';
if (req.body.channel_name === 'directmessage') {
var text = `<${base}${encodeURI(req.body.text)}|${req.body.text}>`;
} else {
var text = `${req.body.user_name} suggests: <${base}${encodeURI(req.body.text)}|${req.body.text}>`;
}
msg(req.body.channel_id, text)
// TODO if there's a warning, send it back to the user
res.send();
});
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});