generated from staart/api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail-forwarder.js
62 lines (61 loc) · 1.49 KB
/
mail-forwarder.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
const https = require("https");
const crypto = require("crypto");
exports.handler = (event) =>
new Promise((resolve) => {
if (
!event.Records.length ||
!event.Records[0].s3 ||
!event.Records[0].s3.object ||
!event.Records[0].s3.object.key
) {
console.error("ERROR", "Did not get an S3 record");
return resolve({
statusCode: 400,
body: JSON.stringify({
success: false,
error: "Did not get an S3 record",
}),
});
}
const key = decodeURIComponent(
event.Records[0].s3.object.key.replace(/\+/g, " ")
);
const req = https.request(
{
hostname: "api.myeiva.com",
port: 443,
path: "/v1/webhooks/inbound/email/" + key,
method: "GET",
headers: {
Authorization:
"Bearer " +
crypto
.createHmac("sha256", "WEBHOOK_SECRET")
.update(key)
.digest("hex"),
},
},
(res) => {
res.on("end", () => {
resolve({
statusCode: 200,
body: JSON.stringify({
success: true,
key,
}),
});
});
}
);
req.on("error", (error) => {
console.error("ERROR", error);
resolve({
statusCode: 500,
body: JSON.stringify({
success: false,
error: "An error occurred in making the webhook request",
}),
});
});
req.end();
});