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

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

تشير رسالة الأشكال البيانية مرجع ThreadReadState هو مورد فرديتون يمثل تفاصيل حول آخر رسالة مقروءة للمستخدم في سلسلة رسائل 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 نطاق التفويض.
  • عليك استدعاء طريقة getThreadReadState في صفحة مرجع واحد (ThreadReadState)
  • يجب اجتياز name لحالة قراءة سلسلة المحادثات التي تحصل عليها، والتي تتضمن رقم تعريف المستخدم أو والاسم المستعار ومعرّف المساحة. لا يتيح الحصول على حالة قراءة سلسلة المحادثات إلا الحصول على القيمة التي تمت قراءتها حالة المستخدم المتصل، والتي يمكن تحديدها عن طريق تعيين إحدى التالي:
    • العنوان البديل للبريد الإلكتروني me على سبيل المثال: users/me/spaces/SPACE/threads/THREAD/threadReadState
    • عنوان البريد الإلكتروني للمستخدم في Workspace على سبيل المثال: users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
    • رقم تعريف المستخدم الخاص بالمستخدم المتصل على سبيل المثال: users/USER/spaces/SPACE/threads/THREAD/threadReadState

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

Python

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

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

    python3 chat_threadReadState_get.py
    

Node.js

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

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

    node chat_threadReadState_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 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);
      }
    }
    

تحصل Google Chat API على حالة قراءة سلسلة المحادثات المحدّدة وترجع مثيل على مرجع ThreadReadState