获取有关邮件附件的元数据

本指南介绍了如何在getMedia 用于获取消息附件的元数据。该响应是 的 Attachment 资源

当用户向您的应用发送消息时,Google Chat 会发送一个 MESSAGE 互动事件。 您的应用接收的互动事件包括请求正文,即 表示互动事件的 JSON 载荷,包括所有附件。通过 取决于附件是否 已上传的内容(本地文件)或存储在云端硬盘中的文件。通过 Media 资源 表示上传到 Google Chat 的文件,例如图片、视频和文档。 通过 Attachment 资源 表示附加到消息的媒体(文件)实例。Attachment 资源包含有关连接的元数据,如 保存位置

前提条件

Python

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

获取消息附件

如需异步获取 Google Chat 中消息附件的元数据,请将以下代码传递 在您的请求中:

下面介绍了如何获取有关邮件附件的元数据:

Python

  1. 在您的工作目录中,创建一个名为 chat_get_message_attachment.py
  2. chat_get_message_attachment.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)
    
    # Get a Chat message.
    result = chat.spaces().messages().attachments().get(
    
        # The message to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        #
        # Replace MESSAGE with a message name.
        # Obtain the message name from the response body returned
        # after creating a message asynchronously with Chat REST API.
        name='spaces/SPACE/messages/MESSAGE/attachments/ATTACHMENT'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. 在代码中,将 spaces/SPACE/messages/MESSAGE/attachments/ATTACHMENT 替换为邮件附件名称。

  4. 在您的工作目录中,构建并运行该示例:

    python3 chat_get_message_attachment.py
    

Chat API 会返回 Attachment 用于详细说明指定邮件附件的相关元数据。