-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGmailToDiscord.js
60 lines (50 loc) · 1.79 KB
/
GmailToDiscord.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
function checkEmails() {
var webhookUrl = "URL_WEBHOOK_DISCORD"; // !!!!!!!!!!!!!!
var query = 'from:[email protected] is:unread'; // Configured for Distill Web Monitor
var threads = GmailApp.search(query, 0, 5);
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
if (!message.isUnread()) {
continue; // Ignore read messages
}
var subject = message.getSubject();
var body = message.getPlainBody();
// Divide the body into lines
var lines = body.split('\n');
// Check that there are at least two lines
if (lines.length >= 2) {
var secondLine = lines[1];
// Find the position of the string '<You>can unsubscribe'
var marker = '<You>can unsubscribe';
var index = secondLine.indexOf(marker);
// Extract text before marker
var desiredText;
if (index !== -1) {
desiredText = secondLine.substring(0, index);
} else {
// If marker not found, use entire line
desiredText = secondLine;
}
} else {
// If there is no second line, define desiredText as empty or a default message
var desiredText = '';
}
// Create payload for Discord
var payload = {
"content": "# " + subject + "\n <@ID_DISCORD>\n" + desiredText // !!!!!!!!!!!!!!
};
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
// Send message to Discord
UrlFetchApp.fetch(webhookUrl, options);
// Mark message as read
message.markRead();
}
}
}