Bir kullanıcının alan okuma durumunu güncelleme

Bu kılavuzda, updateSpaceReadState yönteminin nasıl kullanılacağı Alanları okundu veya okunmadı olarak işaretlemek için SpaceReadState Google Chat API kaynağı.

İlgili içeriği oluşturmak için kullanılan SpaceReadState kaynak hakkındaki ayrıntıları temsil eden tekil bir kaynaktır: Belirtilen kullanıcının bir Google Chat alanında son okunan mesajı.

Ön koşullar

Python

  • Python 3.6 veya sonraki sürümler
  • pip paket yönetim aracı
  • En yeni Google istemci kitaplıkları. Bu uygulamaları 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
    

Node.js

  • Node.js 14 veya sonraki sürümler
  • npm paket yönetim aracı
  • En yeni Google istemci kitaplıkları. Bu uygulamaları yüklemek veya güncellemek 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
    

Apps Komut Dosyası

Arayan kullanıcının alan okuma durumunu güncelle

Bir kullanıcının bir alandaki okuma durumunu güncellemek için aşağıdakileri ekleyin: talebiniz:

  • chat.users.readstate yetkilendirme kapsamını belirtin.
  • Şunu çağırın: updateSpaceReadState yöntem uygulamasında SpaceReadState kaynak.
  • Almak için alan okuma durumunun name kısmını iletin (kullanıcı kimliği veya kullanıcı kimliği dahil). takma ad ve alan kimliği girin. Alan okuma durumu, yalnızca okumanın alınmasını destekler Bu, arayan kullanıcının durumu, takip etmek için:
    • 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 kullanıcı kimliği. Örneğin, users/USER/spaces/SPACE/spaceReadState
  • Alanın okuma durumunun özelliklerini belirten updateMask parametresini iletin. güncelleme:
    • lastReadTime: Kullanıcının alan okuma durumunun güncellendiği zaman. Bu genellikle son okunan mesajın zaman damgasına karşılık gelir. veya boşluk oluşturur. lastReadTime en son mesaj oluşturulma zamanından önce olduğunda boşluk, kullanıcı arayüzünde okunmamış olarak görünür. Alanı okundu olarak işaretlemek için lastReadTime, oluşturulan en son mesajdan sonraki herhangi bir değere (daha büyük) gerekir. lastReadTime, en son mesaj oluşturma zamanıyla eşleşmeye zorlandı. Alan okuma durumunun yalnızca şurada bulunan iletilerin okunma durumunu etkilediğini unutmayın: alanın üst düzey görüşmesinde görünür. İleti dizilerindeki yanıtlar: etkilenmez ve bunun yerine, ileti dizisinin okuma durumunu temel alır.

Aşağıdaki örnek, arayan kullanıcının alan okuma durumunu günceller:

Python

  1. Çalışma dizininizde şu ada sahip bir dosya oluşturun: chat_spaceReadState_update.py
  2. Aşağıdaki kodu chat_spaceReadState_update.py bölümüne 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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates 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().updateSpaceReadState(
    
            # The space read state to update.
            #
            # 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',
            updateMask='lastReadTime',
            body={'lastReadTime': f'{datetime.datetime(2000, 1, 3).isoformat()}Z'}
    
          ).execute()
    
        # Prints the API's response.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. Bu kodda, aşağıdakini değiştirin:

    • SPACE: bir alan adı; şuradan edinebilirsiniz: spaces.list yöntem API'den veya bir alanın URL'sinden.
  4. Çalışma dizininizde örneği derleyin ve çalıştırın:

    python3 chat_spaceReadState_update.py
    

Node.js

  1. Çalışma dizininizde şu ada sahip bir dosya oluşturun: chat_spaceReadState_update.js
  2. Aşağıdaki kodu chat_spaceReadState_update bölümüne ekleyin:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Authenticates with Chat API via user credentials,
    * then updates the space read state for the calling user.
    * @return {!Promise<!Object>}
    */
    async function updateSpaceReadState() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.users.readstate',
      ];
    
      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.updateSpaceReadState({
    
        /**
        * The space read state to update.
        *
        * 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',
        updateMask: 'lastReadTime',
        requestBody: {
          lastReadTime: '{datetime.datetime(2000, 1, 3).isoformat()}Z'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    getSpaceReadState().then(console.log);
    
  3. Bu kodda, aşağıdakini değiştirin:

    • SPACE: bir alan adı; şuradan edinebilirsiniz: spaces.list yöntem API'den veya bir alanın URL'sinden.
  4. Çalışma dizininizde örneği derleyin ve çalıştırın:

    node chat_spaceReadState_update.js
    

Apps Komut Dosyası

Bu örnekte, Gelişmiş Chat Hizmeti.

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

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate"
    ]
    
  2. Apps Komut Dosyası projesinin kod:

    /**
    * Authenticates with Chat API via user credentials,
    * then updates the space read state for the calling user.
    * @param {string} spaceReadStateName The resource name of the space read state.
    */
    function updateSpaceReadState(spaceReadStateName) {
      try {
        const time = new Date('January 1, 2000')).toJSON();
        const body = {'lastReadTime': time};
        Chat.Users.Spaces.updateSpaceReadState(spaceReadStateName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to update read state with error %s', err.message);
      }
    }
    

Google Chat API, belirtilen alanın okuma durumunu günceller ve geri döner örneğinin SpaceReadState kaynak.