From be16446e803ad8c7feef8e629eecd7e917c627fe Mon Sep 17 00:00:00 2001 From: Pierrick Voulet <6769971+PierrickVoulet@users.noreply.github.com> Date: Tue, 30 Jul 2024 11:49:17 -0400 Subject: [PATCH] feat: Add Google Chat API quickstart (#475) Co-authored-by: pierrick --- chat/quickstart/Code.gs | 46 +++++++++++++++++++++++++++++++++ chat/quickstart/README.md | 11 ++++++++ chat/quickstart/appsscript.json | 16 ++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 chat/quickstart/Code.gs create mode 100644 chat/quickstart/README.md create mode 100644 chat/quickstart/appsscript.json diff --git a/chat/quickstart/Code.gs b/chat/quickstart/Code.gs new file mode 100644 index 000000000..5610da27d --- /dev/null +++ b/chat/quickstart/Code.gs @@ -0,0 +1,46 @@ +/** + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START chat_quickstart] +/** + * This quickstart sample shows how to list spaces with user credential + * + * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly' + * referenced in the manifest file (appsscript.json). + */ +function listSpaces() { + // Initialize request argument(s) + // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE) + const filter = 'space_type = "SPACE"'; + + // Iterate through the response pages using page tokens + let responsePage; + let pageToken = null; + do { + // Request response pages + responsePage = Chat.Spaces.list({ + filter: filter, + pageToken: pageToken + }); + // Handle response pages + if (responsePage.spaces) { + responsePage.spaces.forEach((space) => console.log(space)); + } + // Update the page token to the next one + pageToken = responsePage.nextPageToken; + } while (pageToken); +} +// [END chat_quickstart] diff --git a/chat/quickstart/README.md b/chat/quickstart/README.md new file mode 100644 index 000000000..3bbb4f9c3 --- /dev/null +++ b/chat/quickstart/README.md @@ -0,0 +1,11 @@ +# Google Chat Apps Script Quickstart + +Complete the steps described in the [quickstart instructions]( +https://developers.google.com/workspace/chat/api/guides/quickstart/apps-script), +and in about five minutes you'll have a simple Apps Script application +that makes requests to the Google Chat API. + +## Run + +After following the quickstart setup instructions, execute the function `listSpaces` +from the Apps Script console. diff --git a/chat/quickstart/appsscript.json b/chat/quickstart/appsscript.json new file mode 100644 index 000000000..73c0edcf7 --- /dev/null +++ b/chat/quickstart/appsscript.json @@ -0,0 +1,16 @@ +{ + "timeZone": "America/New_York", + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8", + "oauthScopes": [ + "https://www.googleapis.com/auth/chat.spaces.readonly" + ], + "chat": {}, + "dependencies": { + "enabledAdvancedServices": [{ + "userSymbol": "Chat", + "version": "v1", + "serviceId": "chat" + }] + } +}