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
Attachment
resource.
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
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
-
Create OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
Get a message attachment
To asynchronously get metadata about a message attachment in Google Chat, pass the following in your request:
- Specify the
chat.bot
authorization scope. - Call the
get
method on theAttachment
resource, - Pass the
name
of the message attachment.
Here's how to get metadata about a message attachment:
Python
- In your working directory, create a file named
chat_get_message_attachment.py
. Include the following code in
chat_get_message_attachment.py
:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # 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/ATTACHMENT
with the message attachment name.In your working directory, build and run the sample:
python3 chat_get_message_attachment.py
The Chat API returns an instance of
Attachment
that details the metadata about the specified message attachment.