-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (131 loc) · 3.99 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
var AWS = require('aws-sdk');
var url = require('url');
var https = require('https');
var _ = require('lodash');
const hookUrl = process.env.HOOK_URL;
var baseSlackMessage = {}
var postMessage = function(message, callback) {
var body = JSON.stringify(message);
var options = url.parse(hookUrl);
options.method = 'POST';
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
};
var postReq = https.request(options, function(res) {
var chunks = [];
res.setEncoding('utf8');
res.on('data', function(chunk) {
return chunks.push(chunk);
});
res.on('end', function() {
var body = chunks.join('');
if (callback) {
callback({
body: body,
statusCode: res.statusCode,
statusMessage: res.statusMessage
});
}
});
return res;
});
postReq.write(body);
postReq.end();
};
var handleBounce = function(MAIL) {
const timestamp = MAIL.timestamp;
const title = `SES Message Bounce (${MAIL.bounce.bounceType} - ${MAIL.bounce.bounceSubType})`;
var subject = "n/a";
var from = MAIL.mail.source;
// Get better headers, if possible
if(false == MAIL.mail.headersTruncated){
subject = MAIL.mail.commonHeaders.subject;
from = MAIL.mail.commonHeaders.from[0];
}
var recipients = '';
MAIL.bounce.bouncedRecipients.forEach(function(recipient){
recipients += `* ${recipient.emailAddress} (${recipient.action}): ${recipient.diagnosticCode}\n`;
})
var color = "danger";
switch (MAIL.bounce.bounceType) {
case 'Transient':
color = "warning";
break;
}
var slackMessage = {
text: "*" + title + "*",
attachments: [
{
"fields": [
{ "title": "Subject", "value": subject, "short": false},
{ "title": "From", "value": from, "short": false},
{ "title": "Recipients", "value": recipients, "short": false}
],
"color": color,
"ts": timestamp,
"icon_emoji": ":aws-ses:"
}
]
};
return _.merge(slackMessage, baseSlackMessage);
};
var handleDelivery = function(MAIL) {
const timestamp = MAIL.timestamp;
const title = `SES Message Delivery`;
var subject = "n/a";
var from = MAIL.mail.source;
// Get better headers, if possible
if(false == MAIL.mail.headersTruncated){
subject = MAIL.mail.commonHeaders.subject;
from = MAIL.mail.commonHeaders.from[0];
}
var recipients = '';
MAIL.delivery.recipients.forEach(function(recipient){
recipients += `* ${recipient}\n`;
})
var color = "good";
var slackMessage = {
text: "*" + title + "*",
attachments: [
{
"fields": [
{ "title": "Subject", "value": subject, "short": false},
{ "title": "From", "value": from, "short": false},
{ "title": "Recipients", "value": recipients, "short": false}
],
"color": color,
"ts": timestamp,
"icon_emoji": ":aws-ses:"
}
]
};
return _.merge(slackMessage, baseSlackMessage);
};
exports.handler = function(event, context) {
console.log("message received:" + JSON.stringify(event, null, 2));
const MAIL = JSON.parse(event.Records[0].Sns.Message);
var slackMessage = '';
switch(MAIL.notificationType) {
case 'Delivery':
slackMessage = handleDelivery(MAIL);
break;
case 'Bounce':
slackMessage = handleBounce(MAIL);
break;
}
postMessage(slackMessage, function(response) {
if (response.statusCode < 400) {
console.info('message posted successfully');
context.succeed();
} else if (response.statusCode < 500) {
console.error("error posting message to slack API: " + response.statusCode + " - " + response.statusMessage);
// Don't retry because the error is due to a problem with the request
context.succeed();
} else {
// Let Lambda retry
context.fail("server error when processing message: " + response.statusCode + " - " + response.statusMessage);
}
});
};