-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
323 lines (284 loc) · 8.75 KB
/
main.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
type AiEmail = {
gmail_message: GoogleAppsScript.Gmail.GmailMessage;
text: string;
prompt?: string;
reply?: string;
};
type AiEmailWithPrompt = AiEmail & {prompt: string}
type AiEmailWithReply = AiEmailWithPrompt & {reply: string}
function getConfig(key: "email_address"|"emails_search_string"|"emails_exclude_string"|"OPENAI_API_KEY"|"reply_prompt"|"replied_label"|"noreplyneeded_label"): string {
const config_cells = {
"email_address": "B2",
"emails_search_string": "B3",
"emails_exclude_string": "B4",
"reply_prompt": "B5",
"OPENAI_API_KEY": "B6",
"replied_label": "B7",
"noreplyneeded_label": "B8",
}
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CONFIG")?.getRange(config_cells[key])?.getValues()[0][0];
}
/**
*
* Get the messages from Gmail.
* Assumption:
* - users will know the right search terms
* - replies will only be drafted on the last email of the thread
*
* @author fabriceb
* @date 2023-12-25
*/
function getEmails(maxThreads: number = 20): GoogleAppsScript.Gmail.GmailMessage[] {
const searchString = getConfig("emails_search_string") + ' ' + getConfig("emails_exclude_string");
let threads = GmailApp.search(searchString, 0, maxThreads);
let emails: GoogleAppsScript.Gmail.GmailMessage[] = threads.flatMap(
thread => thread.getMessages()[thread.getMessageCount() - 1]
);
return emails;
}
/**
*
* Adds some context to an email plain body by adding FROM, TO and SUBJECT at the top
* Note: email content is sliced to avoid the 50,000 character limit of google Sheet
*
* @author fabriceb
* @date 2023-12-25
*/
function extractTextFromEmail(email: GoogleAppsScript.Gmail.GmailMessage): AiEmail {
let from = email.getFrom();
let to = email.getTo();
let subject = email.getSubject();
let body = email.getPlainBody();
let text = `FROM: ${from}
TO: ${to}
SUBJECT: ${subject}
${body.substring(0, 48000)}`;
return {
gmail_message: email,
text: text
};
}
/**
*
* Combines prompt and email text
*
* @author fabriceb
* @date 2023-12-25
*/
function getPromptAndEmailText(email_text: string): string {
const reply_prompt: string = getConfig("reply_prompt");
return `${reply_prompt}
${email_text}`;
}
/**
*
* Adds prompt to an email
*
* @author fabriceb
* @date 2023-12-25
*/
function addReplyPrompt(email: AiEmail): AiEmailWithPrompt {
return {
gmail_message: email.gmail_message,
text: email.text,
prompt: getPromptAndEmailText(email.text)
};
}
/**
*
* Adds reply to an email
*
* @author fabriceb
* @date 2023-12-25
*/
function addReply(email: AiEmailWithPrompt): AiEmailWithReply {
return {
gmail_message: email.gmail_message,
text: email.text,
prompt: email.prompt,
reply: callOpenAI(email.prompt)
};
}
/**
*
* Calls OpenAI API to generate a reply
*
* @author fabriceb
* @date 2023-12-25
*/
function callOpenAI(prompt: string): string {
const OPENAI_API_KEY: string = getConfig("OPENAI_API_KEY");
const model = 'gpt-4-1106-preview'; // Choose the appropriate engine for your needs
const url = 'https://api.openai.com/v1/chat/completions'; // Chat completions endpoint
const payload = {
model: model,
messages: [
// Previous messages in the conversation (if any)
// {"role": "system", "content": "Your system message"},
{"role": "user", "content": prompt}
],
max_tokens: 150
};
const options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(payload),
'headers': {
'Authorization': 'Bearer ' + OPENAI_API_KEY
}
};
const response = UrlFetchApp.fetch(url, options);
const json = response.getContentText();
const data = JSON.parse(json);
Logger.log(data.choices[0].message.content); // Log the chat completion
return data.choices[0].message.content; // Return the chat completion
}
/**
*
* Helper function to put data in a column in the REPLIES sheet
*
* @author fabriceb
* @date 2023-12-25
*/
function writeInSpreadsheetColumn(data_list: any[], extractFunction: Function, column: number) {
if (data_list.length == 0) {
return;
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("REPLIES");
if (!sheet) {
return;
}
// Prepare the data as a 2D array (even if it's just one column)
const data: string[][] = data_list.map(data => [extractFunction(data)]);
const range = sheet.getRange(2, column, data.length, data[0].length);
range.setValues(data);
}
/**
*
* Converts plain text to HTML, written by ChatGPT4
*
* @author ChatGPT4 + fabriceb
* @date 2024-01-09
*/
function textToHtml(text: string): string {
// Replace special HTML characters with their corresponding HTML entities
const htmlText = text
.replace(/&/g, '&') // Ampersand
.replace(/</g, '<') // Less than
.replace(/>/g, '>') // Greater than
.replace(/"/g, '"') // Double quotes
.replace(/'/g, ''') // Single quote
.replace(/\n/g, '<br>'); // Replace newline characters with HTML line breaks
return htmlText;
}
/**
*
* creates the reply object in both plain text and HTML with signature
* Requires adding this dependency in appsscript.json
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Gmail",
"version": "v1",
"serviceId": "gmail"
}
]
},
* @author fabriceb
* @date 2023-01-08
*/
function convertToHtmlReply(reply_text: string): string
{
const signature: string | undefined = Gmail.Users.Settings.SendAs.get("me", getConfig("email_address")).signature;
return textToHtml(reply_text) + "<br>" + signature;
}
/**
*
* remove superfluous "from" email address from the cc list
* to counter a weird behaviour in createDraftReplyAll
*
* @author fabriceb
* @date 2023-01-08
*/
function cleanCcList(draft: GoogleAppsScript.Gmail.GmailDraft): GoogleAppsScript.Gmail.GmailDraft {
// There is a weird behaviour in createDraftReplyAll that automatically
// adds yourself to the CC list.
// So we are removing it here
let ccList = draft.getMessage().getCc().split(",");
let getFrom = draft.getMessage().getFrom();
ccList = ccList.filter(email_address => !email_address.includes(getFrom));
return draft.update(
draft.getMessage().getTo(), // Preserve the "To" recipients
draft.getMessage().getSubject(), // Preserve the subject
draft.getMessage().getPlainBody(), // Preserve the body
{
"htmlBody": draft.getMessage().getBody(), // Preserve the HTML body
"cc": ccList.join(","), // Updated "CC" recipients
}
);
}
/**
*
* create the draft reply in Gmail based on the reply generated by OpenAI
*
* @author fabriceb
* @date 2023-12-25
*/
function createDraftReplyInGmail(email: AiEmailWithReply): void {
let label: GoogleAppsScript.Gmail.GmailLabel = GmailApp.getUserLabelByName(getConfig("noreplyneeded_label"));
if (email.reply !== "NO REPLY NEEDED") {
let draft = email.gmail_message.createDraftReplyAll(
email.reply,
{
"htmlBody": convertToHtmlReply(email.reply),
}
);
cleanCcList(draft);
label = GmailApp.getUserLabelByName(getConfig("replied_label"));
}
label.addToThread(email.gmail_message.getThread());
}
/**
*
* 1. Downloads the last unread emails
* 2. Puts them in the spreadsheet
* 3. Creates the prompt to generate replies
* 4. Generates replies
* 5. Puts them in the spreadsheet
*
* @author fabriceb
* @date 2023-12-25
*/
function writeEmailRepliesToSheet(): void {
// get last unread emails
const emails = getEmails();
const ai_mails: AiEmail[] = emails.map(extractTextFromEmail);
const ai_mails_with_prompt: AiEmailWithPrompt[] = ai_mails.map(addReplyPrompt);
writeInSpreadsheetColumn(ai_mails_with_prompt, ai_mail => ai_mail.text, 1);
const ai_mails_with_replies: AiEmailWithReply[] = ai_mails_with_prompt.map(addReply);
writeInSpreadsheetColumn(ai_mails_with_replies, ai_mail => ai_mail.reply, 2);
// generated the draf replies in Gmail
ai_mails_with_replies.map(createDraftReplyInGmail);
}
/*
*
* 1. Reads the content of the active cell
* @author fabriceb
* @date 2023-12-25
* 2. Uses callOpenAI to get a reply
* 3. Puts the reply in the right cell
*/
function writeReplyToSheet(): void {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const cell = sheet.getActiveCell();
const prompt = getPromptAndEmailText(cell.getValue());
const reply = callOpenAI(prompt);
sheet.getRange(cell.getRow(), cell.getColumn() + 1).setValue(reply);
}
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('GmailAiREplier')
.addItem('Download last 10 email threads', 'writeEmailRepliesToSheet')
.addItem('Reply to the current email', 'writeReplyToSheet')
.addToUi();
}