-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
112 lines (89 loc) · 3.63 KB
/
Copy pathCode.js
File metadata and controls
112 lines (89 loc) · 3.63 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function createTaskFromStarred() {
const todoistToken = PropertiesService.getScriptProperties().getProperty('TODOIST_API_TOKEN');
if (!todoistToken) throw new Error("Todoist API token not found. Run setTodoistToken() first.");
const threads = GmailApp.search('is:starred');
threads.forEach(thread => {
const messages = thread.getMessages();
if (!messages || messages.length === 0) return;
messages.forEach(message => {
if (!message || !message.isStarred()) return;
const subject = message.getSubject();
const messageId = message.getId();
const link = `https://mail.google.com/mail/u/0/?shva=1#inbox/${messageId}`;
const rawBody = extractCleanBodySimple(message);
const bodyText = cleanEmailBody(rawBody);
const payload = {
content: subject + ' @starred',
description: `[View original email](${link})\n\n${bodyText}`
};
const response = UrlFetchApp.fetch('https://api.todoist.com/api/v1/tasks', {
method: 'POST',
contentType: 'application/json',
headers: {
Authorization: `Bearer ${todoistToken}`
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
if (response.getResponseCode() !== 200) {
console.log('Error response:', response.getContentText());
throw new Error(`API request failed with code ${response.getResponseCode()}: ${response.getContentText()}`);
}
message.unstar();
});
});
}
function createTrigger() {
ScriptApp.newTrigger('createTaskFromStarred')
.timeBased()
.everyMinutes(1)
.create();
console.log('Trigger created successfully - will run every minute');
}
function extractCleanBodySimple(message) {
if (!message || typeof message.getPlainBody !== 'function') return '';
const plain = message.getPlainBody();
if (plain && plain.trim().length > 20) return plain;
let html = message.getBody();
html = html.replace(/<style[\s\S]*?<\/style>/gi, '');
html = html.replace(/<script[\s\S]*?<\/script>/gi, '');
html = html.replace(/<\/?[^>]+(>|$)/g, ''); // strip tags
return html.replace(/\s+/g, ' ').trim().slice(0, 3000);
}
function walkHtmlAndExtract(element) {
const tag = element.getName().toLowerCase();
const invisible = ['style', 'script', 'head', 'meta', 'noscript'];
if (invisible.includes(tag)) return '';
// Special case: link conversion
if (tag === 'a' && element.getAttribute('href')) {
const url = element.getAttribute('href').getValue();
const text = extractTextContent(element);
return `[${text}](${url})`;
}
// Aggregate text and recurse into children
let text = '';
if (element.getText()) text += element.getText();
const children = element.getChildren();
for (let i = 0; i < children.length; i++) {
text += walkHtmlAndExtract(children[i]);
}
return text.replace(/\s+/g, ' ').trim() + ' ';
}
function extractTextContent(element) {
let text = element.getText() || '';
element.getChildren().forEach(child => {
text += extractTextContent(child);
});
return text.trim();
}
function cleanEmailBody(bodyText) {
// Remove long bare URLs (especially trackers)
bodyText = bodyText.replace(/https?:\/\/[^\s]*?(list\.[^\s]+|actionnetwork\.org)[^\s)]+/gi, '');
// Remove lines that include unsubscribe/update contact
bodyText = bodyText.replace(/^.*(unsubscribe|update.*contact|privacy policy|stop receiving).*$/gim, '');
// Optional: strip after first unsubscribe mention
const cutIndex = bodyText.search(/(unsubscribe|stop receiving)/i);
if (cutIndex > 0) bodyText = bodyText.slice(0, cutIndex);
// Clean up spacing
return bodyText.replace(/\n{3,}/g, '\n\n').trim();
}