以文件附件形式上传媒体内容

本指南介绍了如何对以下资源的 Media 资源使用 upload 方法: 使用 Google Chat API 将媒体(文件)上传到 Google Chat,然后将其附加到 消息。

当用户向您的应用发送消息时,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
    

作为文件附件上传

要上传媒体并将其附加到消息,请在 请求:

  • 指定 chat.messages.createchat.messages 授权范围。
  • 调用以下 Google Chat 方法:
    1. 要上传文件,请调用 upload 方法 针对 Media 资源。
      • parent 设为托管文件的空间的名称。
      • body(请求正文)中,将 filename 设置为上传的 文件附件。
      • media_body 设置为要上传的文件的实例。
    2. 要创建附加了上传的文件的邮件,请调用 create 方法Messages 资源

以下示例会上传一个 PNG 图片文件,并将其附加到邮件中。

Python

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

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.http import MediaFileUpload
    
    # 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.messages.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then uploads a file as media, creates a message, and
        attaches the file to the message.
        '''
    
        # 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.
        service = build('chat', 'v1', credentials=creds)
    
        # Upload a file to Google Chat.
        media = MediaFileUpload('test_image.png', mimetype='image/png')
    
        # Create a message and attach the uploaded file to it.
        attachment_uploaded = service.media().upload(
    
            # The space to upload the attachment in.
            #
            # 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',
    
            # The filename of the attachment, including the file extension.
            body={'filename': 'test_image.png'},
    
            # Media resource of the attachment.
            media_body=media
    
        ).execute()
    
        print(attachment_uploaded)
    
        # Create a Chat message with attachment.
        result = service.spaces().messages().create(
    
            # The space to create the message in.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Must match the space name that the attachment is uploaded to.
            parent='spaces/SPACE',
    
            # The message to create.
            body={
                'text': 'Hello, world!',
                'attachment': [attachment_uploaded]
            }
    
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,将 SPACE 替换为聊天室名称, 上传附件,您可以从 spaces.list 方法 或通过聊天室网址发送。

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

    python3 chat_media_and_attachment_upload.py
    

Chat API 返回一个响应正文,其中包含 将 attachmentDataRef 替换为上传文件的详细信息。

限制和注意事项

在准备上传文件并将其附加到邮件时,请注意以下事项 限制和注意事项: