更新聊天室

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

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

  • 即時訊息 (DM) 是兩個使用者或使用者和 Chat 應用程式之間的對話。
  • 群組通訊是三位以上的使用者和 Chat 應用程式之間的對話。
  • 已命名的聊天室是永久供使用者傳送訊息、分享檔案及協同合作的地方。

必要條件

Python

  • Python 3.6 或更高版本
  • pip 套件管理工具
  • 最新的 Python 專用 Google 用戶端程式庫。如要安裝或更新,請在指令列介面中執行下列指令:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
  • 已為 Chat 應用程式設定授權。更新聊天室時需要透過 chat.spaces 授權範圍進行使用者驗證

Node.js

  • Node.js 與 npm
  • 最新的 Node.js 專用 Google 用戶端程式庫。如要安裝,請在指令列介面中執行下列指令:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
  • 已為 Chat 應用程式設定授權。更新聊天室時需要透過 chat.spaces 授權範圍進行使用者驗證

更新聊天室

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

  • 指定 chat.spaces 授權範圍。
  • Space 資源呼叫 patch 方法,並傳遞要更新空間的 name,以及指定更新空間屬性的 updateMaskbody
  • updateMask 會指定要更新空間的切面,包括:
    • displayName:更新使用者可理解的 Google Chat UI 顯示聊天室名稱。只有一併支援將具有 SPACE 類型的聊天室顯示名稱,或是一併加入 spaceType 遮罩,將 GROUP_CHAT 空間類型變更為 SPACE 時。如果嘗試更新 GROUP_CHATDIRECT_MESSAGE 聊天室的顯示名稱,會導致引數無效。
    • spaceType:更新聊天室類型,但僅支援將 GROUP_CHAT 空間類型變更為 SPACE。在更新遮罩中加入 displayNamespaceType,並確認指定空間含有非空白的 displayNameSPACE 空間類型。如果現有空間已有 SPACE 類型,包括 spaceType 遮罩和 SPACE 類型,則您可以選擇是否要更新顯示名稱。嘗試透過其他方式更新聊天室類型會導致引數無效。
    • spaceDetails:聊天室的詳細資料,包括說明和規則。
    • spaceHistoryState:如果機構允許使用者變更自己的記錄設定,支援開啟或關閉聊天室記錄功能。與其他所有欄位路徑互斥。

以下說明如何更新現有聊天室的 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 created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在程式碼中,將 SPACE 替換為聊天室名稱,您可以在 Chat API 的 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 替換為聊天室名稱,您可以在 Chat API 的 spaces.list 方法或聊天室網址取得這項資訊。

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

    node update-space.js
    

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