|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Handle messages that have links whose URLs match patterns configured |
| 19 | + * for link previewing. |
| 20 | + * |
| 21 | + * - Reply with text messages that echo "text.example.com" link URLs in messages. |
| 22 | + * - Attach cards to messages with "support.example.com" link URLs. |
| 23 | + * |
| 24 | + * @param {Object} event The event object from Google Workspace add-on. |
| 25 | + * @return {Object} The action response. |
| 26 | + */ |
| 27 | +function onMessage(event) { |
| 28 | + // Stores the Google Chat event as a variable. |
| 29 | + const chatMessage = event.chat.messagePayload.message; |
| 30 | + |
| 31 | + // If the Chat app doesn't detect a link preview URL pattern, reply |
| 32 | + // with a text message that says so. |
| 33 | + if (!chatMessage.matchedUrl) { |
| 34 | + return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: { |
| 35 | + text: 'No matchedUrl detected.' |
| 36 | + }}}}}; |
| 37 | + } |
| 38 | + |
| 39 | + // [START preview_links_text] |
| 40 | + // Reply with a text message for URLs of the subdomain "text". |
| 41 | + if (chatMessage.matchedUrl.url.includes("text.example.com")) { |
| 42 | + return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: { |
| 43 | + text: 'event.chat.messagePayload.message.matchedUrl.url: ' + chatMessage.matchedUrl.url |
| 44 | + }}}}}; |
| 45 | + } |
| 46 | + // [END preview_links_text] |
| 47 | + |
| 48 | + // [START preview_links_card] |
| 49 | + // Attach a card to the message for URLs of the subdomain "support". |
| 50 | + if (chatMessage.matchedUrl.url.includes("support.example.com")) { |
| 51 | + // A hard-coded card is used in this example. In a real-life scenario, |
| 52 | + // the case information would be fetched and used to build the card. |
| 53 | + return { hostAppDataAction: { chatDataAction: { updateInlinePreviewAction: { cardsV2: [{ |
| 54 | + cardId: 'attachCard', |
| 55 | + card: { |
| 56 | + header: { |
| 57 | + title: 'Example Customer Service Case', |
| 58 | + subtitle: 'Case summary', |
| 59 | + }, |
| 60 | + sections: [{ widgets: [ |
| 61 | + { decoratedText: { topLabel: 'Case ID', text: 'case123'}}, |
| 62 | + { decoratedText: { topLabel: 'Assignee', text: 'Charlie'}}, |
| 63 | + { decoratedText: { topLabel: 'Status', text: 'Open'}}, |
| 64 | + { decoratedText: { topLabel: 'Subject', text: 'It won\'t turn on...' }}, |
| 65 | + { buttonList: { buttons: [{ |
| 66 | + text: 'OPEN CASE', |
| 67 | + onClick: { openLink: { |
| 68 | + url: 'https://support.example.com/orders/case123' |
| 69 | + }}, |
| 70 | + }, { |
| 71 | + text: 'RESOLVE CASE', |
| 72 | + onClick: { openLink: { |
| 73 | + url: 'https://support.example.com/orders/case123?resolved=y', |
| 74 | + }}, |
| 75 | + }, { |
| 76 | + text: 'ASSIGN TO ME', |
| 77 | + // Clicking this button triggers the execution of the function |
| 78 | + // "assign" from the Apps Script project. |
| 79 | + onClick: { action: { function: 'assign'}} |
| 80 | + }]}} |
| 81 | + ]}] |
| 82 | + } |
| 83 | + }]}}}}; |
| 84 | + } |
| 85 | + // [END preview_links_card] |
| 86 | +} |
| 87 | + |
| 88 | +// [START preview_links_assign] |
| 89 | +/** |
| 90 | + * Assigns and updates the card that's attached to a message with a |
| 91 | + * previewed link of the pattern "support.example.com". |
| 92 | + * |
| 93 | + * @param {Object} event The event object from the Google Workspace add-on. |
| 94 | + * @return {Object} Action response depending on the message author. |
| 95 | + */ |
| 96 | +function assign(event) { |
| 97 | + // Creates the updated card that displays "You" for the assignee |
| 98 | + // and that disables the button. |
| 99 | + // |
| 100 | + // A hard-coded card is used in this example. In a real-life scenario, |
| 101 | + // an actual assign action would be performed before building the card. |
| 102 | + const message = { cardsV2: [{ |
| 103 | + cardId: 'attachCard', |
| 104 | + card: { |
| 105 | + header: { |
| 106 | + title: 'Example Customer Service Case', |
| 107 | + subtitle: 'Case summary', |
| 108 | + }, |
| 109 | + sections: [{ widgets: [ |
| 110 | + { decoratedText: { topLabel: 'Case ID', text: 'case123'}}, |
| 111 | + // The assignee is now "You" |
| 112 | + { decoratedText: { topLabel: 'Assignee', text: 'You'}}, |
| 113 | + { decoratedText: { topLabel: 'Status', text: 'Open'}}, |
| 114 | + { decoratedText: { topLabel: 'Subject', text: 'It won\'t turn on...' }}, |
| 115 | + { buttonList: { buttons: [{ |
| 116 | + text: 'OPEN CASE', |
| 117 | + onClick: { openLink: { |
| 118 | + url: 'https://support.example.com/orders/case123' |
| 119 | + }}, |
| 120 | + }, { |
| 121 | + text: 'RESOLVE CASE', |
| 122 | + onClick: { openLink: { |
| 123 | + url: 'https://support.example.com/orders/case123?resolved=y', |
| 124 | + }}, |
| 125 | + }, { |
| 126 | + text: 'ASSIGN TO ME', |
| 127 | + // The button is now disabled |
| 128 | + disabled: true, |
| 129 | + onClick: { action: { function: 'assign'}} |
| 130 | + }]}} |
| 131 | + ]}] |
| 132 | + } |
| 133 | + }]}; |
| 134 | + |
| 135 | + // Use the adequate action response type. It depends on whether the message |
| 136 | + // the preview link card is attached to was created by a human or a Chat app. |
| 137 | + if(event.chat.buttonClickedPayload.message.sender.type === 'HUMAN') { |
| 138 | + return { hostAppDataAction: { chatDataAction: { updateInlinePreviewAction: message }}}; |
| 139 | + } else { |
| 140 | + return { hostAppDataAction: { chatDataAction: { updateMessageAction: message }}}; |
| 141 | + } |
| 142 | +} |
| 143 | +// [END preview_links_assign] |
0 commit comments