このガイドでは、Google Chat API の ThreadReadState
リソースの getThreadReadState
メソッドを使用して、メッセージ スレッド内のユーザーの既読状態の詳細を取得する方法について説明します。スペース内のメッセージの読み取り状態を取得するには、ユーザーのスペースの読み取り状態の詳細を取得するをご覧ください。
ThreadReadState
リソースは、指定したユーザーが最後に読んだ Google Chat メッセージ メッセージの詳細を表すシングルトン リソースです。
前提条件
Python
- Python 3.6 以降
- pip パッケージ管理ツール
Python 用の最新の Google クライアント ライブラリ。これらをインストールまたは更新するには、コマンドライン インターフェースで次のコマンドを実行します。
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat アプリに構成された認可。スペース内のユーザーの読み取り状態の詳細を取得するには、
chat.users.readstate
またはchat.users.readstate.readonly
認可スコープを使用したユーザー認証が必要です。
Node.js
- Node.js と npm
Node.js 用の最新の Google クライアント ライブラリ。これらをインストールするには、コマンドライン インターフェースで次のコマンドを実行します。
npm install @google-cloud/local-auth @googleapis/chat
- Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat アプリに構成された認可。スペース内のユーザーの読み取り状態の詳細を取得するには、
chat.users.readstate
またはchat.users.readstate.readonly
認可スコープを使用したユーザー認証が必要です。
Apps Script
- Google Chat へのアクセス権を持つ Google Workspace アカウント。
- 公開されている Chat アプリ。Chat アプリを作成するには、このquickstartの手順に沿ってください。
- Chat アプリに構成された認可。スペース内のユーザーの読み取り状態の詳細を取得するには、
chat.users.readstate
またはchat.users.readstate.readonly
認可スコープを使用したユーザー認証が必要です。
呼び出し元のユーザーのスレッドの読み取り状態を取得する
メッセージ スレッド内のユーザーの既読状態の詳細を取得するには、リクエストに以下を含めます。
chat.users.readstate
またはchat.users.readstate.readonly
認可スコープを指定します。ThreadReadState
リソースでgetThreadReadState
メソッドを呼び出します。- 取得するスレッド読み取り状態の
name
を渡します。これには、ユーザー ID またはエイリアスとスペース ID が含まれます。スレッドの読み取り状態の取得は、呼び出し元のユーザーの読み取り状態の取得のみをサポートしています。これは、次のいずれかを設定して指定できます。me
エイリアス。例:users/me/spaces/SPACE/threads/THREAD/threadReadState
。- 呼び出し元のユーザーの Workspace メールアドレス。次に例を示します。
users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
- 呼び出し元のユーザー ID。次に例を示します。
users/USER/spaces/SPACE/threads/THREAD/threadReadState
次の例では、呼び出し元のユーザーのスレッドの読み取り状態を取得しています。
Python
- 作業ディレクトリに
chat_threadReadState_get.py
という名前のファイルを作成します。 chat_threadReadState_get.py
に次のコードを追加します。from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.users.readstate.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets the thread read state for the calling user. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.users().spaces().threads().getThreadReadState( # The thread read state to get. # # Replace USER with the calling user's ID, Workspace email, # or the alias me. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace THREAD with a thread name. # Obtain the thread name from the messages resource of Chat API. name='users/me/spaces/SPACE/threads/THREAD/threadReadState' ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。THREAD
: スレッド名。Chat API のspaces.messages.get
メソッドから取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_threadReadState_get.py
Node.js
- 作業ディレクトリに
chat_threadReadState_get.js
という名前のファイルを作成します。 chat_threadReadState_get
に次のコードを追加します。const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @return {!Promise<!Object>} */ async function getThreadReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate.readonly', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.users.spaces.threads.getThreadReadState({ /** * The thread read state to get. * * Replace USER with the calling user's ID, Workspace email, * or the alias me. * * Replace SPACE with a space name. * Obtain the space name from the spaces resource of Chat API, * or from a space's URL. */ name: 'users/me/spaces/SPACE/threads/THREADS/threadReadState' }); } /** * Use the service endpoint to call Chat API. */ getThreadReadState().then(console.log);
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。THREAD
: スレッド名。Chat API のspaces.messages.get
メソッドから取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
node chat_threadReadState_get.js
Apps Script
この例では、高度なチャット サービスを使用して Chat API を呼び出します。
chat.users.readstate.readonly
承認スコープを Apps Script プロジェクトのappsscript.json
ファイルに追加します。"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
次のような関数を Apps Script プロジェクトのコードに追加します。
/** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @param {string} threadReadStateName The resource name of the thread read state. */ function getThreadReadState(threadReadStateName) { try { Chat.Users.Spaces.Threads.getThreadReadState(threadReadStateName); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to get read state with error %s', err.message); } }
Google Chat API は指定されたスレッド読み取り状態を取得し、ThreadReadState
リソースのインスタンスを返します。