-
Notifications
You must be signed in to change notification settings - Fork 5
/
SlackTest.js
56 lines (50 loc) · 1.75 KB
/
SlackTest.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
console.log('Loading function');
const https = require('https');
const url = require('url');
const slack_url = 'https://hooks.slack.com/services/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';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = {
'Content-Type': 'application/json'
};
exports.handler = function(event, context) {
(event.Records || []).forEach(function(rec) {
if (rec.Sns) {
var req = https.request(slack_req_opts, function(res) {
if (res.statusCode === 200) {
context.succeed('posted to slack');
} else {
context.fail('status code: ' + res.statusCode);
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
context.fail(e.message);
});
var text_msg = JSON.stringify(rec.Sns.Message, null, ' ');
try {
var msg_data = [];
var parsed = JSON.parse(rec.Sns.Message);
for (var key in parsed) {
msg_data.push(key + ': ' + parsed[key]);
}
text_msg = msg_data.join("\n");
} catch (e) {
console.log(e);
}
var params = {
attachments: [{
fallback: text_msg,
pretext: rec.Sns.Subject,
color: "#D00000",
fields: [{
"value": text_msg,
"short": false
}]
}]
};
req.write(JSON.stringify(params));
req.end();
}
});
};