Skip to content

Commit e6ca6da

Browse files
authored
refactor: Refactored UI, consolidated sample generation code updated add-on name add-on name (#518)
* refactor: Refactored UI, consolidated sample generation code updated add-on name add-on name * fix: reverted sample code to previous version * fix: updated to correct code, switched to use flash model * fix: placed project placeholder back
1 parent f9f8895 commit e6ca6da

File tree

6 files changed

+80
-105
lines changed

6 files changed

+80
-105
lines changed

gmail-sentiment-analysis/Cards.gs

+22-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2024 Google LLC
2+
Copyright 2024-2025 Google LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -20,52 +20,46 @@ limitations under the License.
2020
* @return {CardService.Card} The card to show to the user.
2121
*/
2222

23-
function buildCard_GmailHome(notifyOk = false) {
23+
function buildHomepageCard() {
2424
const imageUrl = 'https://fonts.gstatic.com/s/i/googlematerialicons/dynamic_feed/v6/black-24dp/1x/gm_dynamic_feed_black_24dp.png';
2525

2626
const cardHeader = CardService.newCardHeader()
2727
.setImageUrl(imageUrl)
2828
.setImageStyle(CardService.ImageStyle.CIRCLE)
29-
.setTitle("Analyze your GMail");
29+
.setTitle("Analyze your Gmail");
3030

31-
const action = CardService.newAction().setFunctionName('analyzeSentiment');
32-
const button = CardService.newTextButton()
33-
.setText('Identify angry customers')
34-
.setOnClickAction(action)
35-
.setTextButtonStyle(CardService.TextButtonStyle.FILLED);
31+
const analyzeSentimentAction = CardService.newAction().setFunctionName('analyzeSentiment');
32+
const analyzeSentimentBtn = CardService.newTextButton()
33+
.setText('Analyze emails')
34+
.setOnClickAction(analyzeSentimentAction)
35+
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
36+
.setBackgroundColor('#FF0000');
37+
38+
const generateSampleEmailAction = CardService.newAction().setFunctionName('generateSampleEmails');
3639

37-
const generateSampleEmailsAction = CardService.newAction()
38-
.setFunctionName('generateSampleEmails');
3940
const generateSampleEmailsBtn = CardService.newTextButton()
4041
.setText('Generate sample emails')
41-
.setOnClickAction(generateSampleEmailsAction)
42+
.setOnClickAction(generateSampleEmailAction)
4243
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
4344
.setBackgroundColor('#34A853');
4445

45-
const buttonSet = CardService.newButtonSet().addButton(button).addButton(generateSampleEmailsBtn);
46+
const buttonSet = CardService.newButtonSet().addButton(generateSampleEmailsBtn).addButton(analyzeSentimentBtn);
4647

4748
const section = CardService.newCardSection()
48-
.setHeader("Emails sentiment analysis")
4949
.addWidget(buttonSet);
5050

5151
const card = CardService.newCardBuilder()
5252
.setHeader(cardHeader)
5353
.addSection(section);
5454

55-
/**
56-
* This builds the card that contains the footer that informs
57-
* the user about the successful execution of the Add-on.
58-
*/
59-
if (notifyOk) {
60-
const fixedFooter = CardService.newFixedFooter()
61-
.setPrimaryButton(
62-
CardService.newTextButton()
63-
.setText("Analysis complete")
64-
.setOnClickAction(
65-
CardService.newAction()
66-
.setFunctionName(
67-
"buildCard_GmailHome")));
68-
card.setFixedFooter(fixedFooter);
69-
}
7055
return card.build();
7156
}
57+
58+
function buildNotificationResponse(notificationText) {
59+
const notification = CardService.newNotification().setText(notificationText);
60+
61+
const actionResponse = CardService.newActionResponseBuilder()
62+
.setNotification(notification);
63+
64+
return actionResponse.build();
65+
}

gmail-sentiment-analysis/Code.gs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ limitations under the License.
1818
* Callback for rendering the homepage card.
1919
* @return {CardService.Card} The card to show to the user.
2020
*/
21-
function onHomepage(e) {
22-
return buildCard_GmailHome();
23-
}
21+
function onHomepageTrigger(e) {
22+
return buildHomepageCard();
23+
}

gmail-sentiment-analysis/Gmail.gs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2024 Google LLC
2+
Copyright 2024-2025 Google LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ limitations under the License.
2121

2222
function analyzeSentiment() {
2323
analyzeAndLabelEmailSentiment();
24-
return buildCard_GmailHome(true);
24+
return buildNotificationResponse("Successfully completed sentiment analysis");
2525
}
2626

2727
/**
@@ -63,3 +63,43 @@ function analyzeAndLabelEmailSentiment() {
6363
function isNegativeSentiment(text) {
6464
return processSentiment(text) === 'negative';
6565
}
66+
67+
/**
68+
* Create sample emails
69+
*/
70+
function generateSampleEmails() {
71+
// Get active user's email
72+
const userEmail = Session.getActiveUser().getEmail();
73+
74+
// Send emails
75+
GmailApp.sendEmail(
76+
userEmail,
77+
'Thank you for amazing service!',
78+
'Hi, I really enjoyed working with you. Thank you again!',
79+
{
80+
name: 'Customer A',
81+
},
82+
);
83+
84+
GmailApp.sendEmail(
85+
userEmail,
86+
'Request for information',
87+
'Hello, I need more information on your recent product launch. Thank you.',
88+
{
89+
name: 'Customer B',
90+
},
91+
);
92+
93+
GmailApp.sendEmail(
94+
userEmail,
95+
'Complaint!',
96+
'',
97+
{
98+
name: 'Customer C',
99+
htmlBody: `<p>Hello, You are late in delivery, again.</p>
100+
<p>Please contact me ASAP before I cancel our subscription.</p>`,
101+
},
102+
);
103+
104+
return buildNotificationResponse("Successfully generated sample emails");
105+
}

gmail-sentiment-analysis/Samples.gs

-55
This file was deleted.

gmail-sentiment-analysis/Vertex.gs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2024 Google LLC
2+
Copyright 2024-2025 Google LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
const PROJECT_ID = '[ADD YOUR GCP PROJECT ID HERE]';
1818
const VERTEX_AI_LOCATION = 'us-central1';
19-
const MODEL_ID = 'gemini-1.5-pro-002';
19+
const MODEL_ID = 'gemini-1.5-flash';
2020

2121
/**
2222
* Packages prompt and necessary settings, then sends a request to
@@ -67,16 +67,12 @@ function processSentiment(emailText) {
6767
payload: JSON.stringify(request),
6868
}
6969

70-
const url =
71-
`https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/` +
72-
`projects/${PROJECT_ID}/` +
73-
`locations/${VERTEX_AI_LOCATION}/` +
74-
`publishers/google/` +
75-
`models/${MODEL_ID}:generateContent`;
70+
const url = `https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/`
71+
+ `locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:generateContent`
7672

7773
const response = UrlFetchApp.fetch(url, fetchOptions);
7874
const payload = JSON.parse(response.getContentText());
7975
const text = JSON.parse(payload.candidates[0].content.parts[0].text);
8076

8177
return text.response;
82-
}
78+
}
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
2-
"timeZone": "America/Los_Angeles",
2+
"timeZone": "America/Toronto",
33
"oauthScopes": [
4+
"https://www.googleapis.com/auth/script.external_request",
45
"https://www.googleapis.com/auth/cloud-platform",
6+
"https://www.googleapis.com/auth/script.locale",
7+
"https://www.googleapis.com/auth/userinfo.email",
58
"https://www.googleapis.com/auth/gmail.addons.execute",
69
"https://www.googleapis.com/auth/gmail.labels",
7-
"https://www.googleapis.com/auth/gmail.modify",
8-
"https://www.googleapis.com/auth/script.external_request",
9-
"https://www.googleapis.com/auth/script.locale",
10-
"https://www.googleapis.com/auth/userinfo.email"
10+
"https://www.googleapis.com/auth/gmail.modify"
1111
],
1212
"addOns": {
1313
"common": {
14-
"name": "Productivity toolbox",
14+
"name": "Sentiment Analysis",
1515
"logoUrl": "https://fonts.gstatic.com/s/i/googlematerialicons/dynamic_feed/v6/black-24dp/1x/gm_dynamic_feed_black_24dp.png",
1616
"useLocaleFromApp": true
1717
},
1818
"gmail": {
1919
"homepageTrigger": {
20-
"runFunction": "onHomepage",
20+
"runFunction": "onHomepageTrigger",
2121
"enabled": true
2222
}
2323
}
2424
},
2525
"exceptionLogging": "STACKDRIVER",
2626
"runtimeVersion": "V8"
27-
}
27+
}

0 commit comments

Comments
 (0)