|
| 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 | + * http://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 | +// [START chat_incident_response_app] |
| 17 | + |
| 18 | +/** |
| 19 | + * Responds to a MESSAGE event in Google Chat. |
| 20 | + * |
| 21 | + * This app only responds to a slash command with the ID 1 ("/closeIncident"). |
| 22 | + * It will respond to any other message with a simple "Hello" text message. |
| 23 | + * |
| 24 | + * @param {Object} event the event object from Google Chat |
| 25 | + */ |
| 26 | +function onMessage(event) { |
| 27 | + if (event.message.slashCommand) { |
| 28 | + return processSlashCommand_(event); |
| 29 | + } |
| 30 | + return { "text": "Hello from Incident Response app!" }; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Responds to a CARD_CLICKED event in Google Chat. |
| 35 | + * |
| 36 | + * This app only responds to one kind of dialog (Close Incident). |
| 37 | + * |
| 38 | + * @param {Object} event the event object from Google Chat |
| 39 | + */ |
| 40 | +function onCardClick(event) { |
| 41 | + if (event.isDialogEvent) { |
| 42 | + if (event.dialogEventType == 'SUBMIT_DIALOG') { |
| 43 | + return processSubmitDialog_(event); |
| 44 | + } |
| 45 | + return { |
| 46 | + actionResponse: { |
| 47 | + type: "DIALOG", |
| 48 | + dialogAction: { |
| 49 | + actionStatus: "OK" |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Responds to a MESSAGE event with a Slash command in Google Chat. |
| 58 | + * |
| 59 | + * This app only responds to a slash command with the ID 1 ("/closeIncident") |
| 60 | + * by returning a Dialog. |
| 61 | + * |
| 62 | + * @param {Object} event the event object from Google Chat |
| 63 | + */ |
| 64 | +function processSlashCommand_(event) { |
| 65 | + if (event.message.slashCommand.commandId != CLOSE_INCIDENT_COMMAND_ID) { |
| 66 | + return { |
| 67 | + "text": "Command not recognized. Use the command `/closeIncident` to close the incident managed by this space." |
| 68 | + }; |
| 69 | + } |
| 70 | + const sections = [ |
| 71 | + { |
| 72 | + header: "Close Incident", |
| 73 | + widgets: [ |
| 74 | + { |
| 75 | + textInput: { |
| 76 | + label: "Please describe the incident resolution", |
| 77 | + type: "MULTIPLE_LINE", |
| 78 | + name: "description" |
| 79 | + } |
| 80 | + }, |
| 81 | + { |
| 82 | + buttonList: { |
| 83 | + buttons: [ |
| 84 | + { |
| 85 | + text: "Close Incident", |
| 86 | + onClick: { |
| 87 | + action: { |
| 88 | + function: "closeIncident" |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + ] |
| 93 | + } |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | + ]; |
| 98 | + return { |
| 99 | + actionResponse: { |
| 100 | + type: "DIALOG", |
| 101 | + dialogAction: { |
| 102 | + dialog: { |
| 103 | + body: { |
| 104 | + sections, |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Responds to a CARD_CLICKED event with a Dialog submission in Google Chat. |
| 114 | + * |
| 115 | + * This app only responds to one kind of dialog (Close Incident). |
| 116 | + * It creates a Doc with a summary of the incident information and posts a message |
| 117 | + * to the space with a link to the Doc. |
| 118 | + * |
| 119 | + * @param {Object} event the event object from Google Chat |
| 120 | + */ |
| 121 | +function processSubmitDialog_(event) { |
| 122 | + const resolution = event.common.formInputs.description[""].stringInputs.value[0]; |
| 123 | + const chatHistory = concatenateAllSpaceMessages_(event.space.name); |
| 124 | + const chatSummary = summarizeChatHistory_(chatHistory); |
| 125 | + const docUrl = createDoc_(event.space.displayName, resolution, chatHistory, chatSummary); |
| 126 | + return { |
| 127 | + actionResponse: { |
| 128 | + type: "NEW_MESSAGE", |
| 129 | + }, |
| 130 | + text: `Incident closed with the following resolution: ${resolution}\n\nHere is the automatically generated post-mortem:\n${docUrl}` |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Lists all the messages in the Chat space, then concatenate all of them into |
| 136 | + * a single text containing the full Chat history. |
| 137 | + * |
| 138 | + * For simplicity for this demo, it only fetches the first 100 messages. |
| 139 | + * |
| 140 | + * Messages with slash commands are filtered out, so the returned history will |
| 141 | + * contain only the conversations between users and not app command invocations. |
| 142 | + * |
| 143 | + * @return {string} a text containing all the messages in the space in the format: |
| 144 | + * Sender's name: Message |
| 145 | + */ |
| 146 | +function concatenateAllSpaceMessages_(spaceName) { |
| 147 | + // Call Chat API method spaces.messages.list |
| 148 | + const response = Chat.Spaces.Messages.list(spaceName, { 'pageSize': 100 }); |
| 149 | + const messages = response.messages; |
| 150 | + // Fetch the display names of the message senders and returns a text |
| 151 | + // concatenating all the messages. |
| 152 | + let userMap = new Map(); |
| 153 | + return messages |
| 154 | + .filter(message => message.slashCommand === undefined) |
| 155 | + .map(message => `${getUserDisplayName_(userMap, message.sender.name)}: ${message.text}`) |
| 156 | + .join('\n'); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Obtains the display name of a user by using the Admin Directory API. |
| 161 | + * |
| 162 | + * The fetched display name is cached in the provided map, so we only call the API |
| 163 | + * once per user. |
| 164 | + * |
| 165 | + * If the user does not have a display name, then the full name is used. |
| 166 | + * |
| 167 | + * @param {Map} userMap a map containing the display names previously fetched |
| 168 | + * @param {string} userName the resource name of the user |
| 169 | + * @return {string} the user's display name |
| 170 | + */ |
| 171 | +function getUserDisplayName_(userMap, userName) { |
| 172 | + if (userMap.has(userName)) { |
| 173 | + return userMap.get(userName); |
| 174 | + } |
| 175 | + let displayName = 'Unknown User'; |
| 176 | + try { |
| 177 | + const user = AdminDirectory.Users.get( |
| 178 | + userName.replace("users/", ""), |
| 179 | + { projection: 'BASIC', viewType: 'domain_public' }); |
| 180 | + displayName = user.name.displayName ? user.name.displayName : user.name.fullName; |
| 181 | + } catch (e) { |
| 182 | + // Ignore error if the API call fails (for example, because it's an |
| 183 | + // out-of-domain user or Chat app)) and just use 'Unknown User'. |
| 184 | + } |
| 185 | + userMap.set(userName, displayName); |
| 186 | + return displayName; |
| 187 | +} |
| 188 | +// [END chat_incident_response_app] |
0 commit comments