ユーザーのスレッドの読み取り状態の詳細を取得する

このガイドでは、Terraform で getThreadReadState メソッドを使用する方法について説明します。 ユーザーの会話に関する詳細情報を取得するための Google Chat API の ThreadReadState リソース 読み取り状態を保持できます。特定のスレッドでメッセージの読み取り状態を スペースについては、以下をご覧ください。 ユーザーのスペース読み取り状態の詳細を確認する

ThreadReadState リソース 特定のリソースの詳細情報を表すシングルトン リソース Google Chat のメッセージ スレッドで最後に読まれたメッセージ。

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • 最新の Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 以降
  • npm パッケージ管理ツール
  • 最新の Google クライアント ライブラリ。インストールまたは更新する手順は次のとおりです。 コマンドライン インターフェースで次のコマンドを実行します。
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

呼び出し元のユーザーのスレッド読み取り状態を取得する

メッセージ スレッド内のユーザーの既読状態の詳細を取得するには、 追加します。

  • chat.users.readstate または chat.users.readstate.readonly を指定する 認可スコープを指定します。
  • 呼び出し getThreadReadState メソッド 日付 ThreadReadState リソース
  • 取得するスレッド読み取り状態の 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

  1. 作業ディレクトリに、先ほど作成した chat_threadReadState_get.py
  2. 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()
    
  3. コードの次のように置き換えます。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_threadReadState_get.py
    

Node.js

  1. 作業ディレクトリに、先ほど作成した chat_threadReadState_get.js
  2. 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);
    
  3. コードの次のように置き換えます。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    node chat_threadReadState_get.js
    

Apps Script

この例では、API 呼び出しを使用して Chat API を呼び出し、 高度なチャット サービス

  1. chat.users.readstate.readonly 承認スコープを以下に追加します。 Apps Script プロジェクトの appsscript.json ファイル:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. このような関数を 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 は指定されたスレッドの読み取り状態を取得し、 Pod の ThreadReadState リソース