ลบพื้นที่ทำงาน

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด delete ในทรัพยากร Space ของ Google Chat API เพื่อลบพื้นที่ทำงานที่ระบุชื่อเมื่อไม่จำเป็นต้องใช้อีกต่อไป การลบพื้นที่ทำงานจะลบทุกอย่างที่อยู่ในพื้นที่ทำงานด้วย รวมถึงข้อความและไฟล์แนบ

แหล่งข้อมูล Space เป็นพื้นที่ที่ผู้คนและแอป Chat ใช้ในการส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน พื้นที่ทำงานมีหลายประเภทดังนี้

  • ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คนหรือผู้ใช้กับแอป Chat
  • การแชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปกับแอป 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.delete จากผู้ใช้ที่มีสิทธิ์ลบพื้นที่ทำงานที่ระบุ

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.delete จากผู้ใช้ที่มีสิทธิ์ลบพื้นที่ทำงานที่ระบุ

ลบพื้นที่ทำงานที่ตั้งชื่อ

หากต้องการลบพื้นที่ทำงานที่มีอยู่ใน Google Chat ให้ส่งข้อมูลต่อไปนี้ในคำขอ

วิธีลบพื้นที่ทำงาน

Python

  1. สร้างไฟล์ชื่อ chat_space_delete.py ในไดเรกทอรีการทำงาน
  2. ใส่รหัสต่อไปนี้ใน chat_space_delete.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.delete"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes the specified 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().delete(
    
              # The space to delete.
              #
              # 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'
    
          ).execute()
    
        # Print Chat API's response in your command line interface.
        # When deleting a space, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่งคุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

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

    python3 chat_space_delete.py
    

Node.js

  1. สร้างไฟล์ชื่อ delete-space.js ในไดเรกทอรีการทำงาน
  2. ใส่รหัสต่อไปนี้ใน delete-space.js:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Deletes a Chat space.
    * @return {!Promise<!Object>}
    */
    async function deleteSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.delete',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.delete({name: 'spaces/SPACE'});
    }
    
    deleteSpace().then(console.log);
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. เรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    node delete-space.js
    

    หากทำสำเร็จ เนื้อหาการตอบกลับจะว่างเปล่า ซึ่งระบุว่าพื้นที่ทำงานถูกลบแล้ว