本指南介绍了如何使用 Google Chat API 的 Media
资源中的 upload
方法将媒体内容(文件)上传到 Google Chat,然后将其附加到消息。
当用户向您的应用发送消息时,Google Chat 会调度 MESSAGE
互动事件。您的应用收到的互动事件包含一个请求正文,即表示互动事件的 JSON 载荷,包括任何附件。附件中的数据因附件是上传的内容(本地文件)还是存储在云端硬盘中的文件而异。Media
资源表示上传到 Google Chat 的文件,例如图片、视频和文档。
Attachment
资源表示附加到消息的媒体(文件)实例。Attachment
资源包含有关附件的元数据,例如附件的保存位置。
前提条件
Python
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 同意屏幕。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、图标和说明。
- 安装 Python Google API 客户端库。
- 为桌面应用
创建 OAuth 客户端 ID 凭据。如需运行本指南中的示例,请将凭据保存为名为
credentials.json
的 JSON 文件,并将其保存到本地目录。
- 选择支持用户身份验证的授权范围。
以文件附件的形式上传
如需上传媒体并将其附加到消息,请在请求中传递以下内容:
- 指定
chat.messages.create
或chat.messages
授权范围。 - 调用以下 Google Chat 方法:
- 如需上传文件,请对
Media
资源调用upload
方法。- 将
parent
设置为托管相应文件的聊天室的聊天室名称。 - 在
body
(请求正文)中,将filename
设置为已上传的文件附件的名称。 - 将
media_body
设置为要上传的文件的实例。
- 将
- 如需创建附加了上传文件的消息,请对
Messages
资源调用create
方法。- 将
attachment
设置为对Media
资源调用upload
方法后的响应。attachment
字段接受列表。
- 将
- 如需上传文件,请对
以下示例上传了一个 PNG 图片文件,并将其附加到邮件中。
Python
- 在工作目录中,创建一个名为
chat_media_and_attachment_upload.py
的文件。 在
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( 'credentials.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()
在代码中,将
SPACE
替换为要上传附件的聊天室名称,该名称可通过 Chat API 中的spaces.list
方法或从聊天室的网址获取。在工作目录中,构建并运行示例:
python3 chat_media_and_attachment_upload.py
Chat API 会返回一个包含 attachmentDataRef
的响应正文,其中包含有关上传文件的详细信息。
限制和注意事项
在准备上传文件并将其附加到邮件时,请注意以下限制和注意事项:
- 您可以上传大小不超过 200 MB 的文件。
- 部分文件类型不受支持,无法上传。有关详情,请参阅 Google Chat 中屏蔽的文件类型。
- 您的消息必须省略辅助 widget。