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

本指南說明如何針對以下項目的 Media 資源使用 upload 方法: 透過 Google Chat API 將媒體 (檔案) 上傳至 Google Chat,然後再附加至 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

限制和注意事項

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