列出聊天室

本指南介绍如何使用 Space 资源中的 list 资源 使用 Google Chat API 列出聊天室。列出聊天室会返回可过滤的分页聊天室 聊天室列表。

通过 Space 资源 代表用户和 Chat 扩展应用能够发送消息的位置, 共享文件和协作聊天室分为以下几种类型:

  • 私信 (DM) 是指两位用户或一位用户与 Chat 应用。
  • 群聊是三位或更多用户之间的对话, 聊天应用。
  • 已命名的聊天室是用户永久保留的位置,可供用户发送消息、共享文件 和协作。

列出聊天室 应用身份验证 列出 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 中列出聊天室,请在 请求:

以下示例列出了已命名的聊天室和群聊(但并未列出直接对话) 消息(已被滤除),显示给经过身份验证的用户:

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 方法