แสดงรายการพื้นที่ทำงาน

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

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

  • ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คนหรือผู้ใช้กับแอป Chat
  • การแชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปกับแอป Chat
  • พื้นที่ทำงานที่มีชื่อจะเป็นพื้นที่ทำงานถาวรที่ผู้คนส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน

การแสดงพื้นที่ทำงานที่มีการตรวจสอบสิทธิ์แอปจะแสดงรายการพื้นที่ทำงานที่แอป Chat มีสิทธิ์เข้าถึง การแสดงพื้นที่ทำงานที่มีการตรวจสอบสิทธิ์ผู้ใช้จะแสดงรายการพื้นที่ทำงานที่ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์มีสิทธิ์เข้าถึง

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

Python

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

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

Node.js

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

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

แสดงรายการพื้นที่ทำงานที่มีการตรวจสอบสิทธิ์ผู้ใช้

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

ตัวอย่างต่อไปนี้จะแสดงพื้นที่ทำงานที่มีชื่อและแชทเป็นกลุ่ม (แต่ไม่ใช่ข้อความส่วนตัวที่ถูกกรองออก) ที่ปรากฏแก่ผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์แล้ว

Python

  1. สร้างไฟล์ชื่อ chat_space_list.py ในไดเรกทอรีการทำงาน
  2. ใส่รหัสต่อไปนี้ใน chat_space_list.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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists named spaces and group chats (but not direct messages)
        visible to the authenticated user.
        '''
    
        # 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().list(
    
              # An optional filter that returns named spaces or unnamed group chats,
              # but not direct messages (DMs).
              filter='spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
    
          ).execute()
    
        # Prints the returned list of spaces.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    python3 chat_space_list.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * List Chat spaces.
    * @return {!Promise<!Object>}
    */
    async function listSpaces() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.list({
        filter: 'spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
      });
    }
    
    listSpaces().then(console.log);
    
  3. เรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    node list-spaces.js
    

Chat API จะแสดงผลอาร์เรย์ที่มีเลขหน้าของพื้นที่ทำงานที่มีชื่อและแชทเป็นกลุ่ม

แสดงรายการพื้นที่ทำงานที่มีการตรวจสอบสิทธิ์แอป

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

ตัวอย่างต่อไปนี้จะแสดงพื้นที่ทำงานที่มีชื่อและแชทเป็นกลุ่ม (แต่ไม่ใช่ข้อความส่วนตัว) ที่แอป Chat แสดงได้

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Use the service endpoint to call Chat API.
    result = chat.spaces().list(
    
            # An optional filter that returns named spaces or unnamed
            # group chats, but not direct messages (DMs).
            filter='spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
    
        ).execute()
    
    print(result)
    
  3. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    python3 chat_space_list_app.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    
    /**
    * List Chat spaces.
    * @return {!Promise<!Object>}
    */
    async function listSpaces() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.list({
        filter: 'spaceType = "SPACE" OR spaceType = "GROUP_CHAT"'
      });
    }
    
    listSpaces().then(console.log);
    
  3. เรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    node app-list-spaces.js
    

Chat API จะแสดงผลอาร์เรย์ของพื้นที่ทำงานที่มีเลขหน้ากำกับ

ปรับแต่งการใส่เลขหน้าหรือกรองรายการ

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

  • pageSize: จำนวนสูงสุดของพื้นที่ว่างที่จะแสดงผล บริการอาจแสดงผลน้อยกว่าค่านี้ หากไม่ระบุ ระบบจะแสดงผลเว้นวรรคไม่เกิน 100 รายการ ค่าสูงสุดคือ 1,000 และค่าที่มากกว่า 1,000 จะเปลี่ยนเป็น 1,000 โดยอัตโนมัติ
  • pageToken: โทเค็นหน้าเว็บที่ได้รับจากการเรียกใช้พื้นที่รายการก่อนหน้า ระบุโทเค็นนี้เพื่อเรียกหน้าถัดไป เวลาใส่เลขหน้า ค่าตัวกรองควรตรงกับการเรียกที่ให้โทเค็นของหน้าเว็บ การส่งต่อค่าที่ต่างกันอาจทําให้เกิดผลลัพธ์ที่ไม่คาดคิด
  • filter: ตัวกรองการค้นหา โปรดดูรายละเอียดการค้นหาที่รองรับในเมธอด spaces.list