Xem thông tin chi tiết về trạng thái đọc chuỗi bài đăng của người dùng

Hướng dẫn này giải thích cách sử dụng phương thức getThreadReadState trên Tài nguyên ThreadReadState của API Google Chat để xem thông tin chi tiết về trạng thái đọc trong một chuỗi thư. Để biết trạng thái đã đọc của thư trong không gian, xem Xem thông tin chi tiết về trạng thái đọc không gian của người dùng.

Chiến lược phát hành đĩa đơn Tài nguyên ThreadReadState là một tài nguyên singleton biểu thị thông tin chi tiết về một tin nhắn đọc gần đây nhất của người dùng đã chỉ định trong chuỗi tin nhắn trên Google Chat.

Điều kiện tiên quyết

Python

  • Python 3.6 trở lên
  • Công cụ quản lý gói pip
  • Thư viện ứng dụng mới nhất của Google. Cách cài đặt hoặc cập nhật các tính năng này: chạy lệnh sau trong giao diện dòng lệnh:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • Node.js 14 trở lên
  • npm công cụ quản lý gói
  • Thư viện ứng dụng mới nhất của Google. Cách cài đặt hoặc cập nhật các tính năng này: chạy lệnh sau trong giao diện dòng lệnh:
    npm install @google-cloud/local-auth @googleapis/chat
    

Apps Script

Nhận trạng thái đọc chuỗi của người dùng đang gọi

Để biết thông tin chi tiết về trạng thái đọc của người dùng trong một chuỗi thư, hãy thêm sau đây trong yêu cầu của bạn:

  • Chỉ định chat.users.readstate hoặc chat.users.readstate.readonly phạm vi uỷ quyền.
  • Gọi Phương thức getThreadReadState trên ThreadReadState tài nguyên.
  • Truyền name của trạng thái đã đọc chuỗi để lấy, bao gồm cả mã nhận dạng người dùng hoặc email đại diện và mã không gian. Việc nhận trạng thái đã đọc của chuỗi chỉ hỗ trợ nhận trạng thái đã đọc trạng thái của người dùng gọi, có thể được chỉ định bằng cách đặt một trong sau:
    • Bí danh me. Ví dụ: users/me/spaces/SPACE/threads/THREAD/threadReadState.
    • Địa chỉ email Workspace của người dùng gọi. Ví dụ: users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState.
    • Mã nhận dạng người dùng của người dùng gọi. Ví dụ: users/USER/spaces/SPACE/threads/THREAD/threadReadState.

Ví dụ sau đây lấy trạng thái đọc luồng của người dùng đang gọi:

Python

  1. Trong thư mục đang làm việc, hãy tạo một tệp có tên chat_threadReadState_get.py.
  2. Đưa mã sau vào 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. Trong mã, thay thế các nội dung sau:

  4. Trong thư mục đang làm việc, hãy tạo và chạy mẫu:

    python3 chat_threadReadState_get.py
    

Node.js

  1. Trong thư mục đang làm việc, hãy tạo một tệp có tên chat_threadReadState_get.js.
  2. Đưa mã sau vào 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. Trong mã, thay thế các nội dung sau:

  4. Trong thư mục đang làm việc, hãy tạo và chạy mẫu:

    node chat_threadReadState_get.js
    

Apps Script

Ví dụ này gọi API Chat bằng cách sử dụng Dịch vụ trò chuyện nâng cao.

  1. Thêm phạm vi uỷ quyền chat.users.readstate.readonly vào Tệp appsscript.json của dự án Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. Thêm một hàm như hàm này vào dự án Apps Script mã:

    /**
    * 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);
      }
    }
    

API Google Chat nhận trạng thái đã đọc chuỗi đã chỉ định và trả về một bản sao của Tài nguyên ThreadReadState.