-
Notifications
You must be signed in to change notification settings - Fork 6
/
slack_notification.js
49 lines (42 loc) · 1.14 KB
/
slack_notification.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
'use strict';
const url = require('url');
const https = require('https');
function postMessage(message, callback) {
const body = JSON.stringify(message);
const options = url.parse(process.env.ENDPOINT);
options.method = 'POST';
options.headers = {'Content-Type': 'application/json'};
const postReq = https.request(options, (res) => {
const chunks = [];
res.setEncoding('utf8');
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
if (callback) {
callback({
body: chunks.join(''),
statusCode: res.statusCode,
statusMessage: res.statusMessage
});
}
});
return res;
});
postReq.write(body);
postReq.end();
}
exports.handler = (event, context, callback) => {
const sns = event.Records[0].Sns;
const message = JSON.parse(sns.Message);
let slackMessage = {
username: "Barcelona",
attachments: [
{
fallback: message.text,
pretext: sns.Subject,
text: `[${process.env.DISTRICT}] ${message.text}`,
color: message.level
}
]
}
postMessage(slackMessage, (response) => {callback(null);});
};