-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.gs
96 lines (78 loc) · 2.92 KB
/
code.gs
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
function onGmailPage() {
var startdateWidget = CardService.newDatePicker()
.setTitle("Choose a start date")
.setFieldName("startDate")
.setValueInMsSinceEpoch(new Date().getTime());
var enddateWidget = CardService.newDatePicker()
.setTitle("Choose an end date")
.setFieldName("endDate")
.setValueInMsSinceEpoch(new Date().getTime());
var applyButton = CardService.newTextButton()
.setText("Apply")
.setOnClickAction(CardService.newAction()
.setFunctionName("handleApply"));
var submitButton = CardService.newTextButton()
.setText("Submit")
.setOnClickAction(CardService.newAction()
.setFunctionName("handleSubmit"));
var section = CardService.newCardSection()
.addWidget(startdateWidget)
.addWidget(enddateWidget)
.addWidget(submitButton)
.addWidget(CardService.newTextParagraph().setText("Maximum email limit: ~200"));
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Welcome to Nico's Email Summarizer"))
.addSection(CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText("Select a start and end date of emails to summarise.")))
.addSection(section)
.build();
return card;
}
function sendToLambdaAsync(payload) {
var url = 'LAMBDA_URL';
var options = {
method: 'post',
contentType: 'application/json',
payload: payload,
muteHttpExceptions: true
};
// Execute the URL Fetch call asynchronously
UrlFetchApp.fetch(url, options);
}
function createProcessingCard() {
var textToDisplay = "Process started. You should receive an email once the process is complete.";
return CardService.newCardBuilder()
.addSection(CardService.newCardSection()
.addWidget(CardService.newTextParagraph().setText(textToDisplay))
).build();
}
function handleSubmit(e) {
var startdateMs = e.formInput.startDate.msSinceEpoch;
var startdate = new Date(startdateMs);
var startdateiso = startdate.toISOString();
var enddateMs = e.formInput.endDate.msSinceEpoch;
var enddate = new Date(enddateMs);
var enddateiso = enddate.toISOString();
var scriptToken = ScriptApp.getOAuthToken();
var payload = JSON.stringify({
start_date: startdateiso,
end_date: enddateiso,
gmail_access_token: scriptToken
});
sendToLambdaAsync(payload);
// Immediately update the UI after starting the process
return createProcessingCard();
console.log(payload)
}
// var response = UrlFetchApp.fetch('LAMBDA_URL', {
// method: 'post',
// contentType: 'application/json',
// payload: payload,
// muteHttpExceptions: true
// });
// textToDisplay = "Process started. You will receive an email once the process is complete."
// return CardService.newCardBuilder()
// .addSection(CardService.newCardSection()
// .addWidget(CardService.newTextParagraph().setText(textToDisplay))
// ).build();
// }