本指南說明如何使用 Google Chat API 的 Media
資源上的 upload
方法,將媒體 (檔案) 上傳至 Google Chat,然後附加至訊息。
使用者傳送訊息給應用程式時,Google Chat 會傳送MESSAGE
互動事件。
應用程式收到的互動事件包含要求內文,也就是代表互動事件的 JSON 酬載,包括任何附件。附件中的資料會因附件是上傳內容 (本機檔案) 或儲存在雲端硬碟中的檔案而異。Media
資源代表上傳至 Google Chat 的檔案,例如圖片、影片和文件。Attachment
資源代表附加至訊息的媒體 (檔案) 執行個體。Attachment
資源包含附件的中繼資料,例如儲存位置。
必要條件
Python
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 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 封鎖的檔案類型」。
- 訊息不得包含配件小工具。