Skip to content

Commit 0b1806b

Browse files
committed
fix: Updated code files to match latest working code in CloudSkillsBost. Updated README.md.
1 parent 101ce15 commit 0b1806b

File tree

6 files changed

+226
-189
lines changed

6 files changed

+226
-189
lines changed

gmail-sentiment-analysis/Cards.gs

Lines changed: 83 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,93 @@ limitations under the License.
1515
*/
1616

1717
/**
18-
* Builds the card to display in the side panel of gmail.
19-
* @return {CardService.Card} The card to show to the user.
18+
* Builds the main card displayed on the Gmail homepage.
19+
*
20+
* @returns {Card} - The homepage card.
2021
*/
21-
2222
function buildHomepageCard() {
23-
const imageUrl = 'https://fonts.gstatic.com/s/i/googlematerialicons/dynamic_feed/v6/black-24dp/1x/gm_dynamic_feed_black_24dp.png';
24-
25-
const cardHeader = CardService.newCardHeader()
26-
.setImageUrl(imageUrl)
27-
.setImageStyle(CardService.ImageStyle.CIRCLE)
28-
.setTitle("Analyze your Gmail");
29-
30-
const analyzeSentimentAction = CardService.newAction()
31-
.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()
39-
.setFunctionName('generateSampleEmails');
40-
41-
const generateSampleEmailsBtn = CardService.newTextButton()
42-
.setText('Generate sample emails')
43-
.setOnClickAction(generateSampleEmailAction)
44-
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
45-
.setBackgroundColor('#34A853');
46-
47-
const buttonSet = CardService.newButtonSet()
48-
.addButton(generateSampleEmailsBtn)
49-
.addButton(analyzeSentimentBtn);
50-
51-
const section = CardService.newCardSection()
52-
.addWidget(buttonSet);
53-
54-
const card = CardService.newCardBuilder()
55-
.setHeader(cardHeader)
56-
.addSection(section);
57-
58-
return card.build();
23+
// Create a new card builder
24+
const cardBuilder = CardService.newCardBuilder();
25+
26+
// Create a card header
27+
const cardHeader = CardService.newCardHeader();
28+
cardHeader.setImageUrl('https://fonts.gstatic.com/s/i/googlematerialicons/mail/v6/black-24dp/1x/gm_mail_black_24dp.png');
29+
cardHeader.setImageStyle(CardService.ImageStyle.CIRCLE);
30+
cardHeader.setTitle("Analyze your Gmail");
31+
32+
// Add the header to the card
33+
cardBuilder.setHeader(cardHeader);
34+
35+
// Create a card section
36+
const cardSection = CardService.newCardSection();
37+
38+
// Create buttons for generating sample emails and analyzing sentiment
39+
const buttonSet = CardService.newButtonSet();
40+
41+
// Create "Generate sample emails" button
42+
const generateButton = createFilledButton('Generate sample emails', 'generateSampleEmails', '#34A853');
43+
buttonSet.addButton(generateButton);
44+
45+
// Create "Analyze emails" button
46+
const analyzeButton = createFilledButton('Analyze emails', 'analyzeSentiment', '#FF0000');
47+
buttonSet.addButton(analyzeButton);
48+
49+
// Add the button set to the section
50+
cardSection.addWidget(buttonSet);
51+
52+
// Add the section to the card
53+
cardBuilder.addSection(cardSection);
54+
55+
// Build and return the card
56+
return cardBuilder.build();
5957
}
6058

61-
function buildNotificationResponse(notificationText) {
62-
const notification = CardService.newNotification().setText(notificationText);
59+
/**
60+
* Creates a filled text button with the specified text, function, and color.
61+
*
62+
* @param {string} text - The text to display on the button.
63+
* @param {string} functionName - The name of the function to call when the button is clicked.
64+
* @param {string} color - The background color of the button.
65+
* @returns {TextButton} - The created text button.
66+
*/
67+
function createFilledButton(text, functionName, color) {
68+
// Create a new text button
69+
const textButton = CardService.newTextButton();
70+
71+
// Set the button text
72+
textButton.setText(text);
73+
74+
// Set the action to perform when the button is clicked
75+
const action = CardService.newAction();
76+
action.setFunctionName(functionName);
77+
textButton.setOnClickAction(action);
6378

64-
const actionResponse = CardService.newActionResponseBuilder()
65-
.setNotification(notification);
79+
// Set the button style to filled
80+
textButton.setTextButtonStyle(CardService.TextButtonStyle.FILLED);
6681

67-
return actionResponse.build();
82+
// Set the background color
83+
textButton.setBackgroundColor(color);
84+
85+
return textButton;
6886
}
87+
88+
/**
89+
* Creates a notification response with the specified text.
90+
*
91+
* @param {string} notificationText - The text to display in the notification.
92+
* @returns {ActionResponse} - The created action response.
93+
*/
94+
function buildNotificationResponse(notificationText) {
95+
// Create a new notification
96+
const notification = CardService.newNotification();
97+
notification.setText(notificationText);
98+
99+
// Create a new action response builder
100+
const actionResponseBuilder = CardService.newActionResponseBuilder();
101+
102+
// Set the notification for the action response
103+
actionResponseBuilder.setNotification(notification);
104+
105+
// Build and return the action response
106+
return actionResponseBuilder.build();
107+
}

gmail-sentiment-analysis/Code.gs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ limitations under the License.
1515
*/
1616

1717
/**
18-
* Callback for rendering the homepage card.
19-
* @return {CardService.Card} The card to show to the user.
18+
* Triggered when the add-on is opened from the Gmail homepage.
19+
*
20+
* @param {Object} e - The event object.
21+
* @returns {Card} - The homepage card.
2022
*/
2123
function onHomepageTrigger(e) {
2224
return buildHomepageCard();
23-
}
25+
}

gmail-sentiment-analysis/Gmail.gs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,95 +15,95 @@ limitations under the License.
1515
*/
1616

1717
/**
18-
* Callback for initiating the sentiment analysis.
19-
* @return {CardService.Card} The card to show to the user.
18+
* Analyzes the sentiment of the first 10 threads in the inbox
19+
* and labels them accordingly.
20+
*
21+
* @returns {ActionResponse} - A notification confirming completion.
2022
*/
21-
2223
function analyzeSentiment() {
24+
// Analyze and label emails
2325
analyzeAndLabelEmailSentiment();
26+
27+
// Return a notification
2428
return buildNotificationResponse("Successfully completed sentiment analysis");
2529
}
2630

2731
/**
28-
* Analyzes the sentiment of recent emails in the inbox and labels threads with
29-
* the appropriate sentiment label.
32+
* Analyzes the sentiment of emails and applies appropriate labels.
3033
*/
3134
function analyzeAndLabelEmailSentiment() {
32-
const positiveLabelName = "HAPPY TONE 😊";
33-
const neutralLabelName = "NEUTRAL TONE 😐";
34-
const negativeLabelName = "UPSET TONE 😡";
35-
const maxThreads = 10;
35+
// Define label names
36+
const labelNames = ["HAPPY TONE 😊", "NEUTRAL TONE 😐", "UPSET TONE 😡"];
3637

37-
// Get the label, or create it if it doesn't exist.
38-
const positiveLabel = GmailApp.getUserLabelByName(positiveLabelName) || GmailApp.createLabel(positiveLabelName);
39-
const neutralLabel = GmailApp.getUserLabelByName(neutralLabelName) || GmailApp.createLabel(neutralLabelName);
40-
const negativeLabel = GmailApp.getUserLabelByName(negativeLabelName) || GmailApp.createLabel(negativeLabelName);
38+
// Get or create labels for each sentiment
39+
const positiveLabel = GmailApp.getUserLabelByName(labelNames[0]) || GmailApp.createLabel(labelNames[0]);
40+
const neutralLabel = GmailApp.getUserLabelByName(labelNames[1]) || GmailApp.createLabel(labelNames[1]);
41+
const negativeLabel = GmailApp.getUserLabelByName(labelNames[2]) || GmailApp.createLabel(labelNames[2]);
4142

42-
// Get the first 'maxThreads' threads from the inbox.
43-
const threads = GmailApp.getInboxThreads(0, maxThreads);
43+
// Get the first 10 threads in the inbox
44+
const threads = GmailApp.getInboxThreads(0, 10);
4445

45-
// Process each thread.
46+
// Iterate through each thread
4647
for (const thread of threads) {
48+
// Iterate through each message in the thread
4749
const messages = thread.getMessages();
48-
49-
// Process each message within the thread.
5050
for (const message of messages) {
51-
const emailText = message.getPlainBody();
52-
const sentiment = processSentiment(emailText);
53-
54-
switch (sentiment) {
55-
case 'positive':
56-
thread.addLabel(positiveLabel);
57-
break;
58-
case 'neutral':
59-
thread.addLabel(neutralLabel);
60-
break;
61-
case 'negative':
62-
thread.addLabel(negativeLabel);
63-
break;
64-
default:
65-
break;
51+
// Get the plain text body of the message
52+
const emailBody = message.getPlainBody();
53+
54+
// Analyze the sentiment of the email body
55+
const sentiment = processSentiment(emailBody);
56+
57+
// Apply the appropriate label based on the sentiment
58+
if (sentiment === 'positive') {
59+
thread.addLabel(positiveLabel);
60+
} else if (sentiment === 'neutral') {
61+
thread.addLabel(neutralLabel);
62+
} else if (sentiment === 'negative') {
63+
thread.addLabel(negativeLabel);
6664
}
6765
}
6866
}
6967
}
7068

7169
/**
72-
* Create sample emails
70+
* Generates sample emails for testing the sentiment analysis.
71+
*
72+
* @returns {ActionResponse} - A notification confirming email generation.
7373
*/
7474
function generateSampleEmails() {
75-
// Get active user's email
75+
// Get the current user's email address
7676
const userEmail = Session.getActiveUser().getEmail();
7777

78-
// Send emails
79-
GmailApp.sendEmail(
80-
userEmail,
81-
'Thank you for amazing service!',
82-
'Hi, I really enjoyed working with you. Thank you again!',
78+
// Define sample emails
79+
const sampleEmails = [
8380
{
84-
name: 'Customer A',
81+
subject: 'Thank you for amazing service!',
82+
body: 'Hi, I really enjoyed working with you. Thank you again!',
83+
name: 'Customer A'
8584
},
86-
);
87-
88-
GmailApp.sendEmail(
89-
userEmail,
90-
'Request for information',
91-
'Hello, I need more information on your recent product launch. Thank you.',
9285
{
93-
name: 'Customer B',
86+
subject: 'Request for information',
87+
body: 'Hello, I need more information on your recent product launch. Thank you.',
88+
name: 'Customer B'
9489
},
95-
);
96-
97-
GmailApp.sendEmail(
98-
userEmail,
99-
'Complaint!',
100-
'',
10190
{
102-
name: 'Customer C',
91+
subject: 'Complaint!',
92+
body: '',
10393
htmlBody: `<p>Hello, You are late in delivery, again.</p>
104-
<p>Please contact me ASAP before I cancel our subscription.</p>`,
105-
},
106-
);
94+
<p>Please contact me ASAP before I cancel our subscription.</p>`,
95+
name: 'Customer C'
96+
}
97+
];
98+
99+
// Send each sample email
100+
for (const email of sampleEmails) {
101+
GmailApp.sendEmail(userEmail, email.subject, email.body, {
102+
name: email.name,
103+
htmlBody: email.htmlBody
104+
});
105+
}
107106

107+
// Return a notification
108108
return buildNotificationResponse("Successfully generated sample emails");
109-
}
109+
}

0 commit comments

Comments
 (0)