Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions solutions/ooo-assistant/.clasp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "scriptId": "16L_UmGrkrDKYWrfw9YlnUnnnWOMBEWywyPrZDZIQqKF17Q97RtZeinqn" }
69 changes: 69 additions & 0 deletions solutions/ooo-assistant/Chat.gs
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 }}}};
}
127 changes: 127 additions & 0 deletions solutions/ooo-assistant/Common.gs
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();
}
7 changes: 7 additions & 0 deletions solutions/ooo-assistant/README.md
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.
40 changes: 40 additions & 0 deletions solutions/ooo-assistant/appsscript.json
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": {}
}
}
Loading