以檔案附件的形式上傳媒體

本指南說明如何使用 Google Chat API Media 資源的 upload 方法,將媒體 (檔案) 上傳至 Google Chat,然後附加至訊息。

使用者傳送訊息給應用程式時,Google Chat 會傳送MESSAGE互動事件。 應用程式收到的互動事件包含要求主體,也就是代表互動事件的 JSON 酬載,包括任何附件。附件中的資料會因附件是上傳內容 (本機檔案) 或儲存在雲端硬碟中的檔案而異。「Media」資源代表上傳至 Google Chat 的檔案,例如圖片、影片和文件。Attachment 資源代表附加至訊息的媒體 (檔案) 執行個體。Attachment 資源包含附件的中繼資料,例如附件的儲存位置。

必要條件

Python

上傳為檔案附件

如要上傳媒體並附加至訊息,請在要求中傳遞下列內容:

  • 指定 chat.messages.createchat.messages 授權範圍。
  • 呼叫下列 Google Chat 方法:
    1. 如要上傳檔案,請呼叫 Media 資源的 upload 方法
      • parent 設為檔案所在聊天室的名稱。
      • body (要求主體) 中,將 filename 設為上傳的檔案附件名稱。
      • media_body 設為要上傳檔案的執行個體。
    2. 如要建立附有上傳檔案的訊息,請在 Messages 資源上呼叫 create 方法

以下範例會上傳 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(
                      '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()
    
  3. 在程式碼中,將 SPACE 替換為要上傳附件的空間名稱,您可以透過 Chat API 中的 spaces.list 方法或空間網址取得該名稱。

  4. 在工作目錄中,建構並執行範例:

    python3 chat_media_and_attachment_upload.py

Chat API 會傳回包含 attachmentDataRef 的回覆主體,其中含有上傳檔案的詳細資料。

限制和注意事項

準備上傳檔案並附加至郵件時,請注意下列限制和考量事項: