-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add OOO assistant sample #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "scriptId": "16L_UmGrkrDKYWrfw9YlnUnnnWOMBEWywyPrZDZIQqKF17Q97RtZeinqn" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * 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"; | ||
|
|
||
| /** | ||
| * 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 }}}}; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * 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"; | ||
|
|
||
| // ---------------------- | ||
| // Homepage util functions | ||
| // ---------------------- | ||
|
|
||
| /** | ||
| * Responds to homepage load request. | ||
| */ | ||
| 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: "<b>⛔ Block day out</b>: I will block out your calendar for today.", wrapText: true }}, { | ||
| decoratedText: { text: "<b>↩️ Set auto reply</b>: 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.<br><br><i>Created by OOO Assistant add-on!</i>', | ||
| 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(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Build a Google Workspace add-on extending all UIs | ||
|
|
||
| **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. | ||
|
|
||
| It relies on app commands in Chat, and homepage and universal actions in the others. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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": {} | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.