本指南介绍了如何对 Google Chat API 的 ThreadReadState 资源使用 update 方法,将消息串标记为已读或未读。
ThreadReadState 资源是一个单例资源,表示有关指定用户在 Google Chat 消息串中最后阅读的聊天消息的详细信息。
前提条件
Node.js
- 可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用设置名称、图标和说明。
- 安装 Node.js Cloud 客户端库。
- 为桌面应用创建 OAuth 客户端 ID 凭据。如需运行本指南中的示例,请将凭据保存为名为
credentials.json的 JSON 文件,并将其保存到本地目录。
- 选择支持用户身份验证的授权范围。
Python
- 可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用设置名称、图标和说明。
- 安装 Python Cloud 客户端库。
- 为桌面应用创建 OAuth 客户端 ID 凭据。如需运行本指南中的示例,请将凭据保存为名为
credentials.json的 JSON 文件,并将其保存到本地目录。
- 选择支持用户身份验证的授权范围。
更新调用用户的会话阅读状态
如需更新用户在消息串中的已读状态,请在请求中添加以下内容:
- 指定
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 的实例。