更新用户的会话已读状态

本指南介绍了如何对 Google Chat API 的 ThreadReadState 资源使用 update 方法,将消息串标记为已读或未读。

ThreadReadState 资源是一个单例资源,表示有关指定用户在 Google Chat 消息串中最后阅读的聊天消息的详细信息。

前提条件

Node.js

Python

更新调用用户的会话阅读状态

如需更新用户在消息串中的已读状态,请在请求中添加以下内容:

  • 指定 chat.users.readstate 授权范围。
  • 调用 UpdateThreadReadState 方法。
  • 传递值为 lastReadTime 的 updateMask。
  • 使用以下代码将 threadReadState 作为 ThreadReadState 的实例传递:
    • name 字段设置为要更新的消息串已读状态,其中包括用户 ID 或别名、聊天室 ID 和消息串 ID。更新会话读取状态仅支持更新调用用户的读取状态,可以通过设置以下任一选项来指定:
      • me 别名。例如 users/me/spaces/SPACE/threads/THREAD/threadReadState。
      • 发起通话的用户的 Workspace 邮箱。例如,users/user@example.com/spaces/SPACE/threads/THREAD/threadReadState。
      • 发起通话的用户的用户 ID。例如,users/USER/spaces/SPACE/threads/THREAD/threadReadState。
    • lastReadTime 字段设置为标记调用用户在会话串中的最后阅读位置的时间戳,例如最后阅读的消息的时间戳或当前时间:
      • 标记为未读:将 lastReadTime 设置为早于消息串中最新消息创建时间的时间戳。在 Chat 中,该消息串会从该时间戳开始显示为未读,同时更新未读标记、将消息串加粗,并将其显示在首页中。
      • 标记为已读:将 lastReadTime 设置为等于或晚于会话中最新消息创建时间的时间戳。如果您指定未来的时间戳,API 会将 lastReadTime 强制转换为当前请求时间。
      • 时间戳下限:lastReadTime 字段必须在 Unix 纪元 (1970-01-01T00:00:00Z) 当天或之后。设置负时间戳(1970-01-01T00:00:00Z 之前)会返回 INVALID_ARGUMENT (400) 错误。

以下示例会更新调用用户的会话已读状态:

Node.js

/**
 * This sample shows how to update the calling user's thread read state.
 *
 * It relies on the @google-apps/chat npm package.
 */
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rest/v1/users.spaces.threads/updateThreadReadState

const {ChatServiceClient} = require('@google-apps/chat');
const {auth} = require('google-auth-library');

async function main() {
  // Create a client
  const chatClient = new ChatServiceClient({
    authClient: await auth.getClient({
      scopes: ['https://www.googleapis.com/auth/chat.users.readstate']
    })
  });

  // Initialize request arguments.
  const request = {
    threadReadState: {
      name: 'users/me/spaces/SPACE_NAME/threads/THREAD_NAME/threadReadState',
      lastReadTime: {
        seconds: Math.floor(Date.now() / 1000),
        nanos: 0
      }
    },
    updateMask: {
      paths: ['last_read_time']
    }
  };

  // Make the request
  const response = await chatClient.updateThreadReadState(request);
  console.log(response);
}

main().catch(console.error);

Python

"""
This sample shows how to update the calling user's thread read state.
"""
from google.apps import chat_v1
import google.auth
from google.protobuf import timestamp_pb2

# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rest/v1/users.spaces.threads/updateThreadReadState

def update_thread_read_state():
    # Authenticate with Chat API using user credentials.
    credentials, _ = google.auth.default(
        scopes=["https://www.googleapis.com/auth/chat.users.readstate"]
    )
    client = chat_v1.ChatServiceClient(credentials=credentials)

    # Set timestamp for last read time.
    now = timestamp_pb2.Timestamp()
    now.GetCurrentTime()

    thread_read_state = chat_v1.ThreadReadState(
        name="users/me/spaces/SPACE_NAME/threads/THREAD_NAME/threadReadState",
        last_read_time=now,
    )

    request = chat_v1.UpdateThreadReadStateRequest(
        thread_read_state=thread_read_state,
        update_mask={"paths": ["last_read_time"]},
    )

    response = client.update_thread_read_state(request=request)
    print(response)

if __name__ == "__main__":
    update_thread_read_state()

如需运行此示例,请替换以下内容:

  • SPACE_NAME:聊天室的 name 中的 ID。您可以通过调用 ListSpaces 方法或从聊天室的网址中获取该 ID。
  • THREAD_NAME:来自线程的 name 的 ID。您可以从使用 Chat API 异步创建消息后返回的响应正文中获取该 ID,也可以从创建消息时分配给消息的自定义名称中获取该 ID。

Chat API 会更新指定的会话已读状态,并返回 ThreadReadState 的实例。