-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (48 loc) · 1.06 KB
/
index.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
var koa = require('koa');
var body = require('koa-body');
var app = module.exports = koa();
// Load environment variables
if (app.env === 'development') {
require('node-env-file')(__dirname + '/.env');
}
app.use(body());
/**
* Handshake-ish
*/
app.use(function *(next) {
var body = this.request.body;
var received = new Date();
yield next;
if (this.valid && this.text) {
socket.emit('message', {
text: this.text,
name: body.user_name,
timestamp: body.timestamp,
delayed: (received - new Date(body.timestamp))
});
}
});
/**
* Valid, are ya?
*/
app.use(function *(next) {
var body = this.request.body;
var token = process.env.SLACK_TOKEN;
this.valid = (!token || (token && (body.token !== token)));
yield next;
this.valid = (this.valid && this.text);
});
/**
* Parsage
*/
app.use(function *(next) {
var body = this.request.body;
if (body.trigger_word) {
this.text = body.text.replace(body.trigger_word, '').trim();
} else {
this.text = body.text;
}
yield next;
});
});
app.listen(process.env.PORT);