设置包含初始成员的聊天室

本指南介绍了如何在setupSpace 使用 Google Chat API 设置 Google Chat 聊天室。设置聊天室会创建一个聊天室 并向其中添加指定用户

通过 Space 资源 代表用户和 Chat 扩展应用能够发送消息的位置, 共享文件和协作聊天室分为以下几种类型:

  • 私信 (DM) 是指两位用户或一位用户与 Chat 应用。
  • 群聊是三位或更多用户之间的对话, 聊天应用。
  • 已命名的聊天室是用户永久保留的位置,可供用户发送消息、共享文件 和协作。

设置聊天室时,请考虑以下事项:

  • 系统会自动将发起调用(经过身份验证)的用户添加到聊天室,因此您可以 就无需在请求中指定用户的成员资格。
  • 创建私信 (DM) 时,如果两位用户之间存在私信, 系统会返回私信否则,系统会创建私信。
  • 创建群聊时,如果请求中未提供任何成员资格 已成功添加到群聊中(例如,权限问题),则 系统可能会创建空的群聊(仅包括发起通话的用户)。
  • 您无法设置包含“话题式回复”的聊天室,也无法添加您 Google Workspace。
  • 请求中提供的重复成员资格(包括发起调用的用户) 而不是导致请求错误

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 最新的 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 或更高版本
  • npm 软件包管理工具
  • 最新的 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
    npm install @google-cloud/local-auth @googleapis/chat
    

设置聊天室

如需设置聊天室,请在请求中传递以下内容:

  • 指定 chat.spaces.createchat.spaces 授权范围。
  • 调用 setup 方法 针对 Space 资源
  • 如需将真人用户添加为聊天室成员,请指定 users/{user},其中 {user}{person_id} person 来自 People API 的 user 目录 API。例如,如果 People API 人员 “resourceName”的状态为 people/123456789,您可以通过以下方式将该用户添加到聊天室 包括以users/123456789member.name的会员资格。
  • 如需将群组添加为聊天室成员,请指定“groups/{group}”,其中 {group} 为 要为其创建成员资格的群组 ID。该组的 ID 可以 使用 Cloud Identity API 检索。 例如,如果 Cloud Identity API 返回名为 groups/123456789 的组,然后设置 从membership.groupMember.name改为groups/123456789。无法 添加到群聊或私信中,但仅添加到已命名的聊天室。
  • 为了在发起通话的用户和另一个人之间创建私信 user,请在请求中指定该真人用户的成员资格。
  • 如需在发起通话的用户和发起通话的应用之间创建私信,请将 Space.singleUserBotDmtrue,并且不指定任何成员资格。您可以 只使用此方法为发起调用的应用设置 DM。添加通话对象 作为聊天室成员或两位真人用户之间的现有私信,请参阅 创建成员资格

以下示例会创建一个已命名的聊天室,并为该聊天室创建成员 一个群组和三个真人用户(包括经过身份验证的用户和两个 其他指定用户)。

Python

  1. 在您的工作目录中,创建一个名为 chat_space_setup.py 的文件。
  2. 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()
    
  3. 在您的工作目录中,构建并运行该示例:

    python3 chat_space_setup.py
    

Node.js

  1. 在您的工作目录中,创建一个名为 setup-space.js 的文件。
  2. 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);
    
  3. 在您的工作目录中,运行该示例:

    node setup-space.js
    

一个已命名的 Chat 聊天室,包含一个群组和三个真人用户,其中包括 已设置完毕

如需前往聊天室,请使用聊天室的资源 ID 构建聊天室的网址。 你可以在 Google Chat 回复中从聊天室“name”获取资源 ID 正文。例如,如果聊天室的namespaces/1234567,你可以 发布到聊天室,网址如下: https://mail.google.com/chat/u/0/#chat/space/1234567