查找私信 (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() 方法,并传入要返回的私信中另一用户的 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 的实例,其中包含指定私信的详细信息。