|
| 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 | +} |
0 commit comments