-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (48 loc) · 1.3 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
'use strict';
const s3 = require('./lib/s3');
const slack = require('./lib/slack');
const querystring = require('querystring');
function respond(code, payload) {
return {
statusCode: code,
body: JSON.stringify(payload)
};
}
module.exports.handler = async event => {
try {
// validate request
if (!slack.validateRequest(event)) {
throw new Error('Slack request validation failed.');
}
// parse request
const request = querystring.parse(event.body);
// get history
const history = await slack.getChannelHistory(request.channel_id);
// make file name
const file = `${request.team_id}-${request.channel_id}-${
history.messages[0].ts
}-${history.messages[history.messages.length - 1].ts}.json`;
// put history
await s3.putData(file, history);
// get signed key
const key = s3.getSignedKey(file);
// respond to user
return respond(200, {
// response_type: 'in_channel',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `I made this into <https://test.com?key=${key}|a script>.`
}
}
]
});
} catch (e) {
console.error(e);
return respond(200, {
text: 'Sorry! Something went wrong with the script making.'
});
}
};