Mendapatkan detail tentang status pembacaan ruang pengguna

Panduan ini menjelaskan cara menggunakan metode getSpaceReadState pada resource SpaceReadState Google Chat API untuk mendapatkan detail tentang status baca pengguna dalam ruang. Untuk mendapatkan status baca pesan dalam rangkaian pesan, lihat Mendapatkan detail tentang status baca thread pengguna.

Resource SpaceReadState adalah resource singleton yang mewakili detail tentang pesan yang terakhir dibaca pengguna tertentu di ruang Google Chat.

Prasyarat

Python

  • Python 3.6 atau yang lebih baru
  • Alat pengelolaan paket pip
  • Library klien Google terbaru untuk Python. Untuk menginstal atau mengupdatenya, jalankan perintah berikut di antarmuka command line Anda:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Project Google Cloud dengan Google Chat API yang diaktifkan dan dikonfigurasi. Untuk mengetahui langkah-langkahnya, lihat Mem-build aplikasi Google Chat.
  • Otorisasi dikonfigurasi untuk aplikasi Chat. Mendapatkan detail tentang status baca pengguna dalam ruang memerlukan autentikasi pengguna dengan cakupan otorisasi chat.users.readstate atau chat.users.readstate.readonly.

Node.js

  • Node.js & npm
  • Library klien Google terbaru untuk Node.js. Untuk menginstalnya, jalankan perintah berikut di antarmuka command line Anda:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • Project Google Cloud dengan Google Chat API yang diaktifkan dan dikonfigurasi. Untuk mengetahui langkah-langkahnya, lihat Mem-build aplikasi Google Chat.
  • Otorisasi dikonfigurasi untuk aplikasi Chat. Mendapatkan detail tentang status baca pengguna dalam ruang memerlukan autentikasi pengguna dengan cakupan otorisasi chat.users.readstate atau chat.users.readstate.readonly.

Apps Script

  • Akun Google Workspace dengan akses ke Google Chat.
  • Aplikasi Chat yang dipublikasikan. Untuk mem-build aplikasi Chat, ikuti quickstart ini.
  • Otorisasi dikonfigurasi untuk aplikasi Chat. Mendapatkan detail tentang status baca pengguna dalam ruang memerlukan autentikasi pengguna dengan cakupan otorisasi chat.users.readstate atau chat.users.readstate.readonly.

Mendapatkan status baca ruang pengguna yang melakukan panggilan

Untuk mendapatkan detail tentang status baca pengguna dalam ruang, sertakan hal berikut dalam permintaan Anda:

  • Tentukan cakupan otorisasi chat.users.readstate atau chat.users.readstate.readonly.
  • Panggil metode getSpaceReadState di resource SpaceReadState.
  • Teruskan name status baca ruang untuk mendapatkan, yang mencakup ID pengguna atau alias dan ID ruang. Mendapatkan status baca ruang hanya mendukung pengambilan status baca pengguna yang memanggil, yang dapat ditentukan dengan menetapkan salah satu hal berikut:
    • Alias me. Misalnya, users/me/spaces/SPACE/spaceReadState.
    • Alamat email Workspace pengguna yang menelepon. Misalnya, users/user@example.com/spaces/SPACE/spaceReadState.
    • ID pengguna dari pengguna yang menelepon. Misalnya, users/USER/spaces/SPACE/spaceReadState.

Contoh berikut membuat status baca ruang pengguna yang melakukan panggilan:

Python

  1. Di direktori kerja, buat file bernama chat_spaceReadState_get.py.
  2. Sertakan kode berikut di 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. Dalam kode, ganti kode berikut:

    • SPACE: nama ruang, yang dapat Anda peroleh dari metode spaces.list di Chat API, atau dari URL ruang.
  4. Di direktori kerja Anda, buat dan jalankan contoh:

    python3 chat_spaceReadState_get.py
    

Node.js

  1. Di direktori kerja, buat file bernama chat_spaceReadState_get.js.
  2. Sertakan kode berikut di 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. Dalam kode, ganti kode berikut:

    • SPACE: nama ruang, yang dapat Anda peroleh dari metode spaces.list di Chat API, atau dari URL ruang.
  4. Di direktori kerja Anda, buat dan jalankan contoh:

    node chat_spaceReadState_get.js
    

Apps Script

Contoh ini memanggil Chat API menggunakan Advanced Chat Service.

  1. Tambahkan cakupan otorisasi chat.users.readstate.readonly ke file appsscript.json project Apps Script:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. Tambahkan fungsi seperti ini ke kode project Apps Script:

    /**
    * 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 mendapatkan status baca ruang yang ditentukan dan menampilkan instance resource SpaceReadState.