Recuperare i metadati relativi a un allegato a un messaggio
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa guida spiega come utilizzare il metodo
get()
nella risorsa Attachment dell'API
Google Chat per ottenere i metadati di un allegato del messaggio. La risposta è un'istanza della risorsa Attachment.
Quando l'utente invia un messaggio alla tua app, Google Chat invia un
MESSAGE evento di interazione.
L'evento di interazione ricevuto dalla tua app include un corpo della richiesta, ovvero il
payload JSON che rappresenta l'evento di interazione, inclusi eventuali allegati. I dati
dell'allegato variano a seconda che l'allegato sia
un contenuto caricato (un file locale) o un file archiviato su Drive. La
risorsa Media
rappresenta un file caricato su Google Chat, come immagini, video e documenti.
La risorsa
Attachment
rappresenta un'istanza di contenuti multimediali, ovvero un file, allegato a un messaggio. La risorsa Attachment
include i metadati relativi all'allegato, ad esempio
la posizione in cui è salvato.
Crea le credenziali del service account. Per eseguire l'esempio in questa guida, salva le
credenziali come file JSON denominato credentials.json nella directory locale.
import{createClientWithAppCredentials}from'./authentication-utils.js';// This sample shows how to get attachment metadata with app credentialasyncfunctionmain(){// Create a clientconstchatClient=createClientWithAppCredentials();// Initialize request argument(s)constrequest={// Replace SPACE_NAME, MESSAGE_NAME, and ATTACHMENT_NAME herename:'spaces/SPACE_NAME/messages/MESSAGE_NAME/attachments/ATTACHMENT_NAME'};// Make the requestconstresponse=awaitchatClient.getAttachment(request);// Handle the responseconsole.log(response);}main().catch(console.error);
Per eseguire questo esempio, sostituisci spaces/SPACE_NAME/messages/
MESSAGE_NAME/attachments/ATTACHMENT_NAME con il nome dell'allegato del messaggio.
L'API Chat restituisce un'istanza di
Attachment
che descrive in dettaglio i metadati dell'allegato del messaggio specificato.
[null,null,["Ultimo aggiornamento 2025-08-29 UTC."],[[["\u003cp\u003eThis guide explains how to retrieve metadata about a message attachment in Google Chat using the \u003ccode\u003eget()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eAttachment\u003c/code\u003e resource represents an instance of a file attached to a message and includes metadata like its location.\u003c/p\u003e\n"],["\u003cp\u003eTo get attachment metadata, you need to use the \u003ccode\u003echat.bot\u003c/code\u003e authorization scope and call the \u003ccode\u003eGetAttachment()\u003c/code\u003e method with the attachment's name.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites include a Google Workspace account, a Google Cloud project, and Node.js setup with necessary libraries and credentials.\u003c/p\u003e\n"]]],["The core actions involve retrieving metadata about a message attachment in Google Chat using the `get()` method. This requires the `chat.bot` authorization scope and calling `GetAttachment()`, providing the attachment's `name`. Before using the `get()` method, you must set up a Google Cloud project, configure the OAuth consent screen, and set up the Google Chat API. The `Attachment` resource represents the file, with metadata detailing its storage. The request triggers a response with `Attachment` details.\n"],null,["# Get metadata about a message attachment\n\nThis guide explains how to use the\n[`get()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.GetAttachment)\nmethod on the `Attachment` resource of the\nGoogle Chat API to get metadata about a message attachment. The response is an\ninstance of the\n[`Attachment` resource](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Attachment).\n\nWhen the user sends a message to your app, Google Chat dispatches a\n[`MESSAGE` interaction event](/workspace/chat/events#message).\nThe interaction event received by your app includes a request body, which is the\nJSON payload representing the interaction event, including any attachments. The\ndata in the attachment is different depending on whether the attachment is\nuploaded content (a local file) or is a file stored on Drive. The\n[`Media` resource](/workspace/chat/api/reference/rest/v1/media)\nrepresents a file uploaded to Google Chat, like images, videos, and documents.\nThe\n[`Attachment` resource](/workspace/chat/api/reference/rest/v1/spaces.messages.attachments)\nrepresents an instance of media---a file---attached to a message. The `Attachment`\nresource includes the metadata about the attachment, such as\nwhere it's saved.\n\nPrerequisites\n-------------\n\n\n### Node.js\n\n- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).\n\n\u003c!-- --\u003e\n\n- Set up your environment:\n - [Create a Google Cloud project](/workspace/guides/create-project).\n - [Configure the OAuth consent screen](/workspace/guides/configure-oauth-consent).\n - [Enable and configure the Google Chat API](/workspace/chat/configure-chat-api) with a name, icon, and description for your Chat app.\n - Install the Node.js [Cloud Client Library](/workspace/chat/libraries?tab=nodejs#cloud-client-libraries).\n - [Create service account credentials](/workspace/chat/authenticate-authorize-chat-app#create-service-account). To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports app authentication.\n\n\n| The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](/workspace/chat/api/reference).\n\n\u003cbr /\u003e\n\nGet a message attachment\n------------------------\n\nTo asynchronously get metadata about a message attachment in Google Chat, pass\nthe following in your request:\n\n- Specify the `chat.bot` authorization scope.\n- Call the [`GetAttachment()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.GetAttachment) method, passing the `name` of the message attachment.\n\nHere's how to get metadata about a message attachment: \n\n### Node.js\n\nchat/client-libraries/cloud/get-attachment-app-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/get-attachment-app-cred.js) \n\n```javascript\nimport {createClientWithAppCredentials} from './authentication-utils.js';\n\n// This sample shows how to get attachment metadata with app credential\nasync function main() {\n // Create a client\n const chatClient = createClientWithAppCredentials();\n\n // Initialize request argument(s)\n const request = {\n // Replace SPACE_NAME, MESSAGE_NAME, and ATTACHMENT_NAME here\n name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME/attachments/ATTACHMENT_NAME'\n };\n\n // Make the request\n const response = await chatClient.getAttachment(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace `spaces/`\u003cvar translate=\"no\"\u003eSPACE_NAME\u003c/var\u003e`/messages/\n`\u003cvar translate=\"no\"\u003eMESSAGE_NAME\u003c/var\u003e`/attachments/`\u003cvar translate=\"no\"\u003eATTACHMENT_NAME\u003c/var\u003e with the\nmessage attachment name.\n\nThe Chat API returns an instance of\n[`Attachment`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Attachment)\nthat details the metadata about the specified message attachment.\n\nRelated topics\n--------------\n\n- [Upload media as a file attachment](/workspace/chat/upload-media-attachments)\n- [Download media as a file attachment](/workspace/chat/download-media-attachments)"]]