This guide explains how to use the
get method on the
Media resource of the
Google Chat API to get metadata about a message attachment. The response is an
instance of the
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
represents a file uploaded to Google Chat, like images, videos, and documents.
represents an instance of media—a file—attached to a message. The
resource includes the metadata about the attachment, such as
where it's saved.
- 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. Getting a message requires app authentication with the
Get a message attachment
To asynchronously get metadata about a message attachment in Google Chat, pass the following in your request:
- Specify the
- Call the
getmethod on the
- Pass the
nameof the message attachment.
Here's how to get metadata about a message attachment:
- In your working directory, create a file named
Include the following code in
from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Get a Chat message. result = chat.spaces().messages().attachments().get( # The message to get. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE/attachments/ATTACHMENT' ).execute() # Print Chat API's response in your command line interface. print(result)
In the code, replace
spaces/SPACE/messages/MESSAGE/attachments/ATTACHMENTwith the message attachment name.
In your working directory, build and run the sample:
The Chat API returns an instance of
that details the metadata about the specified message attachment.