|
| 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 | +const APP_NAME = 'Connectivity app'; |
| 18 | +const LOGOUT_COMMAND_ID = 1; |
| 19 | + |
| 20 | +// Set to the client_secrets.json file content |
| 21 | +const CLIENT_SECRETS = {}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Responds to a MESSAGE event in Google Chat. |
| 25 | + * |
| 26 | + * @param {Object} event the event object from Google Workspace Add On |
| 27 | + */ |
| 28 | +function onMessage(event) { |
| 29 | + try { |
| 30 | + // Try to obtain an existing OAuth2 token from storage. |
| 31 | + var meetService = getMeetService_(); |
| 32 | + if (!meetService.hasAccess()) { |
| 33 | + // App doesn't have credentials for the user yet. |
| 34 | + // Request configuration to obtain OAuth2 credentials. |
| 35 | + getConfigRequestResponse(meetService).throwException(); |
| 36 | + } |
| 37 | + |
| 38 | + // Call Meet API to create the new space with the user's OAuth2 credentials. |
| 39 | + var response = UrlFetchApp.fetch('https://meet.googleapis.com/v2/spaces', { |
| 40 | + method: 'post', |
| 41 | + contentType: 'application/json', |
| 42 | + headers: { Authorization: 'Bearer ' + meetService.getAccessToken() }, |
| 43 | + // An empty body is sufficient to create a default space. |
| 44 | + payload: JSON.stringify({}) |
| 45 | + }); |
| 46 | + var meetSpace = JSON.parse(response.getContentText()); |
| 47 | + return sendCreateTextMessageAction(`New Meet was created: ${meetSpace.meetingUri}`); |
| 48 | + } catch (e) { |
| 49 | + // This error probably happened because the user revoked the |
| 50 | + // authorization. So, let's request configuration again. |
| 51 | + Logger.log('Error creating Meet space: ' + JSON.stringify(e)); |
| 52 | + throw e; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Responds to a APP_COMMAND event in Google Chat. |
| 58 | + * |
| 59 | + * @param {Object} event the event object from Google Workspace Add On |
| 60 | + */ |
| 61 | +function onAppCommand(event) { |
| 62 | + // Extract data from the event. |
| 63 | + const chatEvent = event.chat; |
| 64 | + const appCommandId = chatEvent.appCommandPayload.appCommandMetadata.appCommandId; |
| 65 | + |
| 66 | + if (appCommandId == LOGOUT_COMMAND_ID) { |
| 67 | + // Delete OAuth2 credentials from storage if any. |
| 68 | + resetMeetServiceAuth(); |
| 69 | + // Reply a Chat message with confirmation |
| 70 | + return sendCreateTextMessageAction('You are now logged out!'); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// ---------------------- |
| 75 | +// Chat utils |
| 76 | +// ---------------------- |
| 77 | + |
| 78 | +/** |
| 79 | + * Returns an action response that tells Chat to reply with a text message. |
| 80 | + * |
| 81 | + * @param {!string} text The text of the message to reply with. |
| 82 | + * @returns {Object} An ActionResponse message. |
| 83 | + */ |
| 84 | +function sendCreateTextMessageAction(text) { |
| 85 | + return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: { text: text } }}}}; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns an action response that tells Chat to request configuration for the |
| 90 | + * app. The configuration will be tied to the user who sent the event. |
| 91 | + * |
| 92 | + * @param {!Service_} meetService The Meet API OAuth2 service |
| 93 | + * @param {!string} configCompleteRedirectUrl The URL to redirect to after |
| 94 | + * completing the flow. |
| 95 | + * @return {Object} An ActionResponse message request additional configuration. |
| 96 | + */ |
| 97 | +function getConfigRequestResponse(meetService) { |
| 98 | + return CardService.newAuthorizationException() |
| 99 | + .setAuthorizationUrl(meetService.getAuthorizationUrl()) |
| 100 | + .setResourceDisplayName(APP_NAME); |
| 101 | +} |
| 102 | + |
| 103 | +// ---------------------- |
| 104 | +// OAuth2 utils |
| 105 | +// ---------------------- |
| 106 | + |
| 107 | +/** |
| 108 | + * Create a new OAuth2 service to facilitate accessing the Meet API. |
| 109 | + * |
| 110 | + * @return A configured OAuth2 service object. |
| 111 | + */ |
| 112 | +function getMeetService_() { |
| 113 | + return OAuth2.createService('Google Meet API') |
| 114 | + .setAuthorizationBaseUrl(CLIENT_SECRETS.web.auth_uri) |
| 115 | + .setTokenUrl(CLIENT_SECRETS.web.token_uri) |
| 116 | + .setClientId(CLIENT_SECRETS.web.client_id) |
| 117 | + .setClientSecret(CLIENT_SECRETS.web.client_secret) |
| 118 | + .setScope('https://www.googleapis.com/auth/meetings.space.created') |
| 119 | + .setCallbackFunction('meetAuthCallback') |
| 120 | + .setCache(CacheService.getUserCache()) |
| 121 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 122 | + .setParam('access_type', 'offline') |
| 123 | + .setParam('prompt', 'consent'); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Function that handles callback requests from the OAuth2 authorization flow |
| 128 | + * for a Meet API OAuth2 service. |
| 129 | + * |
| 130 | + * @param {Object} request The request data received from the callback function. |
| 131 | + * @return {HtmlOutput} a success or denied HTML message to display to the user. |
| 132 | + */ |
| 133 | +function meetAuthCallback(request) { |
| 134 | + var meetService = getMeetService_(); |
| 135 | + var authorized = meetService.handleCallback(request); |
| 136 | + if (authorized) { |
| 137 | + return HtmlService.createHtmlOutput('Success! You can close this tab.'); |
| 138 | + } else { |
| 139 | + return HtmlService.createHtmlOutput('Denied. You can close this tab.'); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | +* Reset user's credentials for a Meet API OAuth2 service. |
| 145 | +*/ |
| 146 | +function resetMeetServiceAuth() { |
| 147 | + getMeetService_().reset(); |
| 148 | +} |
0 commit comments