Cập nhật gói thành viên của người dùng trong phòng Google Chat

Hướng dẫn này giải thích cách sử dụng phương thức patch trên tài nguyên membership của API Google Chat để thay đổi các thuộc tính về gói thành viên, chẳng hạn như thay đổi thành viên không gian thành người quản lý không gian hoặc thay đổi người quản lý không gian thành thành viên không gian.

Chiến lược phát hành đĩa đơn Tài nguyên Membership cho biết liệu người dùng thực hoặc ứng dụng Google Chat có được mời tham gia hay không, một phần hoặc không có trong không gian.

Điều kiện tiên quyết

Python

  • Python 3.6 trở lên
  • Công cụ quản lý gói pip
  • Thư viện ứng dụng mới nhất của Google. Cách cài đặt hoặc cập nhật các tính năng này: chạy lệnh sau trong giao diện dòng lệnh:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 trở lên
  • npm công cụ quản lý gói
  • Thư viện ứng dụng mới nhất của Google. Cách cài đặt hoặc cập nhật các tính năng này: chạy lệnh sau trong giao diện dòng lệnh:
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

Cập nhật gói thành viên

Để cập nhật tư cách thành viên của không gian, hãy truyền những thông tin sau vào yêu cầu:

  • Chỉ định phạm vi uỷ quyền chat.memberships.
  • Gọi Phương thức patch trên tài nguyên Membership, và chuyển name của tư cách thành viên để cập nhật, cũng như updateMaskbody chỉ định các thuộc tính mới cập nhật của gói thành viên.
  • updateMask chỉ định các chương trình thành viên cần cập nhật, và bao gồm:
    • role: Vai trò của người dùng trong phòng Chat, vai trò này xác định những quyền mà họ được phép hành động trong không gian. Các giá trị có thể có là:
      • ROLE_MEMBER: Một thành viên của không gian. Người dùng có các quyền cơ bản, chẳng hạn như gửi tin nhắn đến không gian. Trong nhóm 1:1 và chưa đặt tên các cuộc trò chuyện, mọi người đều có vai trò này.
      • ROLE_MANAGER: Người quản lý không gian. Người dùng có tất cả các quyền cơ bản cộng thêm quyền quản trị cho phép họ quản lý không gian, chẳng hạn như thêm hoặc xoá thành viên. Chỉ được hỗ trợ trong những không gian có spaceTypeSPACE (không gian được đặt tên).

Chỉ định một thành viên bình thường của không gian làm người quản lý không gian

Ví dụ sau đây đưa một thành viên thông thường vào không gian trở thành người quản lý không gian bằng cách chỉ định roleROLE_MANAGER trong body để chỉ định gói thành viên đã cập nhật thuộc tính:

Python

  1. Trong thư mục đang làm việc, hãy tạo một tệp có tên chat_membership_update.py.
  2. Đưa mã sau vào chat_membership_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.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MANAGER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. Trong mã, thay thế các nội dung sau:

  4. Trong thư mục đang làm việc, hãy tạo và chạy mẫu:

    python3 chat_membership_update.py
    

Node.js

  1. Trong thư mục đang làm việc, hãy tạo một tệp có tên chat_membership_update.js.
  2. Đưa mã sau vào chat_membership_update.js:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a membership in a Chat space to change it from
    * a space member to a space manager.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.spaces.patch({
    
        /**
        * The membership to update, and the updated role.
        *
        * Replace SPACE with a space name.
        * Obtain the space name from the spaces resource of Chat API,
        * or from a space's URL.
        *
        * Replace MEMBERSHIP with a membership name.
        * Obtain the membership name from the membership of Chat API.
        */
        name: 'spaces/SPACE/members/MEMBERSHIP',
        updateMask: 'role',
        requestBody: {
          role: 'ROLE_MANAGER'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    updateSpace().then(console.log);
    
  3. Trong mã, thay thế các nội dung sau:

  4. Trong thư mục đang làm việc, hãy tạo và chạy mẫu:

    python3 chat_membership_update.js
    

Apps Script

Ví dụ này gọi API Chat bằng cách sử dụng Dịch vụ trò chuyện nâng cao.

  1. Thêm phạm vi uỷ quyền chat.memberships vào Tệp appsscript.json của dự án Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. Thêm một hàm như hàm này vào dự án Apps Script mã:

    /**
     * Updates a membership from space member to space manager.
     * @param {string} memberName The resource name of the membership.
    */
    function updateMembershipToSpaceManager(memberName) {
      try {
        const body = {'role': 'ROLE_MANAGER'};
        Chat.Spaces.Members.patch(memberName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }
    

API Google Chat thay đổi thành viên đã chỉ định thành người quản lý không gian rồi trả về một bản sao của Membership nêu chi tiết thay đổi.

Chỉ định người quản lý không gian làm thành viên thường xuyên

Ví dụ sau đây đưa người quản lý không gian trở thành thành viên thông thường của không gian bằng cách chỉ định roleROLE_MEMBER trong body để chỉ định gói thành viên đã cập nhật thuộc tính:

Python

  1. Trong thư mục đang làm việc, hãy tạo một tệp có tên chat_membership_update.py.
  2. Đưa mã sau vào chat_membership_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.memberships"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MEMBER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. Trong mã, thay thế các nội dung sau:

  4. Trong thư mục đang làm việc, hãy tạo và chạy mẫu:

    python3 chat_membership_update.py
    

Node.js

  1. Trong thư mục đang làm việc, hãy tạo một tệp có tên chat_membership_update.js.
  2. Đưa mã sau vào chat_membership_update.js:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a membership in a Chat space to change it from
    * a space manager to a space member.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.spaces.patch({
    
        /**
        * The membership to update, and the updated role.
        *
        * Replace SPACE with a space name.
        * Obtain the space name from the spaces resource of Chat API,
        * or from a space's URL.
        *
        * Replace MEMBERSHIP with a membership name.
        * Obtain the membership name from the membership of Chat API.
        */
        name: 'spaces/SPACE/members/MEMBERSHIP',
        updateMask: 'role',
        requestBody: {
          role: 'ROLE_MEMBER'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    updateSpace().then(console.log);
    
  3. Trong mã, thay thế các nội dung sau:

  4. Trong thư mục đang làm việc, hãy tạo và chạy mẫu:

    python3 chat_membership_update.js
    

Apps Script

Ví dụ này gọi API Chat bằng cách sử dụng Dịch vụ trò chuyện nâng cao.

  1. Thêm phạm vi uỷ quyền chat.memberships vào Tệp appsscript.json của dự án Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. Thêm một hàm như hàm này vào dự án Apps Script mã:

    /**
     * Updates a membership from space manager to space member.
     * @param {string} memberName The resource name of the membership.
    */
    function updateMembershipToSpaceMember(memberName) {
      try {
        const body = {'role': 'ROLE_MEMBER'};
        Chat.Spaces.Members.patch(memberName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }
    

API Google Chat thay đổi thành viên đã chỉ định thành người quản lý không gian rồi trả về một bản sao của Membership nêu chi tiết thay đổi.