取得使用者和 #39 執行緒讀取狀態的詳細資料

本指南說明如何在以下項目中使用 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.readstatechat.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

本範例使用 進階 Chat 服務

  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 會取得指定執行緒讀取狀態,並傳回 執行個體 ThreadReadState 資源