更新聊天室

本指南說明如何針對以下項目的 Space 資源使用 patch 方法: 使用 Google Chat API 更新聊天室。更新聊天室來變更 例如使用者能看到的顯示名稱、說明和規範

Space 項資源 代表使用者和 Chat 擴充應用程式可以傳送訊息的地方 分享檔案及協同合作聊天室分為以下幾類:

  • 即時訊息 (DM) 是指兩位使用者或使用者之間的對話, Chat 應用程式。
  • 群組通訊是 3 位以上使用者之間的對話, Chat 擴充應用程式。
  • 已命名的聊天室是使用者傳送訊息、共用檔案、 這項原則著重於透過對開放式探究 嚴謹治學、誠信與合作的堅持來達到上述目標

必要條件

Python

  • Python 3.6 以上版本
  • pip 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 或以上版本
  • npm 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    npm install @google-cloud/local-auth @googleapis/chat
    

更新聊天室

如要更新 Google Chat 中的現有聊天室,請傳遞以下內容 :

  • 指定 chat.spaces 授權範圍。
  • patch 方法Space 資源而言。於 您在要求中指定空格 name 欄位,也就是 updateMask 欄位 (包含一或多個要更新的欄位),以及 使用最新的聊天室資訊body

您可以更新顯示名稱、聊天室類型、記錄狀態和 內容。如要查看可更新的所有欄位,請參閱 參考說明文件

以下說明如何更新現有聊天室的「spaceDetails」欄位:

Python

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

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.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.spaces"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates the specified space description and guidelines.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # 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 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

  4. 在工作目錄中建構並執行範例:

    python3 chat_space_update.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a Chat space with the description and guidelines.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.patch({
        name: 'spaces/SPACE',
        updateMask: 'spaceDetails',
        requestBody: {
          spaceDetails: {
            description: 'This description was updated with Chat API!',
            guidelines: 'These guidelines were updated with Chat API!'
          },
        }
      });
    }
    
    updateSpace().then(console.log);
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

  4. 在工作目錄中執行範例:

    node update-space.js
    

Google Chat API 會傳回 代表更新資訊的 Space 資源