本指南介绍了如何对 Google Chat API 的 Space
资源使用 findDirectMessage
方法,以获取有关私信 (DM) 聊天室的详细信息。
Space
资源代表用户和 Chat 应用可以发送消息、共享文件以及开展协作的地方。聊天室分为以下几种类型:
- 私信 (DM) 是两位用户或一位用户与一个 Chat 应用之间的对话。
- 群聊是指三个或更多用户与 Chat 应用之间的对话。
- 已命名聊天室是用户在其中发送消息、共享文件和开展协作的永久性位置。
如果使用应用身份验证进行身份验证,Chat 应用可以获取其在 Google Chat 中有权访问的私信(例如它所属的私信)。如果使用用户身份验证进行身份验证,系统会返回已通过身份验证的用户有权访问的 DM。
前提条件
Python
- Python 3.6 或更高版本
- pip 软件包管理工具
适用于 Python 的最新 Google 客户端库。如需安装或更新它们,请在命令行界面中运行以下命令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
- 一个启用了并配置了 Google Chat API 的 Google Cloud 项目。如需了解相关步骤,请参阅构建 Google Chat 应用。
为 Chat 应用配置授权。查找私信支持以下两种操作:
Node.js
- Node.js 和 npm
适用于 Node.js 的最新 Google 客户端库。如需安装这些软件包,请在命令行界面中运行以下命令:
npm install @google-cloud/local-auth @googleapis/chat
- 一个启用了并配置了 Google Chat API 的 Google Cloud 项目。如需了解相关步骤,请参阅构建 Google Chat 应用。
为 Chat 应用配置授权。查找私信支持以下两种操作:
查找私信
如需在 Google Chat 中查找私信,请在请求中传递以下内容:
- 使用应用身份验证时,请指定
chat.bot
授权范围。使用用户身份验证时,请指定chat.spaces.readonly
或chat.spaces
授权范围。 - 对
User
资源调用findDirectMessage
方法,传递 DM 中其他用户的name
以返回结果。使用用户身份验证时,此方法会在调用用户和指定用户之间返回 DM。使用应用身份验证时,此方法会在调用应用和指定用户之间返回 DM。 - 如需将真人用户添加为聊天室成员,请指定
users/{user}
,其中{user}
是 People API 中person
的{person_id}
或者 Directory API 中user
的 ID。例如,如果 People API 用户resourceName
为people/123456789
,您可以将users/123456789
作为member.name
添加成员资格,从而将用户添加到聊天室。
查找已通过用户身份验证的私信
下面介绍了如何查找使用用户身份验证的私信:
Python
- 在您的工作目录中,创建一个名为
chat_space_find_dm_user.py
的文件。 在
chat_space_find_dm_user.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.spaces.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then returns details about a specified DM. ''' # 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.spaces().findDirectMessage( # The other user in the direct message (DM) to return. # # Replace USER with a user name. name='users/USER' ).execute() # Prints details about the direct message. print(result) if __name__ == '__main__': main()
在代码中,将
USER
替换为 Google Chat 中User
的name
。在您的工作目录中,构建并运行示例:
python3 chat_space_find_dm_user.py
Node.js
在您的工作目录中,创建一个名为
find-direct-message-space.js
的文件。在
find-direct-message-space.js
中添加以下代码:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Find a direct message Chat space for a user. * @return {!Promise<!Object>} */ async function findDirectMessageSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces.readonly', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.findDirectMessage( {name: 'users/USER'}); } findDirectMessageSpace().then(console.log);
在代码中,将
USER
替换为 Google Chat 中User
的name
。在您的工作目录中,运行示例:
node find-direct-message-space.js
Chat API 会返回 Space
的实例,其中详细说明了指定 DM。
查找通过应用身份验证的私信
通过应用身份验证查找私信的方法如下:
Python
- 在您的工作目录中,创建一个名为
chat_space_find_dm_app.py
的文件。 在
chat_space_find_dm_app.py
中添加以下代码:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Use the service endpoint to call Chat API. result = chat.spaces().findDirectMessage( # The other user in the direct message (DM) to return. # # Replace USER with a user name. name='users/USER' ).execute() print(result)
在代码中,将
USER
替换为 Google Chat 中User
的name
。在您的工作目录中,构建并运行示例:
python3 chat_space_find_dm_app.py
Node.js
在您的工作目录中,创建一个名为
app-find-direct-message-space.js
的文件。在
app-find-direct-message-space.js
中添加以下代码:const chat = require('@googleapis/chat'); /** * Find a direct message Chat space for a user. * @return {!Promise<!Object>} */ async function findDirectMessageSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.bot', ]; const auth = new chat.auth.GoogleAuth({ scopes, keyFilename: 'credentials.json', }); const authClient = await auth.getClient(); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.findDirectMessage( {name: 'users/USER'}); } findDirectMessageSpace().then(console.log);
在代码中,将
USER
替换为 Google Chat 中User
的name
。在您的工作目录中,运行示例:
node app-find-direct-message-space.js
Chat API 会返回 Space
的实例,其中详细说明了指定私信。