获取有关用户的线程读取状态的详细信息

本指南介绍了如何在getThreadReadState Google Chat API 的 ThreadReadState 资源,用于获取有关用户 读取状态之间的事件。要获取 空格,请参阅 获取有关用户聊天室读取状态的详细信息

通过 ThreadReadState 资源 是一种单例资源, Google Chat 消息串中的指定用户最近阅读的消息。

前提条件

Python

Node.js

Apps 脚本

获取调用方用户的线程读取状态

要获取有关用户在消息会话串中读取状态的详细信息,请添加 以下内容:

  • 指定 chat.users.readstatechat.users.readstate.readonly 授权范围。
  • 调用 getThreadReadState 方法ThreadReadState 资源
  • 传递线程读取状态的 name 以获取状态,其中包括用户 ID 或 别名和聊天室 ID。获取线程读取状态仅支持获取读取 调用用户的状态,可以通过设置 以下: <ph type="x-smartling-placeholder">
      </ph>
    • 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 脚本

此示例使用 高级聊天服务

  1. chat.users.readstate.readonly 授权范围添加到 Apps 脚本项目的 appsscript.json 文件:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. 将这样一个函数添加到 Apps 脚本项目的 代码:

    /**
    * 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 资源