列出聊天室

本指南說明如何在 Google Chat API 的 Space 資源上使用 list 資源,以便列出聊天室。列出空格會傳回經過分頁處理、可篩選的聊天室清單。

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

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

列出具有應用程式驗證的聊天室時,會列出 Chat 應用程式可存取的聊天室。使用使用者驗證功能列出聊天室,會列出已驗證使用者可存取的聊天室。

必要條件

Python

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

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
  • 已為 Chat 應用程式設定授權。商家資訊空間支援以下兩種驗證方式:

Node.js

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

    npm install @google-cloud/local-auth @googleapis/chat
    
  • 已啟用並設定 Google Chat API 的 Google Cloud 專案。相關步驟請參閱「建構 Google Chat 應用程式」。
  • 已為 Chat 應用程式設定授權。商家資訊空間支援以下兩種驗證方式:

列出含使用者驗證的聊天室

如要列出 Google Chat 中的聊天室,請在您的要求中傳遞以下內容:

以下範例會列出已驗證使用者可見的已命名聊天室和群組通訊 (但不包括篩除的即時訊息):

Python

  1. 在工作目錄中,建立名為 chat_space_list.py 的檔案。
  2. chat_space_list.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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists named spaces and group chats (but not direct messages)
        visible to the authenticated user.
        '''
    
        # 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().list(
    
              # An optional filter that returns named spaces or unnamed group chats,
              # but not direct messages (DMs).
              filter='spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
    
          ).execute()
    
        # Prints the returned list of spaces.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在工作目錄中,建構並執行範例:

    python3 chat_space_list.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * List Chat spaces.
    * @return {!Promise<!Object>}
    */
    async function listSpaces() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.list({
        filter: 'spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
      });
    }
    
    listSpaces().then(console.log);
    
  3. 在工作目錄中,執行範例:

    node list-spaces.js
    

Chat API 會傳回已命名聊天室和群組通訊的分頁陣列

列出具有應用程式驗證的聊天室

如要列出 Google Chat 中的聊天室,請在您的要求中傳遞以下內容:

以下範例列出 Chat 應用程式可看到的已命名聊天室和群組通訊 (但不包括即時訊息):

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().list(
    
            # An optional filter that returns named spaces or unnamed
            # group chats, but not direct messages (DMs).
            filter='spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
    
        ).execute()
    
    print(result)
    
  3. 在工作目錄中,建構並執行範例:

    python3 chat_space_list_app.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    
    /**
    * List Chat spaces.
    * @return {!Promise<!Object>}
    */
    async function listSpaces() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.list({
        filter: 'spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
      });
    }
    
    listSpaces().then(console.log);
    
  3. 在工作目錄中,執行範例:

    node app-list-spaces.js
    

Chat API 會傳回分頁式的聊天室陣列。

自訂分頁或篩選清單

如要列出 Google Chat 中的聊天室,請傳送下列選用查詢參數,以便自訂分頁或篩選所列聊天室:

  • pageSize:要傳回的空格數量上限。服務傳回的值可能會少於這個值。如未指定,最多會傳回 100 個空格。最大值為 1,000;超過 1,000 的值會自動變更為 1,000。
  • pageToken:從先前的清單聊天室呼叫收到的網頁權杖。提供這個權杖以擷取後續網頁。進行分頁時,篩選器值應與提供網頁權杖的呼叫相符。傳送不同的值可能會導致非預期的結果。
  • filter:查詢篩選器。如需支援的查詢詳細資料,請參閱 spaces.list 方法相關說明。