本指南說明如何使用 Google Chat API ThreadReadState 資源的 update 方法,將訊息串標示為已讀或未讀。
ThreadReadState 資源是單一資源,代表指定使用者在 Google Chat 訊息串中最後讀取的訊息詳細資料。
必要條件
Node.js
- Business 或 Enterprise Google Workspace 帳戶,且可存取 Google Chat。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,為 Chat 擴充應用程式指定名稱、圖示和說明。
- 安裝 Node.js Cloud 用戶端程式庫。
- 為電腦版應用程式建立 OAuth 用戶端 ID 憑證。如要執行本指南中的範例,請將憑證儲存為 JSON 檔案,並命名為
credentials.json,然後儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
Python
- Business 或 Enterprise Google Workspace 帳戶,且可存取 Google Chat。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,為 Chat 擴充應用程式指定名稱、圖示和說明。
- 安裝 Python Cloud 用戶端程式庫。
- 為電腦版應用程式建立 OAuth 用戶端 ID 憑證。如要執行本指南中的範例,請將憑證儲存為 JSON 檔案,並命名為
credentials.json,然後儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
更新呼叫使用者的討論串讀取狀態
如要更新使用者在郵件串中的讀取狀態,請在要求中加入下列內容:
- 指定
chat.users.readstate授權範圍。 - 呼叫
UpdateThreadReadState方法。 - 傳遞
updateMask,值為lastReadTime。 - 使用下列程式碼,將
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,或使用建立訊息時指派的自訂名稱。
Chat API 會更新指定的討論串讀取狀態,並傳回 ThreadReadState 的例項。