本指南介绍了如何对 Google Chat API 的 Space
资源使用 setup
方法,以便设置 Google Chat 聊天室。设置聊天室后,系统会创建一个聊天室,并向其中添加指定用户。
Space
资源代表用户和 Chat 应用可以发送消息、共享文件以及开展协作的地方。聊天室分为以下几种类型:
- 私信 (DM) 是两位用户或一位用户与一个 Chat 应用之间的对话。
- 群聊是指三个或更多用户与 Chat 应用之间的对话。
- 已命名聊天室是用户在其中发送消息、共享文件和开展协作的永久性位置。
设置聊天室时,请考虑以下事项:
- 发起调用(经过身份验证的)用户会自动添加到聊天室中,因此您无需在请求中指定用户的成员资格。
- 创建私信 (DM) 时,如果两个用户之间存在 DM,则会返回该 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
,将用户添加到聊天室。 - 如需在发起调用的用户与其他真人用户之间创建私信,请在请求中指定真人用户的成员资格。
- 如需在发起调用的用户和发起调用的应用之间创建私信,请将
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 people and app 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' } } ] } ).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'}}, ] } }); } 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
。