ดูรายละเอียดเกี่ยวกับสถานะการอ่านพื้นที่ทำงานของผู้ใช้

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

แหล่งข้อมูล SpaceReadState รายการ คือทรัพยากรแบบ Singleton ที่แสดงรายละเอียดเกี่ยวกับ ข้อความที่อ่านล่าสุดของผู้ใช้ที่ระบุในพื้นที่ใน 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.users.readstate หรือ chat.users.readstate.readonly ขอบเขตการให้สิทธิ์
  • เรียกใช้ getSpaceReadState วิธี ใน แหล่งข้อมูล SpaceReadState
  • ส่ง name ของสถานะการอ่านพื้นที่ทำงานเพื่อรับ ซึ่งประกอบด้วยรหัสผู้ใช้หรือ อีเมลแทนและรหัสพื้นที่ทำงาน การรับสถานะการอ่านพื้นที่ทำงานรองรับเฉพาะการรับการอ่าน สถานะของผู้ใช้ที่โทร ซึ่งสามารถระบุได้โดยการตั้งค่าหนึ่ง ดังต่อไปนี้:
    • ชื่อแทน me ตัวอย่างเช่น users/me/spaces/SPACE/spaceReadState
    • อีเมล Workspace ของผู้ใช้ที่โทร ตัวอย่างเช่น users/user@example.com/spaces/SPACE/spaceReadState
    • รหัสผู้ใช้ของผู้ใช้ที่โทร ตัวอย่างเช่น users/USER/spaces/SPACE/spaceReadState

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

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_spaceReadState_get.py
  2. รวมรหัสต่อไปนี้ใน chat_spaceReadState_get.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.users.readstate.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets the space read state for the calling 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.users().spaces().getSpaceReadState(
    
            # The space read state to get.
            #
            # Replace USER with the calling user's ID, Workspace email,
            # or the alias me.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            name='users/me/spaces/SPACE/spaceReadState'
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่

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

    python3 chat_spaceReadState_get.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then gets the space read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function getSpaceReadState() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.users.readstate.readonly',
      ];
    
      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.users.spaces.getSpaceReadState({
    
        /**
        * The space read state to get.
        *
        * Replace USER with the calling user's ID, Workspace email,
        * or the alias me.
        *
        * Replace SPACE with a space name.
        * Obtain the space name from the spaces resource of Chat API,
        * or from a space's URL.
        */
        name: 'users/me/spaces/SPACE/spaceReadState'
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getSpaceReadState().then(console.log);
    
  3. ในโค้ด ให้แทนที่

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

    node chat_spaceReadState_get.js
    

Apps Script

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

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

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

    /**
    * Authenticates with Chat API via user credentials,
    * then gets the space read state for the calling user.
    * @param {string} spaceReadStateName The resource name of the space read state.
    */
    function getSpaceReadState(spaceReadStateName) {
      try {
        Chat.Users.Spaces.getSpaceReadState(spaceReadStateName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to get read state with error %s', err.message);
      }
    }
    

Google Chat API จะได้รับสถานะการอ่านและการส่งคืนพื้นที่ทำงานที่ระบุ ตัวอย่าง แหล่งข้อมูล SpaceReadState