Bir kullanıcının alan okuma durumuyla ilgili ayrıntıları alma

Bu rehberde, kullanıcının bir alandaki okuma durumu hakkında ayrıntılı bilgi edinmek için Google Chat API'nin SpaceReadState kaynağında getSpaceReadState yönteminin nasıl kullanılacağı açıklanmıştır. İleti dizisindeki bir mesajın okunma durumunu öğrenmek için Bir kullanıcının ileti dizisi okunma durumuyla ilgili ayrıntıları alma bölümüne göz atın.

SpaceReadState kaynağı, belirli bir kullanıcının Google Chat alanında son okunan mesajıyla ilgili ayrıntıları temsil eden bir tekil kaynaktır.

Ön koşullar

Python

  • Python 3.6 veya sonraki sürümler
  • pip paket yönetimi aracı
  • Python için en yeni Google istemci kitaplıkları. Bunları yüklemek veya güncellemek için komut satırı arayüzünüzde aşağıdaki komutu çalıştırın:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Google Chat API'nin etkin ve yapılandırılmış olduğu bir Google Cloud projesi. Adımlar için Google Chat uygulaması oluşturma başlıklı makaleye göz atın.
  • Yetkilendirme, Chat uygulaması için yapılandırıldı. Bir kullanıcının bir alandaki okuma durumuyla ilgili ayrıntıları almak için chat.users.readstate veya chat.users.readstate.readonly yetkilendirme kapsamıyla kullanıcı kimlik doğrulaması gerekir.

Node.js

  • Node.js ve npm
  • Node.js için en yeni Google istemci kitaplıkları. Bunları yüklemek için komut satırı arayüzünüzde aşağıdaki komutu çalıştırın:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • Google Chat API'nin etkin ve yapılandırılmış olduğu bir Google Cloud projesi. Adımlar için Google Chat uygulaması oluşturma başlıklı makaleye göz atın.
  • Yetkilendirme, Chat uygulaması için yapılandırıldı. Bir kullanıcının bir alandaki okuma durumuyla ilgili ayrıntıları almak için chat.users.readstate veya chat.users.readstate.readonly yetkilendirme kapsamıyla kullanıcı kimlik doğrulaması gerekir.

Apps Komut Dosyası

  • Google Chat'e erişimi olan bir Google Workspace hesabı.
  • Yayınlanmış bir Chat uygulamasıdır. Chat uygulaması oluşturmak için bu quickstart takip edin.
  • Yetkilendirme, Chat uygulaması için yapılandırıldı. Bir kullanıcının bir alandaki okuma durumuyla ilgili ayrıntıları almak için chat.users.readstate veya chat.users.readstate.readonly yetkilendirme kapsamıyla kullanıcı kimlik doğrulaması gerekir.

Arayan kullanıcının alan okuma durumunu alma

Bir kullanıcının alandaki okunma durumuyla ilgili ayrıntıları almak için isteğinize aşağıdakileri ekleyin:

  • chat.users.readstate veya chat.users.readstate.readonly yetkilendirme kapsamını belirtin.
  • SpaceReadState kaynağında getSpaceReadState yöntemini çağırın.
  • Almak için alan okuma durumunun name değerini iletin. Bu, kullanıcı kimliği veya takma ad ve boşluk kimliği içerir. Boşluk okuma durumunun alınması yalnızca çağıran kullanıcının okuma durumunun alınmasını destekler. Bu durum, aşağıdakilerden biri ayarlanarak belirtilebilir:
    • me takma adı. Örneğin, users/me/spaces/SPACE/spaceReadState.
    • Arayan kullanıcının Workspace e-posta adresi. Örneğin, users/user@example.com/spaces/SPACE/spaceReadState.
    • Arayan kullanıcının kimliği. Örneğin, users/USER/spaces/SPACE/spaceReadState.

Aşağıdaki örnekte, arayan kullanıcının alan okuma durumu verilmiştir:

Python

  1. Çalışma dizininizde chat_spaceReadState_get.py adında bir dosya oluşturun.
  2. chat_spaceReadState_get.py içine şu kodu ekleyin:

    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. Kodda aşağıdakileri değiştirin:

  4. Çalışma dizininizde örneği derleyip çalıştırın:

    python3 chat_spaceReadState_get.py
    

Node.js

  1. Çalışma dizininizde chat_spaceReadState_get.js adında bir dosya oluşturun.
  2. chat_spaceReadState_get içine şu kodu ekleyin:

    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. Kodda aşağıdakileri değiştirin:

  4. Çalışma dizininizde örneği derleyip çalıştırın:

    node chat_spaceReadState_get.js
    

Apps Komut Dosyası

Bu örnek, Gelişmiş Sohbet Hizmeti'ni kullanan Chat API'yi çağırır.

  1. chat.users.readstate.readonly yetkilendirme kapsamını, Apps Komut Dosyası projesinin appsscript.json dosyasına ekleyin:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. Apps Komut Dosyası projesinin koduna şunun gibi bir işlev ekleyin:

    /**
    * 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, belirtilen alan okuma durumunu alır ve SpaceReadState kaynağının bir örneğini döndürür.