-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-network-message-writer.js
67 lines (59 loc) · 2.1 KB
/
action-network-message-writer.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
// Creates a new Action Network Email Message with the provided HTML-formatted text as the body.
// Values for Subject, Sender, Reply-To, Origin System, and Wrapper are obtained via defined Script Properties.
const draftANMessage = (doc, apiKey) => {
const emailReplyTo = scriptProperties.getProperty("AN_EMAIL_REPLY_TO");
const emailSender = scriptProperties.getProperty("AN_EMAIL_SENDER");
const emailWrapper = scriptProperties.getProperty("AN_EMAIL_WRAPPER");
const emailCreator = scriptProperties.getProperty("AN_EMAIL_CREATOR");
const emailTarget = scriptProperties.getProperty("EMAIL_TARGET");
const emailSubject = scriptProperties.getProperty("EMAIL_SUBJECT");
if (!apiKey) {
Logger.log('No Action Network Api Key "AN_API_KEY" provided, cannot continue.');
return;
}
if (!emailReplyTo) {
Logger.log('No Email Reply-To Address "AN_EMAIL_REPLY_TO" provided, cannot continue.');
return;
}
if (!emailSubject) {
Logger.log('No Email Subject "EMAIL_SUBJECT" provided, cannot continue.');
return;
}
const subject = `🌹 ${emailSubject} for ${Utilities.formatDate(new Date(), "UTC", "yyyy-MM-dd")} 🫎`;
const payload = {
subject,
body: doc,
from: emailSender,
origin_system: "ActionNetworkEventSync",
reply_to: emailReplyTo,
_links: {},
};
payload.targets = emailTarget
? [
{
href: `${apiUrlAn}queries/${emailTarget}`,
},
]
: undefined;
payload._links["osdi:wrapper"] = emailWrapper
? {
href: `${apiUrlAn}wrappers/${emailWrapper}`,
}
: undefined;
payload._links["osdi:creator"] = emailCreator
? {
href: `${apiUrlAn}people/${emailCreator}`,
}
: undefined;
const options = {
method: "post",
payload: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
"OSDI-API-Token": apiKey,
},
};
const response = UrlFetchApp.fetch(`${apiUrlAn}messages/`, options);
const actionNetworkId = getEventIDFromAN(JSON.parse(response), "action_network");
Logger.log(`Created Action Network Message ${actionNetworkId} with subject ${subject}.`);
};