列出 Google Chat 聊天室中的活动

本指南介绍了如何使用 list 针对 SpaceEvent 资源的 来列出聊天室中资源的更改。

SpaceEvent 资源 表示对目标空间(包括空间的子资源)的更改 例如消息、回应和会员资格如需详细了解 支持的事件类型和事件载荷的列表,请参阅 eventTypeSpaceEvent 资源payload 字段 参考文档。

您最多可以在请求时间之前 28 天内列出事件。服务器 返回包含受影响资源最新版本的事件。 例如,如果您列出与新聊天室成员相关的活动,服务器会返回 Membership 项资源,包含最新的成员资格详细信息。如果是新的 成员在请求的期限内被删除,则事件有效负载包含 空 Membership 资源。

要调用此方法,您必须使用 user 身份验证。收件人列表 在聊天室中发起活动,则经过身份验证的用户必须是聊天室成员。

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 最新的 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

列出聊天室事件

如需列出 Chat 聊天室中的聊天室活动,请执行以下操作:

  • 调用 list 方法SpaceEvent 资源
  • 使用 filter 字段。您必须至少指定一种事件类型,还可以按以下条件进行过滤 日期。如需查看支持的事件类型的列表,请参阅 eventType 字段
  • 用户:<ph type="x-smartling-placeholder"></ph> 身份验证,指定 一个或多个授权范围,以支持请求中的每种事件类型。 最佳做法是,选择仍然 让应用正常运行要选择范围,请参阅 身份验证和授权概览

在以下代码示例中,您将列出有关新成员资格和 消息。

Python

  1. 在您的工作目录中,创建一个名为 chat_space_event_list.py 的文件。
  2. chat_space_event_list.py 中添加以下代码:

    """Lists SpaceEvent resources from the Chat API."""
    
    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.memberships.readonly",
    "https://www.googleapis.com/auth/chat.messages.readonly"]
    
    # 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().spaceEvents().list(
    
        # The space from which to list events.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        parent='spaces/SPACE',
    
        # A required filter. Filters and returns events about new memberships and messages
        filter='event_types:"google.workspace.chat.membership.v1.created" OR event_types:"google.workspace.chat.message.v1.created"'
    
    ).execute()
    
    # Prints details about the created space events.
    print(result)
    
  3. 在代码中进行以下替换:

    • SPACE:聊天室名称,您可以从中获取 spaces.list 方法 或通过聊天室网址发送。
  4. 在您的工作目录中,构建并运行该示例:

    python3 chat_space_event_list.py
    

Chat API 会返回 SpaceEvent 项资源 与新会员和消息相关的活动。

自定义分页

(可选)传递以下查询参数以自定义分页:

  • pageSize:要返回的 SpaceEvent 资源数上限。 服务返回的值可能会少于此值。负值会返回 INVALID_ARGUMENT 个错误。
  • pageToken:从上一个列表空间事件调用接收的页面令牌。 提供此令牌以检索后续页面。进行分页时, 过滤条件值应与提供页面令牌的调用匹配。传递一个 不同的值可能会导致意外结果。