本指南介绍了如何使用 Google Chat API 的 Space
资源中的 setup
方法来设置 Google Chat 聊天室。设置聊天室会创建一个聊天室,并向其中添加指定用户。
Space
资源代表用户和 Chat 应用可以发送消息、共享文件和协作的位置。聊天室分为以下几种类型:
- 私信 (DM) 是指两位用户或一位用户与 Chat 应用之间的对话。
- 群聊是三位或更多用户与 Chat 应用之间的对话。
- 已命名的聊天室是用户发送消息、共享文件和开展协作的持久位置。
设置聊天室时,请考虑以下事项:
- 发起调用(经过身份验证)的用户会自动添加到聊天室,因此您无需在请求中指定用户的成员资格。
- 创建私信 (DM) 时,如果两位用户之间存在私信,则返回该私信。否则,系统会创建私信。
- 创建群聊时,如果请求中提供的成员资格均未成功添加到群聊中(例如,存在权限问题),系统可能会创建空群聊(仅包括发起通话的用户)。
- 您无法设置启用了“话题式回复”的聊天室,也无法添加 Google Workspace 外部的人员。
- 请求中提供的重复成员资格(包括发起调用的用户)会被滤除,而不是导致请求错误。
前提条件
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.spaces.create
或chat.spaces
授权范围的用户身份验证。
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.spaces.create
或chat.spaces
授权范围的用户身份验证。
设置聊天室
如需设置聊天室,请在请求中传递以下内容:
- 指定
chat.spaces.create
或chat.spaces
授权范围。 - 对
Space
资源调用setup
方法。 - 如需将真人用户添加为聊天室成员,请指定
users/{user}
,其中{user}
是 People API 中person
的{person_id}
或 Directory API 中user
的 ID。例如,如果 People API 人员resourceName
为people/123456789
,您可以通过添加以users/123456789
作为member.name
的成员资格将用户添加到聊天室。 - 如需将群组添加为聊天室成员,请指定
groups/{group}
,其中{group}
是要为其创建成员资格的群组 ID。您可以使用 Cloud Identity API 检索群组的 ID。例如,如果 Cloud Identity API 返回名为groups/123456789
的群组,则将membership.groupMember.name
设置为groups/123456789
。无法将 Google 群组添加到群聊或私信中,只能添加到已命名的聊天室。 - 如需在发起调用的用户与其他真人用户之间创建私信,请在请求中指定真人用户的成员资格。
- 如需在发起调用的用户和发起通话的应用之间创建私信,请将
Space.singleUserBotDm
设置为true
,并且不要指定任何成员资格。您只能使用此方法为发起通话的应用设置私信。如需将发起通话的应用添加为聊天室成员或两名真人用户之间的现有私信,请参阅创建成员资格。
以下示例会创建一个命名空间,并为一个群组和三个真人用户(包括经过身份验证的用户和另外两个指定用户)创建该空间的成员资格。
Python
- 在您的工作目录中,创建一个名为
chat_space_setup.py
的文件。 在
chat_space_setup.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.create"] def main(): ''' Authenticates with Chat API via user credentials, then sets up a Chat space by creating a space and adding members. ''' # 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().setup( # Details about the space to set up. body = { # Attributes of the space to set up, like space type and display name. 'space': { # To set up a named space, set spaceType to SPACE. 'spaceType': 'SPACE', # The user-visible name of the space 'displayName': 'API-setup' }, # The users and groups to add to the space. # # The authenticated user is automatically added to the space, # and doesn't need to be specified in the memberships array. 'memberships': [ { 'member': { 'name':'users/123456789', 'type': 'HUMAN' } }, { 'member': { 'name':'users/987654321', 'type': 'HUMAN' } }, { 'groupMember': { 'name': 'groups/11223344' } } ] } ).execute() # Prints details about the created space. print(result) if __name__ == '__main__': main()
在您的工作目录中,构建并运行该示例:
python3 chat_space_setup.py
Node.js
- 在您的工作目录中,创建一个名为
setup-space.js
的文件。 在
setup-space.js
中添加以下代码:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Sets up a new Chat space with users. * @return {!Promise<!Object>} */ async function setupSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces.create', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.setup({ requestBody: { space: { spaceType: 'SPACE', displayName: 'API-made', }, memberships: [ {member: {name: 'users/123456789', type: 'HUMAN'}}, {member: {name: 'users/987654321', type: 'HUMAN'}}, {groupMember: {name: 'groups/11223344'}}, ] } }); } setupSpace().then(console.log);
在您的工作目录中,运行该示例:
node setup-space.js
已设置一个命名的聊天室,其中包含一个群组和三个真人用户(包括通过身份验证的用户)。
如需前往聊天室,请使用聊天室的资源 ID 构建聊天室的网址。
您可以从 Google Chat 响应正文中的聊天室 name
获取资源 ID。例如,如果聊天室的 name
为 spaces/1234567
,则可以使用以下网址前往聊天室:https://mail.google.com/chat/u/0/#chat/space/1234567
。
相关主题
- 创建聊天室。
- 获取聊天室的详细信息。
- 列出聊天室。
- 更新聊天室。
- 删除聊天室。
- 查找私信聊天室。
- 将聊天室设为可供特定用户搜索。