获取有关用户聊天室读取状态的详细信息

本指南介绍了如何在getSpaceReadState Google Chat API 的 SpaceReadState 资源,用于获取有关用户读取操作的详细信息 状态。如需获取消息线程中某封邮件的读取状态, 请参阅 获取有关用户的线程读取状态的详细信息

通过 SpaceReadState 资源 是一种单例资源, 指定用户最近在 Google Chat 聊天室中阅读的消息。

前提条件

Python

Node.js

Apps 脚本

获取发起调用的用户的聊天室读取状态

如需获取用户在聊天室中读取状态的详细信息,请添加以下代码 :

  • 指定 chat.users.readstatechat.users.readstate.readonly 授权范围。
  • 调用 getSpaceReadState 方法SpaceReadState 资源
  • 传递要获取的聊天室读取状态的 name,其中包括用户 ID 或 别名和聊天室 ID。获取空间读取状态仅支持获取读取 调用用户的状态,可以通过设置 以下: <ph type="x-smartling-placeholder">
      </ph>
    • me 别名。例如 users/me/spaces/SPACE/spaceReadState
    • 发起调用的用户的 Workspace 电子邮件地址。例如,users/user@example.com/spaces/SPACE/spaceReadState
    • 发起调用的用户的用户 ID。例如,users/USER/spaces/SPACE/spaceReadState

以下示例会获取发起调用的用户的空间读取状态:

Python

  1. 在您的工作目录中,创建一个名为 chat_spaceReadState_get.py
  2. chat_spaceReadState_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 space 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().getSpaceReadState(
    
            # The space 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/spaceReadState'
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中进行以下替换:

    • SPACE:聊天室名称。 您可以从 spaces.list 方法 或通过聊天室网址发送。
  4. 在您的工作目录中,构建并运行该示例:

    python3 chat_spaceReadState_get.py
    

Node.js

  1. 在您的工作目录中,创建一个名为 chat_spaceReadState_get.js
  2. chat_spaceReadState_get 中添加以下代码:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then gets the space read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function getSpaceReadState() {
    
      /**
      * 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.getSpaceReadState({
    
        /**
        * The space 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/spaceReadState'
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getSpaceReadState().then(console.log);
    
  3. 在代码中进行以下替换:

    • SPACE:聊天室名称。 您可以从 spaces.list 方法 或通过聊天室网址发送。
  4. 在您的工作目录中,构建并运行该示例:

    node chat_spaceReadState_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 space read state for the calling user.
    * @param {string} spaceReadStateName The resource name of the space read state.
    */
    function getSpaceReadState(spaceReadStateName) {
      try {
        Chat.Users.Spaces.getSpaceReadState(spaceReadStateName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to get read state with error %s', err.message);
      }
    }
    

Google Chat API 会获取指定的聊天室读取状态并返回 实例 SpaceReadState 资源