取得郵件附件的相關中繼資料

本指南說明如何使用 Google Chat API Attachment 資源的 get() 方法,取得郵件附件的中繼資料。回應是Attachment資源的例項。

使用者傳送訊息給應用程式時,Google Chat 會傳送MESSAGE互動事件。 應用程式收到的互動事件包含要求主體,也就是代表互動事件的 JSON 酬載,包括任何附件。附件中的資料會因附件是上傳內容 (本機檔案) 或儲存在雲端硬碟中的檔案而異。「Media」資源代表上傳至 Google Chat 的檔案,例如圖片、影片和文件。Attachment 資源代表附加至郵件的媒體 (檔案) 執行個體。Attachment 資源包含附件的中繼資料,例如附件的儲存位置。

必要條件

Node.js

取得郵件附件

如要非同步取得 Google Chat 訊息附件的中繼資料,請在要求中傳遞下列項目:

  • 指定 chat.bot 授權範圍。
  • 呼叫 GetAttachment() 方法,並傳遞郵件附件的 name

如要取得郵件附件的中繼資料,請按照下列步驟操作:

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);
}

await main();

如要執行這個範例,請將 spaces/SPACE_NAME/messages/ MESSAGE_NAME/attachments/ATTACHMENT_NAME 替換成郵件附件名稱。

Chat API 會傳回 Attachment 的執行個體,其中詳述指定訊息附件的中繼資料。