الحصول على تفاصيل حول حالة قراءة مساحة المستخدم

يشرح هذا الدليل كيفية استخدام طريقة getSpaceReadState على مرجع SpaceReadState لواجهة Google Chat API للحصول على تفاصيل حول قراءة المستخدم داخل مساحة ما. للحصول على حالة قراءة رسالة في سلسلة رسائل، الرؤية الحصول على تفاصيل حول حالة قراءة سلسلة محادثات المستخدم

تشير رسالة الأشكال البيانية مرجع SpaceReadState هو مورد فرديتون يمثل تفاصيل حول آخر رسالة مقروءة للمستخدم المحدّد في مساحة Google Chat.

المتطلبات الأساسية

Python

  • Python 3.6 أو أحدث
  • أداة إدارة حزم pip
  • أحدث مكتبات عملاء Google. لتثبيت التطبيقات أو تحديثها، قم بتشغيل الأمر التالي في واجهة سطر الأوامر:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

برمجة تطبيقات

الحصول على حالة قراءة مساحة المستخدم المتصل

للحصول على تفاصيل حول حالة القراءة للمستخدم داخل مساحة، يمكنك تضمين ما يلي: في طلبك:

  • تحديد chat.users.readstate أو chat.users.readstate.readonly نطاق التفويض.
  • عليك استدعاء طريقة getSpaceReadState في صفحة مرجع واحد (SpaceReadState)
  • عليك اجتياز name لحالة قراءة المساحة للحصول على، بما في ذلك رقم تعريف المستخدم أو والاسم المستعار ومعرّف المساحة. لا يدعم الحصول على حالة قراءة المسافة إلا الحصول على القيمة المقروءة حالة المستخدم المتصل، والتي يمكن تحديدها عن طريق تعيين إحدى التالي:
    • العنوان البديل للبريد الإلكتروني me على سبيل المثال: users/me/spaces/SPACE/spaceReadState
    • عنوان البريد الإلكتروني للمستخدم في Workspace على سبيل المثال: users/user@example.com/spaces/SPACE/spaceReadState
    • رقم تعريف المستخدم الخاص بالمستخدم المتصل على سبيل المثال: users/USER/spaces/SPACE/spaceReadState

يحصل المثال التالي على حالة قراءة مساحة المستخدم المتصل:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_spaceReadState_get.py
  2. أدرِج الرمز التالي في 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. في الرمز، استبدل ما يلي:

  4. في دليل العمل، أنشئ النموذج وشغِّله:

    python3 chat_spaceReadState_get.py
    

Node.js

  1. في دليل العمل، أنشئ ملفًا باسم chat_spaceReadState_get.js
  2. أدرِج الرمز التالي في 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. في الرمز، استبدل ما يلي:

  4. في دليل العمل، أنشئ النموذج وشغِّله:

    node chat_spaceReadState_get.js
    

برمجة تطبيقات

يستدعي هذا المثال واجهة برمجة تطبيقات Chat باستخدام خدمة Chat المتقدّمة

  1. إضافة نطاق تفويض chat.users.readstate.readonly إلى ملف appsscript.json لمشروع برمجة التطبيقات:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.users.readstate.readonly"
    ]
    
  2. أضف دالة كهذه إلى مشروع "برمجة تطبيقات Google" الرمز:

    /**
    * 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 على حالة قراءة المساحة المحدّدة وتعرضها مثيل على مرجع SpaceReadState