อัปเดตพื้นที่ทํางาน

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

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

  • ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คนหรือผู้ใช้ 1 รายกับแอป 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.spaces

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.spaces

อัปเดตพื้นที่ทำงาน

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

  • ระบุขอบเขตการให้สิทธิ์ chat.spaces
  • เรียกใช้เมธอด patch ในทรัพยากร Space ในคำขอ คุณสามารถระบุช่อง name ของพื้นที่ทำงาน ช่อง updateMask ที่มีช่องให้อัปเดตอย่างน้อย 1 ช่อง และ body พร้อมข้อมูลพื้นที่ทำงานที่อัปเดตแล้ว

คุณสามารถอัปเดตสิ่งต่างๆ เช่น ชื่อที่แสดง ประเภทพื้นที่ทำงาน สถานะประวัติ และอื่นๆ หากต้องการดูช่องทั้งหมดที่อัปเดตได้ โปรดดูเอกสารอ้างอิง

ต่อไปนี้เป็นวิธีอัปเดตช่อง 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 updated space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่งคุณจะได้มาจากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  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 ด้วยชื่อพื้นที่ทำงาน ซึ่งคุณจะได้มาจากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

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

    node update-space.js
    

Google Chat API จะแสดงผลอินสแตนซ์ของทรัพยากร Space ที่แสดงการอัปเดต