diff --git a/solutions/ooo-chat-app/Code.js b/solutions/ooo-chat-app/Code.js index 1ff83e3a0..da6f54b70 100644 --- a/solutions/ooo-chat-app/Code.js +++ b/solutions/ooo-chat-app/Code.js @@ -4,207 +4,232 @@ * @return {object} JSON-formatted response * @see https://developers.google.com/hangouts/chat/reference/message-formats/events */ - function onAddToSpace(event) { - let message = 'Thank you for adding me to '; - if (event.space.type === 'DM') { - message += 'a DM, ' + event.user.displayName + '!'; - } else { - message += event.space.displayName; - } - return { text: message }; - } - - /** - * Responds to a REMOVED_FROM_SPACE event in Chat. - * @param {object} event the event object from Chat - * @param {object} event the event object from Chat - * @see https://developers.google.com/hangouts/chat/reference/message-formats/events - */ - function onRemoveFromSpace(event) { - console.log('App removed from ', event.space.name); +function onAddToSpace(event) { + let message = 'Thank you for adding me to '; + if (event.space.type === 'DM') { + message += 'a DM, ' + event.user.displayName + '!'; + } else { + message += event.space.displayName; } - - - /** - * Responds to a MESSAGE event triggered in Chat. - * @param {object} event the event object from Chat - * @return {function} call the respective function - */ - function onMessage(event) { - const message = event.message; - - if (message.slashCommand) { - switch (message.slashCommand.commandId) { - case 1: // Help command - return createHelpCard(); - case 2: // Block out day command - return blockDayOut(); - case 3: // Cancel all meetings command - return cancelAllMeetings(); - case 4: // Set auto reply command - return setAutoReply(); - } + return { text: message }; +} + +/** + * Responds to a REMOVED_FROM_SPACE event in Chat. + * @param {object} event the event object from Chat + * @param {object} event the event object from Chat + * @see https://developers.google.com/hangouts/chat/reference/message-formats/events + */ +function onRemoveFromSpace(event) { + console.log('App removed from ', event.space.name); +} + + +/** + * Responds to a MESSAGE event triggered in Chat. + * @param {object} event the event object from Chat + * @return {function} call the respective function + */ +function onMessage(event) { + const message = event.message; + + if (message.slashCommand) { + switch (message.slashCommand.commandId) { + case 1: // Help command + return createHelpCard(); + case 2: // Block out day command + return blockDayOut(); + case 3: // Cancel all meetings command + return cancelAllMeetings(); + case 4: // Set auto reply command + return setAutoReply(); } } - - function createHelpCard() { - return { - "cardsV2": [ - { - "cardId": "2", - "card": { - "sections": [ - { - "header": "", - "widgets": [ - { - "decoratedText": { - "topLabel": "", - "text": "Hi! 👋 I'm here to help you with your out of office tasks.

Here's a list of commands I understand.", - "wrapText": true - } +} + +function createHelpCard() { + return { + "cardsV2": [ + { + "cardId": "2", + "card": { + "sections": [ + { + "header": "", + "widgets": [ + { + "decoratedText": { + "topLabel": "", + "text": "Hi! 👋 I'm here to help you with your out of office tasks.

Here's a list of commands I understand.", + "wrapText": true + } + } + ] + }, + { + "widgets": [ + { + "decoratedText": { + "topLabel": "", + "text": "/blockDayOut: I will block out your calendar for you.", + "wrapText": true } - ] - }, - { - "widgets": [ - { - "decoratedText": { - "topLabel": "", - "text": "/blockDayOut: I will block out your calendar for you.", - "wrapText": true - } - }, - { - "decoratedText": { - "topLabel": "", - "text": "/cancelAllMeetings: I will cancel all your meetings for the day.", - "wrapText": true - } - }, - { - "decoratedText": { - "topLabel": "", - "text": "/setAutoReply: Set an out of office auto reply in Gmail.", - "wrapText": true - } + }, + { + "decoratedText": { + "topLabel": "", + "text": "/cancelAllMeetings: I will cancel all your meetings for the day.", + "wrapText": true } - ] - } - ], - "header": { - "title": "OOO app", - "subtitle": "Helping you manage your OOO", - "imageUrl": "https://goo.gle/3SfMkjb", - "imageType": "SQUARE" + }, + { + "decoratedText": { + "topLabel": "", + "text": "/setAutoReply: Set an out of office auto reply in Gmail.", + "wrapText": true + } + } + ] } + ], + "header": { + "title": "OOO app", + "subtitle": "Helping you manage your OOO", + "imageUrl": "https://goo.gle/3SfMkjb", + "imageType": "SQUARE" } } - ] - } - } - - /** - * Adds an all day event to the users Google Calendar. - * @return {object} JSON-formatted response - */ - function blockDayOut() { - blockOutCalendar(); - return createResponseCard('Your calendar has been blocked out for you.') - } - - /** - * Cancels all of the users meeting for the current day. - * @return {object} JSON-formatted response - */ - function cancelAllMeetings() { - cancelMeetings(); - return createResponseCard('All your meetings have been canceled.') - } - - /** - * Sets an out of office auto reply in the users Gmail account. - * @return {object} JSON-formatted response - */ - function setAutoReply() { - turnOnAutoResponder(); - return createResponseCard('The out of office auto reply has been turned on.') - } - - - const ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; - - /** - * Places an all-day meeting on the user's Calendar. - */ - function blockOutCalendar() { - CalendarApp.createAllDayEvent('I am out of office today', new Date(), new Date(Date.now() + ONE_DAY_MILLIS)); - } - - /** - * Declines all meetings for the day. - */ - - function cancelMeetings() { - const events = CalendarApp.getEventsForDay(new Date()); - - events.forEach(function(event) { - if (event.getGuestList().length > 0) { - event.setMyStatus(CalendarApp.GuestStatus.NO); } - }); + ] } - - /** - * Turns on the user's vacation response for today in Gmail. - */ - function turnOnAutoResponder() { - const currentTime = (new Date()).getTime(); - Gmail.Users.Settings.updateVacation({ - enableAutoReply: true, - responseSubject: 'I am out of the office today', - responseBodyHtml: 'I am out of the office today; will be back on the next business day.

Created by OOO Chat app!', - restrictToContacts: true, - restrictToDomain: true, - startTime: currentTime, - endTime: currentTime + ONE_DAY_MILLIS - }, 'me'); +} + +/** + * Adds an all day event to the users Google Calendar. + * @return {object} JSON-formatted response + */ +function blockDayOut() { + blockOutCalendar(); + return createResponseCard('Your calendar has been blocked out for you.') +} + +/** + * Cancels all of the users meeting for the current day. + * @return {object} JSON-formatted response + */ +function cancelAllMeetings() { + cancelMeetings(); + return createResponseCard('All your meetings have been canceled.') +} + +/** + * Sets an out of office auto reply in the users Gmail account. + * @return {object} JSON-formatted response + */ +function setAutoReply() { + turnOnAutoResponder(); + return createResponseCard('The out of office auto reply has been turned on.') +} + + +const ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; + +/** + * Creates an out of office event in the user's Calendar. + */ +function blockOutCalendar() { + +/** + * Helper function to get a the current date and set the time for the start and end of the event. + * @param {number} hour The hour of the day for the new date. + * @param {number} minutes The minutes of the day for the new date. + * @return {Date} The new date. + */ + 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,00)}, + end: {dateTime: getDateAndHours(17,00)}, + eventType: 'outOfOffice', + summary: 'Out of office', + outOfOfficeProperties: { + autoDeclineMode: 'declineOnlyNewConflictingInvitations', + declineMessage: 'Declined because I am taking a day of.', + } } - - function createResponseCard(responseText) { - return { - "cardsV2": [ - { - "cardId": "1", - "card": { - "sections": [ - { - "widgets": [ - { - "decoratedText": { - "topLabel": "", - "text": responseText, - "startIcon": { - "knownIcon": "NONE", - "altText": "Task done", - "iconUrl": "https://fonts.gstatic.com/s/i/short-term/web/system/1x/task_alt_gm_grey_48dp.png" - }, - "wrapText": true - } + Calendar.Events.insert(event, 'primary'); +} + +/** + * Declines all meetings for the day. + */ + +function cancelMeetings() { + const events = CalendarApp.getEventsForDay(new Date()); + + events.forEach(function(event) { + if (event.getGuestList().length > 0) { + event.setMyStatus(CalendarApp.GuestStatus.NO); + } + }); +} + +/** + * Turns on the user's vacation response for today in Gmail. + */ +function turnOnAutoResponder() { + const currentTime = (new Date()).getTime(); + Gmail.Users.Settings.updateVacation({ + enableAutoReply: true, + responseSubject: 'I am out of the office today', + responseBodyHtml: 'I am out of the office today; will be back on the next business day.

Created by OOO Chat app!', + restrictToContacts: true, + restrictToDomain: true, + startTime: currentTime, + endTime: currentTime + ONE_DAY_MILLIS + }, 'me'); +} + +function createResponseCard(responseText) { + return { + "cardsV2": [ + { + "cardId": "1", + "card": { + "sections": [ + { + "widgets": [ + { + "decoratedText": { + "topLabel": "", + "text": responseText, + "startIcon": { + "knownIcon": "NONE", + "altText": "Task done", + "iconUrl": "https://fonts.gstatic.com/s/i/short-term/web/system/1x/task_alt_gm_grey_48dp.png" + }, + "wrapText": true } - ] - } - ], - "header": { - "title": "OOO app", - "subtitle": "Helping you manage your OOO", - "imageUrl": "https://goo.gle/3SfMkjb", - "imageType": "CIRCLE" + } + ] } + ], + "header": { + "title": "OOO app", + "subtitle": "Helping you manage your OOO", + "imageUrl": "https://goo.gle/3SfMkjb", + "imageType": "CIRCLE" } } - ] - } + } + ] } - - \ No newline at end of file +} + diff --git a/solutions/ooo-chat-app/appsscript.json b/solutions/ooo-chat-app/appsscript.json index ba4ac17c5..de9fa0b7b 100644 --- a/solutions/ooo-chat-app/appsscript.json +++ b/solutions/ooo-chat-app/appsscript.json @@ -1,15 +1,20 @@ { - "timeZone": "Europe/Madrid", - "exceptionLogging": "STACKDRIVER", - "runtimeVersion": "V8", - "dependencies": { - "enabledAdvancedServices": [ - { - "userSymbol": "Gmail", - "version": "v1", - "serviceId": "gmail" - } - ] - }, - "chat": {} - } \ No newline at end of file + "timeZone": "Europe/Madrid", + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8", + "dependencies": { + "enabledAdvancedServices": [ + { + "userSymbol": "Gmail", + "version": "v1", + "serviceId": "gmail" + }, + { + "userSymbol": "Calendar", + "version": "v3", + "serviceId": "calendar" + } + ] + }, + "chat": {} +} \ No newline at end of file