ดูรายละเอียดเกี่ยวกับสถานะการอ่านชุดข้อความของผู้ใช้

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

ทรัพยากร ThreadReadState คือทรัพยากร Singleton ที่แสดงรายละเอียดเกี่ยวกับข้อความที่อ่านล่าสุดของผู้ใช้ที่ระบุในชุดข้อความของ 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.users.readstate หรือ chat.users.readstate.readonly

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.users.readstate หรือ chat.users.readstate.readonly

Apps Script

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

รับสถานะการอ่านชุดข้อความของผู้ใช้ที่เรียกใช้

หากต้องการดูรายละเอียดเกี่ยวกับสถานะการอ่านของผู้ใช้ในชุดข้อความ ให้ใส่ข้อมูลต่อไปนี้ในคำขอ

  • ระบุขอบเขตการให้สิทธิ์ chat.users.readstate หรือ chat.users.readstate.readonly
  • เรียกเมธอด getThreadReadState ในทรัพยากร ThreadReadState
  • ส่ง name ของสถานะการอ่านชุดข้อความที่จะรับ ซึ่งรวมถึงรหัสผู้ใช้หรือชื่อแทน และรหัสพื้นที่ทำงาน การรับสถานะการอ่านของชุดข้อความจะรองรับเฉพาะการรับสถานะการอ่านของผู้ใช้ที่เรียกใช้เท่านั้น ซึ่งสามารถระบุได้โดยการตั้งค่าอย่างใดอย่างหนึ่งต่อไปนี้
    • ชื่อแทน me ตัวอย่างเช่น users/me/spaces/SPACE/threads/THREAD/threadReadState
    • อีเมล Workspace ของผู้ใช้ที่โทร ตัวอย่างเช่น users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
    • รหัสผู้ใช้ของผู้ใช้ที่โทร ตัวอย่างเช่น users/USER/spaces/SPACE/threads/THREAD/threadReadState

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

Python

  1. สร้างไฟล์ชื่อ chat_threadReadState_get.py ในไดเรกทอรีการทำงาน
  2. ใส่รหัสต่อไปนี้ใน chat_threadReadState_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 thread 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().threads().getThreadReadState(
    
            # The thread 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.
            #
            # Replace THREAD with a thread name.
            # Obtain the thread name from the messages resource of Chat API.
            name='users/me/spaces/SPACE/threads/THREAD/threadReadState'
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ส่วนต่อไปนี้

    • SPACE: ชื่อพื้นที่ทำงาน ซึ่งคุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • THREAD: ชื่อชุดข้อความ ซึ่งดูได้จากเมธอด spaces.messages.get ใน Chat API

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

    python3 chat_threadReadState_get.py
    

Node.js

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

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then gets the thread read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function getThreadReadState() {
    
      /**
      * 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.threads.getThreadReadState({
    
        /**
        * The thread 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/threads/THREADS/threadReadState'
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getThreadReadState().then(console.log);
    
  3. ในโค้ด ให้แทนที่ส่วนต่อไปนี้

    • SPACE: ชื่อพื้นที่ทำงาน ซึ่งคุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

    • THREAD: ชื่อชุดข้อความ ซึ่งดูได้จากเมธอด spaces.messages.get ใน Chat API

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

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

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

Google Chat API จะได้รับสถานะการอ่านชุดข้อความที่ระบุและแสดงผลอินสแตนซ์ของทรัพยากร ThreadReadState