forked from johnagan/triagebot-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (38 loc) · 1.49 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
/*
* This is a very basic webserver and HTTP Client.
* In production, you may want to use something like Express.js
* or Botkit to host a webserver and manage API calls
*/
const {TOKEN, PORT} = process.env,
triage = require('./triage'),
qs = require('querystring'),
axios = require('axios'),
http = require('http');
// Handle any request to this server and parse the POST
function handleRequest(req, res){
let body = "";
req.on('data', data => body += data);
req.on('end', () => handleCommand(qs.parse(body)));
res.end('');
}
// Get channel history, build triage report, and respond with results
function handleCommand(payload) {
let {channel_id, response_url} = payload;
if (!response_url) {
console.log('got empty response_url in payload');
return;
}
// load channel history
let ts = Math.round(new Date().getTime() / 1000);
let tsYesterday = ts - (36 * 3600);
let params = qs.stringify({ count: 1000, token: TOKEN, channel: channel_id, oldest: tsYesterday });
let getHistory = axios.post('https://slack.com/api/channels.history', params);
// build the triage report
let buildReport = result => Promise.resolve( triage(payload, result.data.messages || []) );
// post back to channel
let postResults = results => axios.post(response_url, results);
// execute
getHistory.then(buildReport).then(postResults).catch(console.error);
}
// start server
http.createServer(handleRequest).listen(PORT, () => console.log(`server started on ${PORT}`));