-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (38 loc) · 1.16 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
const http = require('http');
const url = require('url');
const request = require('request-promise');
const config = require('./config');
const stackrequest = require('./stackrequest');
http.createServer((clientReq, clientRes) => {
const urlParams = url.parse(clientReq.url, true).query,
token = urlParams.token,
message = urlParams.text,
responseUrl = urlParams.response_url;
if(config.SLACK_TOKEN !== token) {
clientRes.write('Wrong token.');
clientRes.end();
}
if(message ==='') {
clientRes.write(config.MESSAGES.NO_ARGS);
}
const response = {
'text': config.MESSAGES.RESULTS + message
}
stackrequest(message).then((data) => {
if(!data) {
response.text = config.MESSAGES.EPMPTY
}
Object.assign(response, {attachments: data});
const responseParams = {
method: 'POST',
uri: responseUrl,
body: response,
json: true
}
//send response to Slack
return request(responseParams);
}).catch((e) => {
clientRes.write(e.message);
});
clientRes.end();
}).listen(3000);