本指南介绍了如何使用 Google Chat API 的 membership
资源中的 create
方法邀请用户、Google 网上论坛群组或 Chat 应用,或将其添加到聊天室(也称为“创建会员资格”)。创建成员资格时,如果指定成员关闭了自动接受政策,则他们会收到邀请,且必须接受聊天室邀请才能加入。否则,创建成员资格会将成员直接添加到指定的聊天室。
Membership
资源表示真人用户或 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.memberships
或chat.memberships.app
授权范围的用户身份验证。
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.memberships
或chat.memberships.app
授权范围的用户身份验证。
邀请用户或将用户添加到聊天室
如需邀请聊天室或将用户添加到聊天室,请在请求中传递以下内容:
- 指定
chat.memberships
授权范围。 - 对
membership
资源调用create
方法。 - 将
parent
设置为要在其中创建成员资格的聊天室的资源名称。 - 将
member
设置为users/{user}
,其中{user}
是您要为之创建成员资格的用户,可以是以下任意一种:
以下示例展示了如何将用户添加到聊天室:
Python
- 在您的工作目录中,创建一个名为
chat_membership_user_create.py
的文件。 在
chat_membership_user_create.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.memberships"] def main(): ''' Authenticates with Chat API via user credentials, then adds a user to a Chat space by creating a membership. ''' # 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().members().create( # The space in which to create a membership. parent = 'spaces/SPACE', # Specify which user the membership is for. body = { 'member': { 'name':'users/USER', 'type': 'HUMAN' } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
在代码中进行以下替换:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室的网址获取。USER
:用户 ID。
在您的工作目录中,构建并运行该示例:
python3 chat_membership_user_create.py
Node.js
- 在您的工作目录中,创建一个名为
add-user-to-space.js
的文件。 在
add-user-to-space.js
中添加以下代码:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the user to the Chat space. * @return {!Promise<!Object>} */ async function addUserToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {member: {name: 'users/USER', type: 'HUMAN'}} }); } addUserToSpace().then(console.log);
在代码中进行以下替换:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室的网址获取。USER
:用户 ID。
在您的工作目录中,运行该示例:
node add-user-to-space.js
Chat API 会返回一个 membership
实例,其中详细说明了创建的用户成员资格。
邀请 Google 群组或将群组添加到聊天室
如需邀请 Google 群组或向聊天室添加 Google 群组,请在请求中传递以下内容:
- 指定
chat.memberships
授权范围。 - 对
membership
资源调用create
方法。 - 将
parent
设置为要在其中创建成员资格的聊天室的资源名称。 - 将
groupMember
设置为groups/{group}
,其中{group}
是要为其创建成员资格的群组 ID。您可以使用 Cloud Identity API 检索群组的 ID。例如,如果 Cloud Identity API 返回名为groups/123456789
的群组,则将membership.groupMember.name
设置为groups/123456789
。
无法将 Google 群组添加到群聊或私信中,只能添加到命名的聊天室。以下示例展示了如何将群组添加到命名的聊天室:
Python
- 在您的工作目录中,创建一个名为
chat_membership_group_create.py
的文件。 在
chat_membership_group_create.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.memberships"] def main(): ''' Authenticates with Chat API via user credentials, then adds a group to a Chat space by creating a membership. ''' # 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().members().create( # The named space in which to create a membership. parent = 'spaces/SPACE', # Specify which group the membership is for. body = { 'groupMember': { 'name':'groups/GROUP', } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
在代码中进行以下替换:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室的网址获取。GROUP
:群组 ID。
在您的工作目录中,构建并运行该示例:
python3 chat_membership_group_create.py
Node.js
- 在您的工作目录中,创建一个名为
add-group-to-space.js
的文件。 在
add-group-to-space.js
中添加以下代码:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the group to the Chat space. * @return {!Promise<!Object>} */ async function addUserToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {groupMember: {name: 'groups/GROUP'}} }); } addUserToSpace().then(console.log);
在代码中进行以下替换:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室的网址获取。GROUP
:群组 ID。
在您的工作目录中,运行该示例:
node add-group-to-space.js
Chat API 会返回一个 membership
实例,其中详细说明了所创建的群组成员资格。
将 Chat 扩展应用添加到聊天室
Chat 应用无法将其他应用添加为聊天室的成员。如需将 Chat 应用添加到聊天室或两位真人之间的私信对话,请在请求中传递以下内容:
- 指定
chat.memberships.app
授权范围。 - 对
membership
资源调用create
方法。 - 将
parent
设置为要在其中创建成员资格的聊天室的资源名称。 - 将
member
设置为users/app
;这是一个别名,表示调用 Chat API 的应用。
以下示例展示了如何将 Chat 应用添加到聊天室:
Python
- 在您的工作目录中,创建一个名为
chat_membership_app_create.py
的文件。 在
chat_membership_app_create.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.memberships.app"] def main(): ''' Authenticates with Chat API via user credentials, then adds the Chat app to a Chat space. ''' # 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().members().create( # The space in which to create a membership. parent = 'spaces/SPACE', # Set the Chat app as the entity that gets added to the space. # 'app' is an alias for the Chat app calling the API. body = { 'member': { 'name':'users/app', 'type': 'BOT' } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
在代码中,将
SPACE
替换为聊天室名称,您可以从 Chat API 中的spaces.list
方法或聊天室的网址获取该名称。在您的工作目录中,构建并运行该示例:
python3 chat_membership_app_create.py
Node.js
- 在您的工作目录中,创建一个名为
add-app-to-space.js
的文件。 在
add-app-to-space.js
中添加以下代码:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the app to the Chat space. * @return {!Promise<!Object>} */ async function addAppToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships.app', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {member: {name: 'users/app', type: 'BOT'}} }); } addAppToSpace().then(console.log);
在代码中,将
SPACE
替换为聊天室名称,您可以从 Chat API 中的spaces.list
方法或聊天室的网址获取该名称。在您的工作目录中,运行该示例:
node add-app-to-space.js
Chat API 会返回一个 membership
实例,其中详细说明了已创建的应用成员资格。