קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
במדריך הזה מוסבר איך להשתמש בשיטה get() במשאב Attachment של Google Chat API כדי לקבל מטא-נתונים על קובץ מצורף להודעה. התשובה היא מופע של Attachment המשאב.
כשהמשתמש שולח הודעה לאפליקציה, מערכת Google Chat שולחת אירוע אינטראקציה MESSAGE.
אירוע האינטראקציה שמתקבל באפליקציה כולל גוף בקשה, שהוא מטען ייעודי (payload) בפורמט JSON שמייצג את אירוע האינטראקציה, כולל קבצים מצורפים. הנתונים בקובץ המצורף שונים בהתאם לסוג הקובץ המצורף: תוכן שהועלה (קובץ מקומי) או קובץ ששמור ב-Drive. המשאב Media מייצג קובץ שהועלה ל-Google Chat, כמו תמונות, סרטונים ומסמכים.
המשאב Attachment מייצג מכונה של מדיה – קובץ – שמצורפת להודעה. Attachmentהמשאב כולל את המטא-נתונים של הקובץ המצורף, כמו המיקום שבו הוא נשמר.
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);
כדי להריץ את הדוגמה הזו, מחליפים את spaces/SPACE_NAME/messages/
MESSAGE_NAME/attachments/ATTACHMENT_NAME בשם הקובץ המצורף להודעה.
ה-Chat API מחזיר מופע של Attachment שמפרט את המטא-נתונים לגבי הקובץ המצורף להודעה שצוינה.
[null,null,["עדכון אחרון: 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)"]]