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

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

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

Python

  • Python 3.6 ขึ้นไป
  • เครื่องมือการจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
  • การกำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การอัปเดตการเป็นสมาชิกจะต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์ chat.memberships หรือหากนำเข้าข้อมูลไปยัง Chat ก็จะต้องมีขอบเขตการให้สิทธิ์ chat.import

Node.js

  • Node.js และ npm
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Node.js หากต้องการติดตั้ง ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง

    npm install @google-cloud/local-auth @googleapis/chat
    
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
  • การกำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การอัปเดตการเป็นสมาชิกจะต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์ chat.memberships หรือหากนำเข้าข้อมูลไปยัง Chat ก็จะต้องมีขอบเขตการให้สิทธิ์ chat.import

Apps Script

  • บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้าง แอป Chat ให้ทำตาม quickstartนี้
  • การกำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การอัปเดตการเป็นสมาชิกจะต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์ chat.memberships หรือหากนำเข้าข้อมูลไปยัง Chat ก็จะต้องมีขอบเขตการให้สิทธิ์ chat.import

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

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

  • ระบุขอบเขตการให้สิทธิ์ 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. เพิ่มฟังก์ชันที่คล้ายกับฟังก์ชันนี้ในโค้ดของโปรเจ็กต์ Apps Script:

    /**
     * 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. เพิ่มฟังก์ชันที่คล้ายกับฟังก์ชันนี้ในโค้ดของโปรเจ็กต์ Apps Script:

    /**
     * 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 ที่มีรายละเอียดการเปลี่ยนแปลง