This guide explains how to use the Google Chat API to upload media (a file) to Google Chat and then attach it to a message.
Prerequisites
Python
- Python 3.6 or greater
- The pip package management tool
The Google client libraries for Python. To install them, run the following command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
A published Chat app. To create and publish a Chat app, see Build a Google Chat app.
Authorization configured for the Chat app. Uploading media as a file attachment requires User authentication with the
chat.messages.create
orchat.messages
authorization scope.
Upload as a file attachment
To upload media and attach it to a message, pass the following in your request::
- Specify the
chat.messages.create
orchat.messages
authorization scope. - Call the following Google Chat methods:
- To upload the file, call the
upload
method on theMedia
resource.- Set
parent
to the space name of the space that hosts the file. - In
body
(the request body), setfilename
to the name of the uploaded file attachment. - Set
media_body
as an instance of the file to be uploaded.
- Set
- To create a message with the uploaded file attached, call the
create
method on theMessages
resource.- Set
attachment
as the response from calling theupload
method on theMedia
resource. Theattachment
field accepts a list.
- Set
- To upload the file, call the
The following example uploads a PNG image file and attaches it to a message.
Python
- In your working directory, create a file named
chat_media_and_attachment_upload.py
. Include the following code in
chat_media_and_attachment_upload.py
:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError 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()
In the code, replace
SPACE
with the space name to upload the attachment in, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, build and run the sample:
python3 chat_media_and_attachment_upload.py
The Chat API returns a response body containing
attachmentDataRef
with details about the uploaded file.
Limits and considerations
As you prepare to upload files and attach them to messages, take note of these limits and considerations:
- You can upload file sizes up to 200 MB.
- Some file types aren't supported, and can't be uploaded. For details, see File types blocked in Google Chat.