-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
46 lines (39 loc) · 1.21 KB
/
Copy pathhandler.js
File metadata and controls
46 lines (39 loc) · 1.21 KB
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
'use strict';
const axios = require('axios');
const sendgrid = require('@sendgrid/mail');
const doRegex = r => (data) => {
const regex = new RegExp(r);
console.log(`Apply regex ${regex}`);
const result = data.match(regex);
return Promise.resolve(result);
};
const requestWebsite = (url) => {
return axios({
url,
method: 'get'
}).then(response => response.data);
};
module.exports.run = (event, context, callback) => {
const url = process.env.URL;
const regex = process.env.REGEX;
const sendgridApiKey = process.env.SENDGRID;
const email = process.env.EMAIL;
console.log(`Parse URL '${url}' for Regex '${regex}'`);
const checkResponseWithRegex = doRegex(regex);
requestWebsite(url)
.then(checkResponseWithRegex)
.then(async (match) => {
console.log(`Regex matched: ${match}`);
if (match) {
console.log(`Found regex in URL. Send email to '${email}'`);
// initiate sendgrid correctly with ApiKey
sendgrid.setApiKey(sendgridApiKey);
await sendgrid.send({
to: email,
from: email,
subject: `Regex '${regex}' for url '${url}' matched`,
text: `Regex '${regex}' for url '${url}' matched`
});
}
});
};