Skip to content

Commit 891275b

Browse files
author
pierrick
committed
feat: add OOO assitant sample
1 parent c191441 commit 891275b

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "scriptId": "16L_UmGrkrDKYWrfw9YlnUnnnWOMBEWywyPrZDZIQqKF17Q97RtZeinqn" }

solutions/ooo-assistant/Chat.gs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const APP_COMMAND = "app command";
2+
3+
/**
4+
* Responds to an ADDED_TO_SPACE event in Google Chat.
5+
* @param {Object} event the event object from Google Workspace Add On
6+
*/
7+
function onAddToSpace(event) {
8+
return sendCreateMessageAction(createCardMessage(help(APP_COMMAND)));
9+
}
10+
11+
/**
12+
* Responds to a MESSAGE event in Google Chat.
13+
* @param {Object} event the event object from Google Workspace Add On
14+
*/
15+
function onMessage(event) {
16+
return sendCreateMessageAction(createCardMessage(help(APP_COMMAND)));
17+
}
18+
19+
/**
20+
* Responds to a APP_COMMAND event in Google Chat.
21+
* @param {Object} event the event object from Google Workspace Add On
22+
*/
23+
function onAppCommand(event) {
24+
switch (event.chat.appCommandPayload.appCommandMetadata.appCommandId) {
25+
case 2: // Block out day
26+
return sendCreateMessageAction(createCardMessage(blockDayOut()));
27+
case 3: // Set auto reply
28+
return sendCreateMessageAction(createCardMessage(setAutoReply()));
29+
default: // Help, any other
30+
return sendCreateMessageAction(createCardMessage(help(APP_COMMAND)));
31+
}
32+
}
33+
34+
/**
35+
* Responds to a REMOVED_FROM_SPACE event in Google Chat.
36+
* @param {Object} event the event object from Google Workspace Add On
37+
*/
38+
function onRemoveFromSpace(event) {
39+
const space = event.chat.removedFromSpacePayload.space;
40+
console.info(`Chat app removed from ${(space.name || "this chat")}`);
41+
}
42+
43+
// ----------------------
44+
// Util functions
45+
// ----------------------
46+
47+
function createTextMessage(text) { return { text: text }; }
48+
49+
function createCardMessage(card) { return { cardsV2: [{ card: card }]}; }
50+
51+
function sendCreateMessageAction(message) {
52+
return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: message }}}};
53+
}

solutions/ooo-assistant/Common.gs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const UNIVERSAL_ACTION = "universal action";
2+
3+
// ----------------------
4+
// Homepage util functions
5+
// ----------------------
6+
7+
/**
8+
* Responds to any homepage load request in Google Workspace UIs.
9+
*/
10+
function onHomepage() {
11+
return help();
12+
}
13+
14+
// ----------------------
15+
// Action util functions
16+
// ----------------------
17+
18+
// Help action: Show add-on details.
19+
function help(featureName = UNIVERSAL_ACTION) {
20+
return {
21+
header: addOnCardHeader(),
22+
sections: [{ widgets: [{
23+
decoratedText: { text: "Hi! 👋 Feel free to use the following " + featureName + "s:", wrapText: true }}, {
24+
decoratedText: { text: "<b>⛔ Block day out</b>: I will block out your calendar for today.", wrapText: true }}, {
25+
decoratedText: { text: "<b>↩️ Set auto reply</b>: I will set an OOO auto reply in your Gmail.", wrapText: true }
26+
}]}]
27+
};
28+
}
29+
30+
// Block day out action: Adds an all-day event to the user's Google Calendar.
31+
function blockDayOut() {
32+
blockOutCalendar();
33+
return createActionResponseCard('Your calendar is now blocked out for today.')
34+
}
35+
36+
// Creates an OOO event in the user's Calendar.
37+
function blockOutCalendar() {
38+
function getDateAndHours(hour, minutes) {
39+
const date = new Date();
40+
date.setHours(hour);
41+
date.setMinutes(minutes);
42+
date.setSeconds(0);
43+
date.setMilliseconds(0);
44+
return date.toISOString();
45+
}
46+
47+
const event = {
48+
start: { dateTime: getDateAndHours(9, 0) },
49+
end: { dateTime: getDateAndHours(17, 0) },
50+
eventType: 'outOfOffice',
51+
summary: 'OOO',
52+
outOfOfficeProperties: {
53+
autoDeclineMode: 'declineOnlyNewConflictingInvitations',
54+
declineMessage: 'Declined because OOO.',
55+
}
56+
}
57+
Calendar.Events.insert(event, 'primary');
58+
}
59+
60+
// Set auto reply action: Set OOO auto reply in the user's Gmail .
61+
function setAutoReply() {
62+
turnOnAutoResponder();
63+
return createActionResponseCard('The out of office auto reply has been turned on.')
64+
}
65+
66+
// Turns on the user's vacation response for today in Gmail.
67+
function turnOnAutoResponder() {
68+
const ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
69+
const currentTime = (new Date()).getTime();
70+
Gmail.Users.Settings.updateVacation({
71+
enableAutoReply: true,
72+
responseSubject: 'I am OOO today',
73+
responseBodyHtml: 'I am OOO today.<br><br><i>Created by OOO Assistant add-on!</i>',
74+
restrictToContacts: true,
75+
restrictToDomain: true,
76+
startTime: currentTime,
77+
endTime: currentTime + ONE_DAY_MILLIS
78+
}, 'me');
79+
}
80+
81+
// ----------------------
82+
// Card util functions
83+
// ----------------------
84+
85+
function addOnCardHeader() {
86+
return {
87+
title: "OOO Assistant",
88+
subtitle: "Helping manage your OOO",
89+
imageUrl: "https://goo.gle/3SfMkjb",
90+
};
91+
}
92+
93+
// Create an action response card
94+
function createActionResponseCard(text) {
95+
return {
96+
header: addOnCardHeader(),
97+
sections: [{ widgets: [{ decoratedText: {
98+
startIcon: { iconUrl: "https://fonts.gstatic.com/s/i/short-term/web/system/1x/task_alt_gm_grey_48dp.png" },
99+
text: text,
100+
wrapText: true
101+
}}]}]
102+
};
103+
}
104+
105+
// ----------------------
106+
// Universal action util functions
107+
// ----------------------
108+
109+
function respondToUniversalAction(card) {
110+
return CardService.newUniversalActionResponseBuilder().displayAddOnCards([card]).build();
111+
}

solutions/ooo-assistant/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Build a Google Workspace add-on extending all Google Workspace UIs
2+
3+
**Warning:** This sample build a Chat app as a Google Workspace add-on. It's only available in the [Developer Preview Program](https://developers.google.com/workspace/preview).
4+
5+
The add-on extends the following Google Workspace UIs: Chat, Calendar, Gmail, Drive, Docs, Sheets, and Slides.
6+
7+
It relies on app commands in Chat, and homepage and universal actions in the others.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"timeZone": "America/New_York",
3+
"exceptionLogging": "STACKDRIVER",
4+
"runtimeVersion": "V8",
5+
"dependencies": {
6+
"enabledAdvancedServices": [{
7+
"userSymbol": "Gmail",
8+
"version": "v1",
9+
"serviceId": "gmail"
10+
},
11+
{
12+
"userSymbol": "Calendar",
13+
"version": "v3",
14+
"serviceId": "calendar"
15+
}]
16+
},
17+
"addOns": {
18+
"common": {
19+
"name": "OOO Assistant",
20+
"logoUrl": "https://goo.gle/3SfMkjb",
21+
"homepageTrigger": {
22+
"runFunction": "onHomepage"
23+
},
24+
"universalActions": [{
25+
"label": "Block day out",
26+
"runFunction": "blockDayOut"
27+
}, {
28+
"label": "Set auto reply",
29+
"runFunction": "setAutoReply"
30+
}]
31+
},
32+
"chat": {},
33+
"calendar": {},
34+
"gmail": {},
35+
"drive": {},
36+
"docs": {},
37+
"sheets": {},
38+
"slides": {}
39+
}
40+
}

0 commit comments

Comments
 (0)