更新聊天室

本指南說明如何使用 Google Chat API Space 資源的 patch() 方法更新聊天室。更新聊天室可變更聊天室的屬性,例如使用者可見的顯示名稱、說明和規範。

如果您是 Google Workspace 管理員,可以呼叫 patch() 方法,更新 Google Workspace 機構中的現有聊天室。

Space資源代表使用者和 Chat 應用程式可傳送訊息、共用檔案及協作的空間。聊天室分為以下幾種類型:

  • 即時訊息 (DM) 是指兩位使用者之間,或使用者與 Chat 應用程式之間的對話。
  • 群組對話是指三位以上使用者和即時通訊應用程式之間的對話。
  • 具名聊天室是持續存在的空間,可供使用者傳送訊息、分享檔案及協作。

必要條件

Node.js

  • Google Chat 聊天室。如要使用 Google Chat API 建立聊天室,請參閱「建立聊天室」。如要在 Chat 中建立群組,請參閱說明中心文件

Python

  • Google Chat 聊天室。如要使用 Google Chat API 建立聊天室,請參閱「建立聊天室」。如要在 Chat 中建立群組,請參閱說明中心文件

Java

  • Google Chat 聊天室。如要使用 Google Chat API 建立聊天室,請參閱「建立聊天室」。如要在 Chat 中建立群組,請參閱說明中心文件

Apps Script

  • Google Chat 聊天室。如要使用 Google Chat API 建立聊天室,請參閱「建立聊天室」。如要在 Chat 中建立群組,請參閱說明中心文件

以使用者身分更新空間

如要使用使用者驗證更新 Google Chat 中的現有聊天室,請在要求中傳遞下列項目:

  • 指定 chat.spaces 授權範圍。
  • 呼叫 UpdateSpace() 方法。在要求中,您會指定空間 name 欄位、updateMask 欄位 (內含一或多個要更新的欄位),以及 body 欄位 (內含更新後的空間資訊)。

你可以更新顯示名稱、空間類型、記錄狀態等資訊。如要查看可更新的所有欄位,請參閱參考說明文件

如要更新現有聊天室的 displayName 欄位,請按照下列步驟操作:

Node.js

chat/client-libraries/cloud/update-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';  const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.spaces'];  // This sample shows how to update a space with user credential async function main() {   // Create a client   const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);    // Initialize request argument(s)   const request = {     space: {       // Replace SPACE_NAME here       name: 'spaces/SPACE_NAME',       displayName: 'New space display name'     },     // The field paths to update. Separate multiple values with commas or use     // `*` to update all field paths.     updateMask: {       // The field paths to update.       paths: ['display_name']     }   };    // Make the request   const response = await chatClient.updateSpace(request);    // Handle the response   console.log(response); }  main().catch(console.error);

Python

chat/client-libraries/cloud/update_space_user_cred.py
from authentication_utils import create_client_with_user_credentials from google.apps import chat_v1 as google_chat  SCOPES = ["https://www.googleapis.com/auth/chat.spaces"]  # This sample shows how to update a space with user credential def update_space_with_user_cred():     # Create a client     client = create_client_with_user_credentials(SCOPES)      # Initialize request argument(s)     request = google_chat.UpdateSpaceRequest(         space = {             # Replace SPACE_NAME here             'name': 'spaces/SPACE_NAME',             'display_name': 'New space display name'         },         # The field paths to update. Separate multiple values with commas.         update_mask = 'displayName'     )      # Make the request     response = client.update_space(request)      # Handle the response     print(response)  update_space_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/UpdateSpaceUserCred.java
import com.google.chat.v1.ChatServiceClient; import com.google.chat.v1.UpdateSpaceRequest; import com.google.chat.v1.Space; import com.google.protobuf.FieldMask;  // This sample shows how to update space with user credential. public class UpdateSpaceUserCred {    private static final String SCOPE =     "https://www.googleapis.com/auth/chat.spaces";    public static void main(String[] args) throws Exception {     try (ChatServiceClient chatServiceClient =         AuthenticationUtils.createClientWithUserCredentials(           ImmutableList.of(SCOPE))) {       UpdateSpaceRequest.Builder request = UpdateSpaceRequest.newBuilder()         .setSpace(Space.newBuilder()           // Replace SPACE_NAME here.           .setName("spaces/SPACE_NAME")           .setDisplayName("New space display name"))         .setUpdateMask(FieldMask.newBuilder()           // The field paths to update.           .addPaths("display_name"));       Space response = chatServiceClient.updateSpace(request.build());        System.out.println(JsonFormat.printer().print(response));     }   } }

Apps Script

chat/advanced-service/Main.gs
/**  * This sample shows how to update a space with user credential  *   * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces'  * referenced in the manifest file (appsscript.json).  */ function updateSpaceUserCred() {   // Initialize request argument(s)   // TODO(developer): Replace SPACE_NAME here   const name = 'spaces/SPACE_NAME';   const space = {     displayName: 'New space display name'   };   // The field paths to update. Separate multiple values with commas or use   // `*` to update all field paths.   const updateMask = 'displayName';    // Make the request   const response = Chat.Spaces.patch(space, name, {     updateMask: updateMask   });    // Handle the response   console.log(response); }

如要執行這個範例,請將 SPACE_NAME 替換為空間的 name 欄位中的 ID。您可以呼叫 ListSpaces() 方法,或從空間的網址取得 ID。

Google Chat API 會傳回 Space 的執行個體,反映更新內容。

以 Google Workspace 管理員身分更新聊天室

如果您是 Google Workspace 管理員,可以呼叫 UpdateSpace() 方法,更新 Google Workspace 機構中的任何空間。

如要以 Google Workspace 管理員身分呼叫這個方法,請按照下列步驟操作:

  • 使用使用者驗證呼叫方法,並指定支援使用管理員權限呼叫方法的授權範圍
  • 在要求中,將查詢參數 useAdminAccess 指定為 true

如需更多資訊和範例,請參閱「以 Google Workspace 管理員身分管理 Google Chat 聊天室」。

以 Chat 應用程式更新聊天室

應用程式驗證需要管理員核准一次。

如要使用應用程式驗證更新 Google Chat 中的現有聊天室,請在要求中傳遞下列項目:

  • 指定 chat.app.spaces 授權範圍。 使用應用程式驗證時,您只能更新由 Chat 應用程式建立的聊天室。
  • 呼叫 Space 資源的 patch 方法。在要求中,您會指定空間 name 欄位、updateMask 欄位 (含一或多個要更新的欄位),以及 body (含更新的空間資訊)。

您可以更新顯示名稱、聊天室類型、記錄狀態、權限設定等項目。如要查看可更新的所有欄位,請參閱參考說明文件

編寫呼叫 Chat API 的指令碼

如要更新現有聊天室的 spaceDetails 欄位,請按照下列步驟操作:

Python

  1. 在工作目錄中,建立名為 chat_space_update_app.py 的檔案。
  2. chat_space_update_app.py 中加入下列程式碼:

    from google.oauth2 import service_account from apiclient.discovery import build  # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.app.spaces"]  def main():     '''     Authenticates with Chat API using app authentication,     then updates the specified space description and guidelines.     '''      # Specify service account details.     creds = (         service_account.Credentials.from_service_account_file('credentials.json')         .with_scopes(SCOPES)     )      # Build a service endpoint for Chat API.     chat = build('chat', 'v1', credentials=creds)      # Use the service endpoint to call Chat API.     result = chat.spaces().patch(        # The space to update, and the updated space details.       #       # Replace {space} with a space name.       # Obtain the space name from the spaces resource of Chat API,       # or from a space's URL.       name='spaces/SPACE',       updateMask='spaceDetails',       body={          'spaceDetails': {           'description': 'This description was updated with Chat API!',           'guidelines': 'These guidelines were updated with Chat API!'         }        }      ).execute()      # Prints details about the updated space.     print(result)  if __name__ == '__main__':     main() 
  3. 在程式碼中,請替換下列項目:

    • SPACE,您可透過 Chat API 的 spaces.list 方法或聊天室網址取得聊天室名稱。
  4. 在工作目錄中,建構並執行範例:

    python3 chat_space_update_app.py

Google Chat API 會傳回 Space 資源的執行個體,反映更新內容。

限制和注意事項