更新聊天室

本指南介绍了如何对 Google Chat API 的 Space 资源使用 patch 方法来更新聊天室。您可以更新聊天室,以更改聊天室的相关属性,例如用户可见的显示名称、说明和指南。

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

  • 私信 (DM) 是两位用户或一位用户与一个 Chat 应用之间的对话。
  • 群聊是指三个或更多用户与 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 授权范围进行用户身份验证

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 授权范围进行用户身份验证

更新聊天室

如需更新现有 Google Chat 聊天室,请在请求中传递以下内容:

  • 指定 chat.spaces 授权范围。
  • Space 资源调用 patch 方法,并传递要更新的空间的 name,以及指定更新后的空间属性的 updateMaskbody
  • updateMask 指定要更新的空间的各个方面,其中包括以下内容:
    • displayName:更新 Google Chat 界面中显示的聊天室的用户可读名称。仅支持更改具有 SPACE 类型的聊天室的显示名称,或者同时包含 spaceType 掩码以将 GROUP_CHAT 空间类型更改为 SPACE 时。尝试更新 GROUP_CHATDIRECT_MESSAGE 空格的显示名称会导致参数无效错误。
    • spaceType:更新聊天室类型,但仅支持将 GROUP_CHAT 聊天室类型更改为 SPACE。在更新掩码中添加 displayNamespaceType,并确保指定空间的 displayNameSPACE 空间类型不为空。如果现有空间已有 SPACE 类型(包括 spaceType 掩码和更新显示名称时指定空间中的 SPACE 类型),则此项操作是可选的。尝试以其他方式更新空格类型会导致参数无效错误。
    • spaceDetails:聊天室的详细信息,包括说明和规则。
    • spaceHistoryState:如果组织允许用户更改其聊天记录设置,则支持为聊天室开启或关闭聊天记录功能。它与所有其他字段路径相互排斥。

更新现有聊天室的 spaceDetails 的方法如下:

Python

  1. 在您的工作目录中,创建一个名为 chat_space_update.py 的文件。
  2. chat_space_update.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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates the specified space description and guidelines.
        '''
    
        # 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().patch(
    
          # The space to update, and the updated space details.
          #
          # Replace {space} with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          name='spaces/SPACE',
          updateMask='spaceDetails',
          body={
    
            'spaceDetails': {
              'description': 'This description was updated with Chat API!',
              'guidelines': 'These guidelines were updated with Chat API!'
            }
    
          }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,将 SPACE 替换为聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或聊天室网址获取该名称。

  4. 在您的工作目录中,构建并运行示例:

    python3 chat_space_update.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a Chat space with the description and guidelines.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.patch({
        name: 'spaces/SPACE',
        updateMask: 'spaceDetails',
        requestBody: {
          spaceDetails: {
            description: 'This description was updated with Chat API!',
            guidelines: 'These guidelines were updated with Chat API!'
          },
        }
      });
    }
    
    updateSpace().then(console.log);
    
  3. 在代码中,将 SPACE 替换为聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或聊天室网址获取该名称。

  4. 在您的工作目录中,运行示例:

    node update-space.js
    

Google Chat API 会返回 Space 资源的一个实例来反映更新。