-
Notifications
You must be signed in to change notification settings - Fork 39
/
handler.js
148 lines (135 loc) · 3.78 KB
/
handler.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
'use strict';
// importing AWS sdk
import AWS from 'aws-sdk';
import request from 'request';
// importing config file which contains AWS key
// Best practice: to use a config.copy.json when pushing to github
// Coz exposing the AWS keys to public is not good
import config from './config.json';
AWS.config.update({
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey,
region: config.aws.region
});
// Instatiating the SES from AWS SDK
let ses = new AWS.SES();
// Structure of sendMail params structure:
/*
var params = {
Destination: { / required /
BccAddresses: [
'STRING_VALUE',
/ more items /
],
CcAddresses: [
'STRING_VALUE',
/ more items /
],
ToAddresses: [
'STRING_VALUE',
/ more items /
]
},
Message: { / required /
Body: { / required /
Html: {
Data: 'STRING_VALUE', / required /
Charset: 'STRING_VALUE'
},
Text: {
Data: 'STRING_VALUE', / required /
Charset: 'STRING_VALUE'
}
},
Subject: { / required /
Data: 'STRING_VALUE', / required /
Charset: 'STRING_VALUE'
}
},
Source: 'STRING_VALUE', / required /
ConfigurationSetName: 'STRING_VALUE',
ReplyToAddresses: [
'STRING_VALUE',
/ more items /
],
ReturnPath: 'STRING_VALUE',
ReturnPathArn: 'STRING_VALUE',
SourceArn: 'STRING_VALUE',
Tags: [
{
Name: 'STRING_VALUE', / required /
Value: 'STRING_VALUE' / required /
},
/ more items /
]
};
ses.sendEmail(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
*/
// The function to send SES email message
module.exports.sendMail = (event, context, callback) => {
let bccEmailAddresses = event.body.bccEmailAddresses;
let ccEmailAddresses = event.body.ccEmailAddresses;
let toEmailAddresses = event.body.toEmailAddresses;
let bodyData = event.body.bodyData;
let bodyCharset = event.body.bodyCharset;
let subjectdata = event.body.subjectdata;
let subjectCharset = event.body.subjectCharset;
let sourceEmail = event.body.sourceEmail;
let replyToAddresses = event.body.replyToAddresses;
// Building the slack message
var options = {
text: 'We have got a customer support from ' + replyToAddresses + ' Log into <https://privateemail.com/appsuite/> to answer their query.',
}
// The parameters for sending mail using ses.sendEmail()
let emailParams = {
Destination: {
BccAddresses: bccEmailAddresses,
CcAddresses: ccEmailAddresses,
ToAddresses: toEmailAddresses
},
Message: {
Body: {
Text: {
Data: bodyData,
Charset: bodyCharset
}
},
Subject: {
Data: subjectdata,
Charset: subjectCharset
}
},
Source: sourceEmail,
ReplyToAddresses: replyToAddresses
};
// the response to send back after email success.
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Mail sent successfully'
}),
};
// The sendEmail function taking the emailParams and sends the email requests.
ses.sendEmail(emailParams, function (err, data) {
if (err) {
console.log(err, err.stack);
callback(err);
} else {
console.log("SES successful");
console.log(data);
request.post(config.slackWebhook, { body: JSON.stringify(options)}, function (err, httpResponse, body) {
if (err) {
console.error('Slack webhook failed:', err);
callback(err);
}
console.log('Post to slack bot successful!!');
console.log(httpResponse);
console.log('Post to slack bot replied with:', body);
callback(null, response);
});
}
});
};