尋找即時訊息 (DM) 聊天室

本指南說明如何使用 Google Chat API Space 資源的 findDirectMessage() 方法,取得即時通訊 (DM) 聊天室的詳細資料。

Space資源代表使用者和 Chat 應用程式可傳送訊息、共用檔案及協作的空間。聊天室分為以下幾種類型:

  • 即時訊息 (DM) 是指兩位使用者之間,或使用者與 Chat 應用程式之間的對話。
  • 群組對話是指三位以上使用者和即時通訊應用程式之間的對話。
  • 具名聊天室是持續存在的空間,可供使用者傳送訊息、分享檔案及協作。

如果 Google Workspace 管理員為整個 Google Workspace 機構安裝 Chat 應用程式,Google Chat 會在已安裝的 Chat 應用程式和機構中的每位使用者之間建立即時通訊。

透過應用程式驗證進行驗證後,Chat 應用程式就能在 Google Chat 中存取可存取的直接訊息 (例如應用程式是成員的直接訊息)。透過使用者驗證進行驗證,會傳回通過驗證的使用者可存取的直接訊息。

必要條件

Node.js

尋找即時訊息

如要在 Google Chat 中尋找即時訊息,請在要求中傳遞下列項目:

  • 使用應用程式驗證時,請指定 chat.bot 授權範圍。使用使用者驗證時,請指定 chat.spaces.readonlychat.spaces 授權範圍。
  • 呼叫 FindDirectMessage() 方法,傳遞 DM 中其他使用者的 name 以傳回。透過使用者驗證,這個方法會傳回發出呼叫的使用者與指定使用者之間的私訊。透過應用程式驗證,這個方法會傳回呼叫應用程式與指定使用者之間的私訊。
  • 如要將真人使用者新增為聊天室成員,請指定 users/{user},其中 {user} 是 People API 中 person{person_id},或是 Directory API 中 user 的 ID。舉例來說,如果 People API 人員 resourceNamepeople/123456789,您可以加入成員,並將 users/123456789 設為 member.name,將使用者新增至聊天室。

尋找需要使用者驗證的即時訊息

如要尋找已驗證使用者的即時訊息,請按照下列步驟操作:

Node.js

chat/client-libraries/cloud/find-dm-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.spaces.readonly'];

// This sample shows how to find a Direct Message space with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace USER_NAME here
    name: 'users/USER_NAME'
  };

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

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

main().catch(console.error);

如要執行這個範例,請將 USER_NAME 換成使用者 name 欄位中的 ID。

Chat API 會傳回 Space 執行個體,詳細說明指定的直接訊息。

尋找需要應用程式驗證的即時訊息

如要尋找已啟用應用程式驗證的即時訊息,請按照下列步驟操作:

Node.js

chat/client-libraries/cloud/find-dm-space-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to find a Direct Message space with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace USER_NAME here
    name: 'users/USER_NAME'
  };

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

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

main().catch(console.error);

如要執行這個範例,請將 USER_NAME 換成使用者 name 欄位中的 ID。

Chat API 會傳回 Space 執行個體,詳細說明指定的直接訊息。