Get metadata about a message attachment

This guide explains how to use the get() method on the Attachment 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

Node.js

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 GetAttachment() method, passing the name of the message attachment.

Here's how to get metadata about a message attachment:

Node.js

chat/client-libraries/cloud/get-attachment-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to get attachment metadata with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME, MESSAGE_NAME, and ATTACHMENT_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME/attachments/ATTACHMENT_NAME'
  };

  // Make the request
  const response = await chatClient.getAttachment(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

To run this sample, replace spaces/SPACE_NAME/messages/ MESSAGE_NAME/attachments/ATTACHMENT_NAME with the message attachment name.

The Chat API returns an instance of Attachment that details the metadata about the specified message attachment.