本指南介绍了如何对 Google Chat API 的 SpaceReadState
资源使用 getSpaceReadState
方法,以获取有关用户在聊天室中的读取状态的详细信息。如需获取消息线程中消息的读取状态,请参阅获取有关用户线程读取状态的详细信息。
SpaceReadState
资源是一种单例资源,表示 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 脚本
- 有权访问 Google Chat 的 Google Workspace 帐号。
- 一款已发布的 Chat 应用。如需构建 Chat 应用,请按照此quickstart操作。
- 为 Chat 应用配置授权。要获取聊天室中用户读取状态的详细信息,需要通过
chat.users.readstate
或chat.users.readstate.readonly
授权范围进行用户身份验证。
获取发起调用的用户的聊天室读取状态
如需获取有关用户在聊天室中的读取状态的详细信息,请在请求中包含以下内容:
- 指定
chat.users.readstate
或chat.users.readstate.readonly
授权范围。 - 对
SpaceReadState
资源调用getSpaceReadState
方法。 - 传递要获取的聊天室读取状态的
name
,其中包括用户 ID 或别名以及聊天室 ID。获取空间读取状态仅支持获取发起调用的用户的读取状态,可以通过设置以下某项来指定该状态:me
别名。例如users/me/spaces/SPACE/spaceReadState
。- 发起调用的用户的 Workspace 电子邮件地址。例如,
users/user@example.com/spaces/SPACE/spaceReadState
。 - 发起调用的用户的用户 ID。例如,
users/USER/spaces/SPACE/spaceReadState
。
以下示例会获取发起调用的用户的聊天室读取状态:
Python
- 在您的工作目录中,创建一个名为
chat_spaceReadState_get.py
的文件。 在
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()
在代码中,替换以下内容:
SPACE
:聊天室名称,可通过 Chat API 中的spaces.list
方法或聊天室网址获取。
在您的工作目录中,构建并运行示例:
python3 chat_spaceReadState_get.py
Node.js
- 在您的工作目录中,创建一个名为
chat_spaceReadState_get.js
的文件。 在
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);
在代码中,替换以下内容:
SPACE
:聊天室名称,可通过 Chat API 中的spaces.list
方法或聊天室网址获取。
在您的工作目录中,构建并运行示例:
node chat_spaceReadState_get.js
Apps 脚本
此示例使用高级聊天服务调用 Chat API。
在 Apps 脚本项目的
appsscript.json
文件中添加chat.users.readstate.readonly
授权范围:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
在 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
资源的实例。