ユーザーのスレッドの既読ステータスを更新する

このガイドでは、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。ID を取得するには、ListSpaces メソッドを呼び出すか、スペースの URL から取得します。
  • THREAD_NAME: スレッドの name の ID。この ID は、Chat API を使用して非同期でメッセージを作成した後に返されるレスポンス本文から取得できます。また、作成時にメッセージに割り当てられたカスタム名から取得することもできます。

Chat API は、指定されたスレッドの既読状態を更新し、ThreadReadState のインスタンスを返します。