This repository was archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (43 loc) · 1.54 KB
/
index.js
File metadata and controls
48 lines (43 loc) · 1.54 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
47
48
const fetch = require('node-fetch');
const sendSlackMessage = async (email, timestamp) => {
if (email && timestamp) {
const url = process.env.SLACK_WEBHOOK_URL;
const body = {
text: `New activist request:\nEmail: <mailto:${email}|${email}>\nTimestamp: *${timestamp}*\n\nPlease add a :white_check_mark: reaction to this message when you contact the new activist.`,
};
const params = {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
};
return await fetch(url, params);
}
return { ok: false };
};
exports.handler = async (event) => {
const { type, record, old_record } = event;
console.log(record);
if (type === 'INSERT') {
if (record) {
const { email, timestamp, accepted } = record;
if (accepted) {
const res = await sendSlackMessage(email, timestamp);
console.log('Sent message to Slack:', res.ok);
}
}
} else if (type === 'UPDATE') {
if (old_record && record) {
const { email, timestamp, accepted } = record;
const { accepted: old_accepted } = old_record;
if (old_accepted === false && accepted === true) {
const res = await sendSlackMessage(email, timestamp);
console.log('Sent message to Slack:', res.ok);
}
}
}
const response = {
statusCode: 200,
};
return response;
};