|
| 1 | +/** |
| 2 | + * Responds to an ADDED_TO_SPACE event in Google Chat. |
| 3 | + * @param {object} event the event object from Google Chat |
| 4 | + * @return {object} JSON-formatted response |
| 5 | + * @see https://developers.google.com/workspace/chat/receive-respond-interactions |
| 6 | + */ |
| 7 | +function onAddToSpace(event) { |
| 8 | + console.info(event); |
| 9 | + var message = 'Thank you for adding me to '; |
| 10 | + if (event.space.type === 'DM') { |
| 11 | + message += 'a DM, ' + event.user.displayName + '!'; |
| 12 | + } else { |
| 13 | + message += event.space.displayName; |
| 14 | + } |
| 15 | + return { text: message }; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Responds to a REMOVED_FROM_SPACE event in Google Chat. |
| 20 | + * @param {object} event the event object from Google Chat |
| 21 | + * @see https://developers.google.com/workspace/chat/receive-respond-interactions |
| 22 | + */ |
| 23 | +function onRemoveFromSpace(event) { |
| 24 | + console.info(event); |
| 25 | + console.log('Chat app removed from ', event.space.name); |
| 26 | +} |
| 27 | + |
| 28 | +var DEFAULT_IMAGE_URL = 'https://goo.gl/bMqzYS'; |
| 29 | +var HEADER = { |
| 30 | + header: { |
| 31 | + title : 'Attendance Chat app', |
| 32 | + subtitle : 'Log your vacation time', |
| 33 | + imageUrl : DEFAULT_IMAGE_URL |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Creates a card-formatted response. |
| 39 | + * @param {object} widgets the UI components to send |
| 40 | + * @return {object} JSON-formatted response |
| 41 | + */ |
| 42 | +function createCardResponse(widgets) { |
| 43 | + return { |
| 44 | + cards: [HEADER, { |
| 45 | + sections: [{ |
| 46 | + widgets: widgets |
| 47 | + }] |
| 48 | + }] |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +var REASON = { |
| 53 | + SICK: 'Out sick', |
| 54 | + OTHER: 'Out of office' |
| 55 | +}; |
| 56 | +/** |
| 57 | + * Responds to a MESSAGE event triggered in Google Chat. |
| 58 | + * @param {object} event the event object from Google Chat |
| 59 | + * @return {object} JSON-formatted response |
| 60 | + */ |
| 61 | +function onMessage(event) { |
| 62 | + console.info(event); |
| 63 | + var reason = REASON.OTHER; |
| 64 | + var name = event.user.displayName; |
| 65 | + var userMessage = event.message.text; |
| 66 | + |
| 67 | + // If the user said that they were 'sick', adjust the image in the |
| 68 | + // header sent in response. |
| 69 | + if (userMessage.indexOf('sick') > -1) { |
| 70 | + // Hospital material icon |
| 71 | + HEADER.header.imageUrl = 'https://goo.gl/mnZ37b'; |
| 72 | + reason = REASON.SICK; |
| 73 | + } else if (userMessage.indexOf('vacation') > -1) { |
| 74 | + // Spa material icon |
| 75 | + HEADER.header.imageUrl = 'https://goo.gl/EbgHuc'; |
| 76 | + } |
| 77 | + |
| 78 | + var widgets = [{ |
| 79 | + textParagraph: { |
| 80 | + text: 'Hello, ' + name + '.<br/>Are you taking time off today?' |
| 81 | + } |
| 82 | + }, { |
| 83 | + buttons: [{ |
| 84 | + textButton: { |
| 85 | + text: 'Set vacation in Gmail', |
| 86 | + onClick: { |
| 87 | + action: { |
| 88 | + actionMethodName: 'turnOnAutoResponder', |
| 89 | + parameters: [{ |
| 90 | + key: 'reason', |
| 91 | + value: reason |
| 92 | + }] |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + }, { |
| 97 | + textButton: { |
| 98 | + text: 'Block out day in Calendar', |
| 99 | + onClick: { |
| 100 | + action: { |
| 101 | + actionMethodName: 'blockOutCalendar', |
| 102 | + parameters: [{ |
| 103 | + key: 'reason', |
| 104 | + value: reason |
| 105 | + }] |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }] |
| 110 | + }]; |
| 111 | + return createCardResponse(widgets); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Responds to a CARD_CLICKED event triggered in Google Chat. |
| 116 | + * @param {object} event the event object from Google Chat |
| 117 | + * @return {object} JSON-formatted response |
| 118 | + * @see https://developers.google.com/workspace/chat/receive-respond-interactions |
| 119 | + */ |
| 120 | +function onCardClick(event) { |
| 121 | + console.info(event); |
| 122 | + var message = ''; |
| 123 | + var reason = event.action.parameters[0].value; |
| 124 | + if (event.action.actionMethodName == 'turnOnAutoResponder') { |
| 125 | + turnOnAutoResponder(reason); |
| 126 | + message = 'Turned on vacation settings.'; |
| 127 | + } else if (event.action.actionMethodName == 'blockOutCalendar') { |
| 128 | + blockOutCalendar(reason); |
| 129 | + message = 'Blocked out your calendar for the day.'; |
| 130 | + } else { |
| 131 | + message = "I'm sorry; I'm not sure which button you clicked."; |
| 132 | + } |
| 133 | + return { text: message }; |
| 134 | +} |
| 135 | + |
| 136 | +var ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; |
| 137 | +/** |
| 138 | + * Turns on the user's vacation response for today in Gmail. |
| 139 | + * @param {string} reason the reason for vacation, either REASON.SICK or REASON.OTHER |
| 140 | + */ |
| 141 | +function turnOnAutoResponder(reason) { |
| 142 | + var currentTime = (new Date()).getTime(); |
| 143 | + Gmail.Users.Settings.updateVacation({ |
| 144 | + enableAutoReply: true, |
| 145 | + responseSubject: reason, |
| 146 | + responseBodyHtml: "I'm out of the office today; will be back on the next business day.<br><br><i>Created by Attendance Chat app!</i>", |
| 147 | + restrictToContacts: true, |
| 148 | + restrictToDomain: true, |
| 149 | + startTime: currentTime, |
| 150 | + endTime: currentTime + ONE_DAY_MILLIS |
| 151 | + }, 'me'); |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Places an all-day meeting on the user's Calendar. |
| 156 | + * @param {string} reason the reason for vacation, either REASON.SICK or REASON.OTHER |
| 157 | + */ |
| 158 | +function blockOutCalendar(reason) { |
| 159 | + CalendarApp.createAllDayEvent(reason, new Date(), new Date(Date.now() + ONE_DAY_MILLIS)); |
| 160 | +} |
0 commit comments