This guide explains how to use the download
method on the Media
resource of
the Google Chat API to download media (a file) from a message in Google Chat.
When the user sends a message to your app, Google Chat dispatches a
MESSAGE
interaction event.
The interaction event received by your app includes a request body, which is the
JSON payload representing the interaction event, including any attachments. The
data in the attachment is different depending on whether the attachment is
uploaded content (a local file) or is a file stored on Drive. The
Media
resource
represents a file uploaded to Google Chat, like images, videos, and documents.
The
Attachment
resource
represents an instance of media—a file—attached to a message. The Attachment
resource includes the metadata about the attachment, such as
where it's saved.
Prerequisites
Python
- Python 3.6 or greater
- The pip package management tool
The latest Google client libraries for Python. To install or update 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. Downloading media supports both:
- User authentication
with the
chat.messages.readonly
orchat.messages
authorization scope. - App authentication
with the
chat.bot
authorization scope.
- User authentication
with the
Download from a file attachment
To download media from a file attachment, pass the following in your request:
- With user authentication, specify the
chat.messages.readonly
orchat.messages
authorization scope. With app authentication, specify thechat.bot
authorization scope. - Call the following Google Chat methods:
- Get
attachmentDataRef
by calling one of the following methods:- The
get
method on theAttachment
resource. - The
get
method or thelist
method on theMessage
resource.
- The
- Call the
download
method on theMedia
resource, and specify the previously retrievedattachmentDataRef.resourceName
asmedia.download.resourceName
.
- Get
The following example downloads a file attached to a message:
Python
- In your working directory, create a file named
chat_media_and_attachment_download.py
. Include the following code in
chat_media_and_attachment_download.py
:import os.path import io 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 MediaIoBaseDownload # 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"] def main(): ''' Authenticates with Chat API via user credentials, then downloads a file attached to a 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. chat = build('chat', 'v1', credentials=creds) # Download media resource. request = chat.media().download_media( resourceName=RESOURCE_NAME, ) file = io.BytesIO() downloader = MediaIoBaseDownload(file, request) done = False while done is False: status, done = downloader.next_chunk() if status.total_size: print(f'Total size: {status.total_size}') print(f'Download {int(status.progress() * 100)}') if __name__ == '__main__': main()
In the code, replace
RESOURCE_NAME
withattachmentDataRef.resourceName
, which you can retrieve one of the following ways:- The
get
method on theAttachment
resource. - The
get
method on theMessage
resource. - The
list
method on theMessage
resource.
- The
In your working directory, build and run the sample:
python3 chat_media_and_attachment_download.py
If successful, this method returns the file content as bytes.
To download the file contents, choose one of the following approaches:
We recommend using the
MediaIoBaseDownload
class in Python, which contains methods to download the file in sections and save the contents to an output stream.If you must make the HTTP request manually, call the
download
method and specify the portion of the file that you want to download by using a byte range with theRange
header—for example:Range: bytes=500-999
.
Related topics
- If the message is a Drive file, use the Drive API to get access to the file.
- Upload media as a file attachment
- Download media as a file attachment
- Get metadata about a message attachment