Skip to content

Commit 1452ff4

Browse files
committed
refactor: Refactored UI, consolidated sample generation code updated add-on name add-on name
1 parent f9f8895 commit 1452ff4

File tree

6 files changed

+98
-176
lines changed

6 files changed

+98
-176
lines changed

gmail-sentiment-analysis/Cards.gs

Lines changed: 22 additions & 28 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 40 additions & 2 deletions
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,41 @@ 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+
"Hello, You are late in delivery, again. Please contact me ASAP before I cancel our subscription.",
97+
{
98+
name: 'Customer C',
99+
},
100+
);
101+
102+
return buildNotificationResponse("Successfully generated sample emails");
103+
}

gmail-sentiment-analysis/Samples.gs

Lines changed: 0 additions & 55 deletions
This file was deleted.

gmail-sentiment-analysis/Vertex.gs

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,27 @@
1-
/*
2-
Copyright 2024 Google LLC
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
https://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
17-
const PROJECT_ID = '[ADD YOUR GCP PROJECT ID HERE]';
18-
const VERTEX_AI_LOCATION = 'us-central1';
19-
const MODEL_ID = 'gemini-1.5-pro-002';
20-
21-
/**
22-
* Packages prompt and necessary settings, then sends a request to
23-
* Vertex API.
24-
* A check is performed to see if the response from Vertex AI contains FALSE as a value.
25-
* Returns the outcome of that check which is a boolean.
26-
*
27-
* @param emailText - Email message that is sent to the model.
28-
*/
29-
30-
function processSentiment(emailText) {
31-
const prompt = `Analyze the sentiment of the following message: ${emailText}`;
32-
33-
const request = {
34-
"contents": [{
35-
"role": "user",
36-
"parts": [{
37-
"text": prompt,
38-
}]
39-
}],
40-
"generationConfig": {
41-
"temperature": 0.9,
42-
"maxOutputTokens": 1024,
43-
"responseMimeType": "application/json",
44-
"responseSchema": {
45-
"type": "object",
46-
"properties": {
47-
"response": {
48-
"type": "string",
49-
"enum": [
50-
"positive",
51-
"negative",
52-
"neutral",
53-
]
54-
}
55-
}
1+
{
2+
"timeZone": "America/Toronto",
3+
"oauthScopes": [
4+
"https://www.googleapis.com/auth/script.external_request",
5+
"https://www.googleapis.com/auth/cloud-platform",
6+
"https://www.googleapis.com/auth/script.locale",
7+
"https://www.googleapis.com/auth/userinfo.email",
8+
"https://www.googleapis.com/auth/gmail.addons.execute",
9+
"https://www.googleapis.com/auth/gmail.labels",
10+
"https://www.googleapis.com/auth/gmail.modify"
11+
],
12+
"addOns": {
13+
"common": {
14+
"name": "Sentiment Analysis",
15+
"logoUrl": "https://fonts.gstatic.com/s/i/googlematerialicons/dynamic_feed/v6/black-24dp/1x/gm_dynamic_feed_black_24dp.png",
16+
"useLocaleFromApp": true
17+
},
18+
"gmail": {
19+
"homepageTrigger": {
20+
"runFunction": "onHomepageTrigger",
21+
"enabled": true
5622
}
5723
}
58-
};
59-
60-
const fetchOptions = {
61-
method: 'POST',
62-
headers: {
63-
'Authorization': `Bearer ${ScriptApp.getOAuthToken()}`
64-
},
65-
contentType: 'application/json',
66-
muteHttpExceptions: true,
67-
payload: JSON.stringify(request),
68-
}
69-
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`;
76-
77-
const response = UrlFetchApp.fetch(url, fetchOptions);
78-
const payload = JSON.parse(response.getContentText());
79-
const text = JSON.parse(payload.candidates[0].content.parts[0].text);
80-
81-
return text.response;
82-
}
24+
},
25+
"exceptionLogging": "STACKDRIVER",
26+
"runtimeVersion": "V8"
27+
}
Lines changed: 8 additions & 8 deletions
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)