From 891275b76ee57f09030fd830e4897792dd95288c Mon Sep 17 00:00:00 2001 From: pierrick Date: Wed, 14 May 2025 18:39:38 +0000 Subject: [PATCH 1/5] feat: add OOO assitant sample --- solutions/ooo-assistant/.clasp.json | 1 + solutions/ooo-assistant/Chat.gs | 53 +++++++++++ solutions/ooo-assistant/Common.gs | 111 ++++++++++++++++++++++++ solutions/ooo-assistant/README.md | 7 ++ solutions/ooo-assistant/appsscript.json | 40 +++++++++ 5 files changed, 212 insertions(+) create mode 100644 solutions/ooo-assistant/.clasp.json create mode 100644 solutions/ooo-assistant/Chat.gs create mode 100644 solutions/ooo-assistant/Common.gs create mode 100644 solutions/ooo-assistant/README.md create mode 100644 solutions/ooo-assistant/appsscript.json diff --git a/solutions/ooo-assistant/.clasp.json b/solutions/ooo-assistant/.clasp.json new file mode 100644 index 000000000..e10a8d490 --- /dev/null +++ b/solutions/ooo-assistant/.clasp.json @@ -0,0 +1 @@ +{ "scriptId": "16L_UmGrkrDKYWrfw9YlnUnnnWOMBEWywyPrZDZIQqKF17Q97RtZeinqn" } diff --git a/solutions/ooo-assistant/Chat.gs b/solutions/ooo-assistant/Chat.gs new file mode 100644 index 000000000..7392d83e5 --- /dev/null +++ b/solutions/ooo-assistant/Chat.gs @@ -0,0 +1,53 @@ +const APP_COMMAND = "app command"; + +/** + * Responds to an ADDED_TO_SPACE event in Google Chat. + * @param {Object} event the event object from Google Workspace Add On + */ +function onAddToSpace(event) { + return sendCreateMessageAction(createCardMessage(help(APP_COMMAND))); +} + +/** + * Responds to a MESSAGE event in Google Chat. + * @param {Object} event the event object from Google Workspace Add On + */ +function onMessage(event) { + return sendCreateMessageAction(createCardMessage(help(APP_COMMAND))); +} + +/** + * Responds to a APP_COMMAND event in Google Chat. + * @param {Object} event the event object from Google Workspace Add On + */ +function onAppCommand(event) { + switch (event.chat.appCommandPayload.appCommandMetadata.appCommandId) { + case 2: // Block out day + return sendCreateMessageAction(createCardMessage(blockDayOut())); + case 3: // Set auto reply + return sendCreateMessageAction(createCardMessage(setAutoReply())); + default: // Help, any other + return sendCreateMessageAction(createCardMessage(help(APP_COMMAND))); + } +} + +/** + * Responds to a REMOVED_FROM_SPACE event in Google Chat. + * @param {Object} event the event object from Google Workspace Add On + */ +function onRemoveFromSpace(event) { + const space = event.chat.removedFromSpacePayload.space; + console.info(`Chat app removed from ${(space.name || "this chat")}`); +} + +// ---------------------- +// Util functions +// ---------------------- + +function createTextMessage(text) { return { text: text }; } + +function createCardMessage(card) { return { cardsV2: [{ card: card }]}; } + +function sendCreateMessageAction(message) { + return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: message }}}}; +} diff --git a/solutions/ooo-assistant/Common.gs b/solutions/ooo-assistant/Common.gs new file mode 100644 index 000000000..c59a635f4 --- /dev/null +++ b/solutions/ooo-assistant/Common.gs @@ -0,0 +1,111 @@ +const UNIVERSAL_ACTION = "universal action"; + +// ---------------------- +// Homepage util functions +// ---------------------- + +/** + * Responds to any homepage load request in Google Workspace UIs. + */ +function onHomepage() { + return help(); +} + +// ---------------------- +// Action util functions +// ---------------------- + +// Help action: Show add-on details. +function help(featureName = UNIVERSAL_ACTION) { + return { + header: addOnCardHeader(), + sections: [{ widgets: [{ + decoratedText: { text: "Hi! 👋 Feel free to use the following " + featureName + "s:", wrapText: true }}, { + decoratedText: { text: "⛔ Block day out: I will block out your calendar for today.", wrapText: true }}, { + decoratedText: { text: "â†Šī¸ Set auto reply: I will set an OOO auto reply in your Gmail.", wrapText: true } + }]}] + }; +} + +// Block day out action: Adds an all-day event to the user's Google Calendar. +function blockDayOut() { + blockOutCalendar(); + return createActionResponseCard('Your calendar is now blocked out for today.') +} + +// Creates an OOO event in the user's Calendar. +function blockOutCalendar() { + function getDateAndHours(hour, minutes) { + const date = new Date(); + date.setHours(hour); + date.setMinutes(minutes); + date.setSeconds(0); + date.setMilliseconds(0); + return date.toISOString(); + } + + const event = { + start: { dateTime: getDateAndHours(9, 0) }, + end: { dateTime: getDateAndHours(17, 0) }, + eventType: 'outOfOffice', + summary: 'OOO', + outOfOfficeProperties: { + autoDeclineMode: 'declineOnlyNewConflictingInvitations', + declineMessage: 'Declined because OOO.', + } + } + Calendar.Events.insert(event, 'primary'); +} + +// Set auto reply action: Set OOO auto reply in the user's Gmail . +function setAutoReply() { + turnOnAutoResponder(); + return createActionResponseCard('The out of office auto reply has been turned on.') +} + +// Turns on the user's vacation response for today in Gmail. +function turnOnAutoResponder() { + const ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; + const currentTime = (new Date()).getTime(); + Gmail.Users.Settings.updateVacation({ + enableAutoReply: true, + responseSubject: 'I am OOO today', + responseBodyHtml: 'I am OOO today.

Created by OOO Assistant add-on!', + restrictToContacts: true, + restrictToDomain: true, + startTime: currentTime, + endTime: currentTime + ONE_DAY_MILLIS + }, 'me'); +} + +// ---------------------- +// Card util functions +// ---------------------- + +function addOnCardHeader() { + return { + title: "OOO Assistant", + subtitle: "Helping manage your OOO", + imageUrl: "https://goo.gle/3SfMkjb", + }; +} + +// Create an action response card +function createActionResponseCard(text) { + return { + header: addOnCardHeader(), + sections: [{ widgets: [{ decoratedText: { + startIcon: { iconUrl: "https://fonts.gstatic.com/s/i/short-term/web/system/1x/task_alt_gm_grey_48dp.png" }, + text: text, + wrapText: true + }}]}] + }; +} + +// ---------------------- +// Universal action util functions +// ---------------------- + +function respondToUniversalAction(card) { + return CardService.newUniversalActionResponseBuilder().displayAddOnCards([card]).build(); +} diff --git a/solutions/ooo-assistant/README.md b/solutions/ooo-assistant/README.md new file mode 100644 index 000000000..35cdc02a1 --- /dev/null +++ b/solutions/ooo-assistant/README.md @@ -0,0 +1,7 @@ +# Build a Google Workspace add-on extending all Google Workspace UIs + +**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). + +The add-on extends the following Google Workspace UIs: Chat, Calendar, Gmail, Drive, Docs, Sheets, and Slides. + +It relies on app commands in Chat, and homepage and universal actions in the others. diff --git a/solutions/ooo-assistant/appsscript.json b/solutions/ooo-assistant/appsscript.json new file mode 100644 index 000000000..8553c6087 --- /dev/null +++ b/solutions/ooo-assistant/appsscript.json @@ -0,0 +1,40 @@ +{ + "timeZone": "America/New_York", + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8", + "dependencies": { + "enabledAdvancedServices": [{ + "userSymbol": "Gmail", + "version": "v1", + "serviceId": "gmail" + }, + { + "userSymbol": "Calendar", + "version": "v3", + "serviceId": "calendar" + }] + }, + "addOns": { + "common": { + "name": "OOO Assistant", + "logoUrl": "https://goo.gle/3SfMkjb", + "homepageTrigger": { + "runFunction": "onHomepage" + }, + "universalActions": [{ + "label": "Block day out", + "runFunction": "blockDayOut" + }, { + "label": "Set auto reply", + "runFunction": "setAutoReply" + }] + }, + "chat": {}, + "calendar": {}, + "gmail": {}, + "drive": {}, + "docs": {}, + "sheets": {}, + "slides": {} + } +} From 751dad0f646ac7c08f9ef662fde2d63b8a6c6516 Mon Sep 17 00:00:00 2001 From: pierrick Date: Wed, 14 May 2025 18:42:27 +0000 Subject: [PATCH 2/5] Change title --- solutions/ooo-assistant/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/ooo-assistant/README.md b/solutions/ooo-assistant/README.md index 35cdc02a1..943f6701b 100644 --- a/solutions/ooo-assistant/README.md +++ b/solutions/ooo-assistant/README.md @@ -1,4 +1,4 @@ -# Build a Google Workspace add-on extending all Google Workspace UIs +# Build a Google Workspace add-on extending all UIs **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). From f3cbcbe8caaec3496952a17ce8b899615b6c4cf0 Mon Sep 17 00:00:00 2001 From: pierrick Date: Wed, 14 May 2025 18:46:50 +0000 Subject: [PATCH 3/5] Edit comment --- solutions/ooo-assistant/Common.gs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/ooo-assistant/Common.gs b/solutions/ooo-assistant/Common.gs index c59a635f4..cedfde3ef 100644 --- a/solutions/ooo-assistant/Common.gs +++ b/solutions/ooo-assistant/Common.gs @@ -5,7 +5,7 @@ const UNIVERSAL_ACTION = "universal action"; // ---------------------- /** - * Responds to any homepage load request in Google Workspace UIs. + * Responds to homepage load request. */ function onHomepage() { return help(); From efef9b21054d29070964564e8c24c747768c0438 Mon Sep 17 00:00:00 2001 From: pierrick Date: Wed, 14 May 2025 18:58:18 +0000 Subject: [PATCH 4/5] Add license info --- solutions/ooo-assistant/Chat.gs | 16 ++++++++++++++++ solutions/ooo-assistant/Common.gs | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/solutions/ooo-assistant/Chat.gs b/solutions/ooo-assistant/Chat.gs index 7392d83e5..13a51006f 100644 --- a/solutions/ooo-assistant/Chat.gs +++ b/solutions/ooo-assistant/Chat.gs @@ -1,3 +1,19 @@ +/** + * Copyright 2025 Google LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + const APP_COMMAND = "app command"; /** diff --git a/solutions/ooo-assistant/Common.gs b/solutions/ooo-assistant/Common.gs index cedfde3ef..7a3dd9fd1 100644 --- a/solutions/ooo-assistant/Common.gs +++ b/solutions/ooo-assistant/Common.gs @@ -1,3 +1,19 @@ +/** + * Copyright 2025 Google LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + const UNIVERSAL_ACTION = "universal action"; // ---------------------- From 15fef8bf5e50ed28156ec8473269a69549c1b7a7 Mon Sep 17 00:00:00 2001 From: Pierrick Voulet <6769971+PierrickVoulet@users.noreply.github.com> Date: Wed, 14 May 2025 16:03:47 -0400 Subject: [PATCH 5/5] Fix typo Co-authored-by: Vinay Vyas <69166360+vinay-google@users.noreply.github.com> --- solutions/ooo-assistant/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/ooo-assistant/README.md b/solutions/ooo-assistant/README.md index 943f6701b..86f4fa53d 100644 --- a/solutions/ooo-assistant/README.md +++ b/solutions/ooo-assistant/README.md @@ -1,6 +1,6 @@ # Build a Google Workspace add-on extending all UIs -**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). +**Warning:** This sample builds a Chat app as a Google Workspace add-on. It's only available in the [Developer Preview Program](https://developers.google.com/workspace/preview). The add-on extends the following Google Workspace UIs: Chat, Calendar, Gmail, Drive, Docs, Sheets, and Slides.