创建聊天室

本指南介绍了如何在 Google Chat API 的 Space 资源上使用 create 方法创建命名聊天室。

Space 资源代表用户和 Chat 应用可以发送消息、共享文件以及开展协作的地方。聊天室分为以下几种类型:

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

命名聊天室是供用户发送消息、共享文件和开展协作的地方。命名的聊天室可以包含 Chat 应用。已命名聊天室包括未命名的群组对话和私信所不具备的额外功能,例如聊天室管理员可以应用管理设置、说明,以及添加或移除人员和应用。创建命名空间后,该空间的唯一成员是经过身份验证的用户。该聊天室不包含其他人或应用,甚至不包含创建该聊天室的 Chat 应用。如需添加人员,请对 Member 资源调用 create 方法,在聊天室中创建成员。如需了解具体方法,请参阅创建会员资格

如需创建包含多个成员的已命名聊天室(三人或多人之间的未命名群聊、两人之间的私信对话,或一位用户与调用 Chat API 的 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.spaces.createchat.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.createchat.spaces 授权范围的用户身份验证

创建命名聊天室

如需创建命名空间,请在请求中传递以下内容:

  • 指定 chat.spaces.createchat.spaces 授权范围。
  • Space 资源调用 create 方法
  • spaceType 设置为 SPACE
  • displayName 设置为空间的用户可见名称。在以下示例中,displayName 设置为 API-made
  • (可选)设置其他聊天室属性,例如 spaceDetails(用户可见的说明和聊天室指南集)。

如要创建命名空间,请按以下步骤操作:

Python

  1. 在您的工作目录中,创建一个名为 chat_space_create_named.py 的文件。
  2. chat_space_create_named.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 creates 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().create(
    
          # Details about the space to create.
          body = {
    
            # To create a named space, set spaceType to SPACE.
            'spaceType': 'SPACE',
    
            # The user-visible name of the space.
            'displayName': 'API-made'
          }
    
          ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在您的工作目录中,构建并运行示例:

    python3 chat_space_create_named.py
    

Node.js

  1. 在您的工作目录中,创建一个名为 create-space.js 的文件。
  2. create-space.js 中添加以下代码:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Creates a new chat space.
    * @return {!Promise<!Object>}
    */
    async function createSpace() {
      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.create(
          {requestBody: {spaceType: 'SPACE', displayName: 'API-made'}});
    }
    
    createSpace().then(console.log);
    
  3. 在您的工作目录中,运行示例:

    node create-space.js
    

系统会创建一个已命名的聊天室。如需前往聊天室,请使用聊天室的资源 ID 来构建聊天室网址。您可以在 Google Chat 响应正文中的聊天室 name 中找到资源 ID。例如,如果聊天室的 namespaces/1234567,您可以使用以下网址导航到聊天室:https://mail.google.com/chat/u/0/#chat/space/1234567