อัปเดตการเป็นสมาชิกของผู้ใช้ในพื้นที่ของ Google Chat

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด patch ในแหล่งข้อมูล membership Google Chat API เพื่อเปลี่ยนแอตทริบิวต์เกี่ยวกับการเป็นสมาชิก เช่น การเปลี่ยน สมาชิกพื้นที่ทำงานเป็นผู้จัดการพื้นที่ทำงาน หรือเปลี่ยนผู้จัดการพื้นที่ทำงานเป็นสมาชิกพื้นที่ทำงาน

แหล่งข้อมูล Membership รายการ จะแสดงว่ามีการเชิญผู้ใช้ที่เป็นมนุษย์หรือแอป Google Chat หรือไม่ เพียงบางส่วน หรือไม่ปรากฏในพื้นที่ทำงาน

ข้อกำหนดเบื้องต้น

Python

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Python 3.6 ขึ้นไป
  • เครื่องมือจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Node.js 14 ขึ้นไป
  • npm เครื่องมือจัดการแพ็กเกจ
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat

อัปเดตการเป็นสมาชิก

หากต้องการอัปเดตการเป็นสมาชิกพื้นที่ทำงาน ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • ระบุขอบเขตการให้สิทธิ์ chat.memberships
  • เรียกใช้ patch วิธี ในแหล่งข้อมูล Membership และส่งการเป็นสมาชิก name เพื่ออัปเดต รวมถึงupdateMask และ body ที่ระบุแอตทริบิวต์การเป็นสมาชิกที่อัปเดตแล้ว
  • updateMask จะระบุแง่มุมต่างๆ ของการเป็นสมาชิกที่ต้องอัปเดต ได้แก่
    • role: บทบาทของผู้ใช้ภายในพื้นที่ใน Chat ซึ่งจะเป็นตัวกำหนดที่ได้รับอนุญาต ในพื้นที่ทำงาน โดยค่าที่เป็นไปได้มีดังนี้
      • ROLE_MEMBER: สมาชิกของพื้นที่ทำงาน ผู้ใช้มีสิทธิ์พื้นฐาน เช่น การส่งข้อความถึงพื้นที่ทำงาน ในกลุ่มแบบ 1:1 และที่ไม่มีชื่อ การสนทนา ทุกคนมีบทบาทนี้
      • ROLE_MANAGER: ผู้จัดการพื้นที่ทำงาน ผู้ใช้มีสิทธิ์พื้นฐานทั้งหมด รวมถึง สิทธิ์ระดับผู้ดูแลระบบที่อนุญาตให้จัดการพื้นที่ทำงานได้ เช่น การเพิ่มหรือ การนำสมาชิกออก รองรับเฉพาะในพื้นที่ทำงานที่ spaceType เท่ากับ SPACE (เว้นวรรคที่ตั้งชื่อ)

กำหนดให้สมาชิกพื้นที่ทำงานปกติเป็นผู้จัดการพื้นที่ทำงาน

ตัวอย่างต่อไปนี้ทำให้สมาชิกพื้นที่ทำงานปกติเป็นผู้จัดการพื้นที่ทำงานโดยการระบุ role เป็น ROLE_MANAGER ใน body ที่ระบุการเป็นสมาชิกที่อัปเดตแล้ว ดังนี้

Python

  1. สร้างไฟล์ชื่อ chat_membership_update.py ในไดเรกทอรีการทำงาน
  2. รวมรหัสต่อไปนี้ใน 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. ในโค้ด ให้แทนที่

    • SPACE: ชื่อพื้นที่ทำงาน ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBERSHIP: ชื่อการเป็นสมาชิก ที่คุณจะได้รับจาก spaces.members.list วิธี ใน Chat API

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_membership_update.py
    

Node.js

  1. สร้างไฟล์ชื่อ chat_membership_update.js ในไดเรกทอรีการทำงาน
  2. รวมรหัสต่อไปนี้ใน 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. ในโค้ด ให้แทนที่

    • SPACE: ชื่อพื้นที่ทำงาน ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBERSHIP: ชื่อการเป็นสมาชิก ที่คุณจะได้รับจาก spaces.members.list วิธี ใน Chat API

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_membership_update.js
    

Apps Script

ตัวอย่างนี้เรียกใช้ Chat API โดยใช้ บริการ Chat ขั้นสูง

  1. เพิ่มขอบเขตการให้สิทธิ์ chat.memberships ไปยัง ไฟล์ appsscript.json ของโครงการ Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. เพิ่มฟังก์ชันเช่นนี้ลงใน รหัส:

    /**
     * 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);
      }
    }
    

Google Chat API เปลี่ยนการเป็นสมาชิกที่ระบุเป็นผู้จัดการพื้นที่ทำงานและการคืนสินค้า อินสแตนซ์ของ Membership การอธิบายการเปลี่ยนแปลง

กำหนดให้ผู้จัดการพื้นที่ทำงานเป็นสมาชิกทั่วไป

ตัวอย่างต่อไปนี้ทำให้ผู้จัดการพื้นที่ทำงานเป็นสมาชิกพื้นที่ทำงานทั่วไปโดยระบุ role เป็น ROLE_MEMBER ใน body ที่ระบุการเป็นสมาชิกที่อัปเดตแล้ว ดังนี้

Python

  1. สร้างไฟล์ชื่อ chat_membership_update.py ในไดเรกทอรีการทำงาน
  2. รวมรหัสต่อไปนี้ใน 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. ในโค้ด ให้แทนที่

    • SPACE: ชื่อพื้นที่ทำงาน ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBERSHIP: ชื่อการเป็นสมาชิก ที่คุณจะได้รับจาก spaces.members.list วิธี ใน Chat API

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_membership_update.py
    

Node.js

  1. สร้างไฟล์ชื่อ chat_membership_update.js ในไดเรกทอรีการทำงาน
  2. รวมรหัสต่อไปนี้ใน 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. ในโค้ด ให้แทนที่

    • SPACE: ชื่อพื้นที่ทำงาน ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • MEMBERSHIP: ชื่อการเป็นสมาชิก ที่คุณจะได้รับจาก spaces.members.list วิธี ใน Chat API

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_membership_update.js
    

Apps Script

ตัวอย่างนี้เรียกใช้ Chat API โดยใช้ บริการ Chat ขั้นสูง

  1. เพิ่มขอบเขตการให้สิทธิ์ chat.memberships ไปยัง ไฟล์ appsscript.json ของโครงการ Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. เพิ่มฟังก์ชันเช่นนี้ลงใน รหัส:

    /**
     * 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);
      }
    }
    

Google Chat API เปลี่ยนการเป็นสมาชิกที่ระบุเป็นผู้จัดการพื้นที่ทำงานและการคืนสินค้า อินสแตนซ์ของ Membership ระบุรายละเอียดการเปลี่ยนแปลง