更新使用者的討論串讀取狀態

本指南說明如何使用 Google Chat API ThreadReadState 資源的 update 方法,將訊息串標示為已讀或未讀。

ThreadReadState 資源是單一資源,代表指定使用者在 Google Chat 訊息串中最後讀取的訊息詳細資料。

必要條件

Node.js

Python

更新呼叫使用者的討論串讀取狀態

如要更新使用者在郵件串中的讀取狀態,請在要求中加入下列內容:

  • 指定 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 的例項。