获取有关用户聊天室读取状态的详细信息

本指南介绍了如何使用 Google Chat API 的 SpaceReadState 资源中的 get() 方法来获取有关用户在聊天室中的已读状态的详细信息。如需获取消息线程中消息的已读状态,请参阅获取有关用户线程已读状态的详细信息

SpaceReadState 资源是一个单例资源,用于表示指定用户在 Google Chat 聊天室中最后一条已读消息的详细信息。

前提条件

Node.js

获取调用用户的空间读取状态

如需获取有关用户在空间内的已读状态的详细信息,请在请求中添加以下内容:

  • 指定 chat.users.readstatechat.users.readstate.readonly 授权范围。
  • 调用 GetSpaceReadState() 方法,传递聊天室读取状态的 name 以获取包含用户 ID 或别名和聊天室 ID 的聊天室读取状态。获取空间读取状态仅支持获取调用用户的读取状态,该状态可通过设置以下任一参数来指定:
    • me 别名。例如 users/me/spaces/SPACE/spaceReadState
    • 调用用户的 Workspace 电子邮件地址。例如,users/user@example.com/spaces/SPACE/spaceReadState
    • 调用用户的用户 ID。例如,users/USER/spaces/SPACE/spaceReadState

以下示例用于获取调用用户的空间读取状态:

Node.js

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

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

// This sample shows how to get the space read state for the calling user
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    name: 'users/me/spaces/SPACE_NAME/spaceReadState',
  };

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

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

main().catch(console.error);

如需运行此示例,请将 SPACE_NAME 替换为相应空间的 name 中的 ID。 您可以通过调用 ListSpaces() 方法或从空间的网址中获取 ID。

Google Chat API 会获取指定聊天室的已读状态,并返回 SpaceReadState 的实例。