-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
64 lines (56 loc) · 2.1 KB
/
Code.gs
File metadata and controls
64 lines (56 loc) · 2.1 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const url = "YOUR_WEBHOOK_URL";
function startTrigger() {
ScriptApp.newTrigger('checkEmails').timeBased().everyMinutes(5).create();
resetLastRun();
}
function checkEmails() {
const lastRun = PropertiesService.getUserProperties().getProperty('lastRun');
const threads = GmailApp.getInboxThreads(0, 20);
const teamEmail = Session.getActiveUser().getEmail();
for (const thread of threads) {
const messages = thread.getMessages();
for (const message of messages) {
if (message.getDate().getTime() > lastRun) {
const from = message.getFrom();
if (!from.includes(teamEmail)) {
const date = message.getDate();
let subject = message.getSubject();
if (subject.length > 256) {
subject = subject.substring(0, 253) + "...";
}
let body = message.getPlainBody();
body = body.replace("\r\n\r\n", "");
if (body.length > 4096) {
body = body.substring(0, 4050) + "\n\n**email has been truncated due to length**";
}
const fields = [];
fields.push({'name': "From", 'value': from, 'inline': true});
fields.push({'name': "Date", 'value': date, 'inline': true});
announceEmbed(subject, body, fields, 16088613);
}
}
}
}
resetLastRun();
}
function resetLastRun() {
PropertiesService.getUserProperties().setProperty('lastRun', ""+new Date().getTime());
}
function announceEmbed(title, description, fields, color) {
const data = {
"embeds": [{
"title": title,
"description": description,
"color": color,
"fields": fields
}]
};
const options = {
method: "post",
payload: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}