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

يوضّح هذا الدليل كيفية استخدام طريقة getThreadReadState على مورد ThreadReadState لواجهة برمجة تطبيقات Google Chat للحصول على تفاصيل حول حالة القراءة للمستخدم ضمن سلسلة محادثات. لمعرفة حالة قراءة رسالة في مساحة، راجِع الحصول على تفاصيل حول حالة القراءة في مساحة المستخدم.

المورد ThreadReadState هو مورد مفرد يمثّل تفاصيل حول آخر رسالة مقروءة لمستخدم محدّد في سلسلة رسائل على Google Chat.

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

Python

  • Python 3.6 أو أحدث
  • تتيح لك أداة إدارة الحِزم pip
  • أحدث مكتبات عملاء Google للغة Python. لتثبيتها أو تحديثها، شغّل الأمر التالي في واجهة سطر الأوامر:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • مشروع على Google Cloud تم فيه تفعيل Google Chat API وضبطه لمعرفة الخطوات، يُرجى الاطّلاع على مقالة إنشاء تطبيق Google Chat.
  • تم ضبط التفويض لتطبيق Chat. للحصول على تفاصيل حول حالة القراءة للمستخدم في إحدى المساحات، يجب توفُّر مصادقة المستخدم من خلال نطاق التفويض chat.users.readstate أو chat.users.readstate.readonly.

Node.js

  • Node.js وnpm
  • أحدث مكتبات عملاء Google لنظام Node.js. ولتثبيتها، شغِّل الأمر التالي في واجهة سطر الأوامر:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • مشروع على Google Cloud تم فيه تفعيل Google Chat API وضبطه لمعرفة الخطوات، يُرجى الاطّلاع على مقالة إنشاء تطبيق Google Chat.
  • تم ضبط التفويض لتطبيق Chat. للحصول على تفاصيل حول حالة القراءة للمستخدم في إحدى المساحات، يجب توفُّر مصادقة المستخدم من خلال نطاق التفويض chat.users.readstate أو chat.users.readstate.readonly.

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

  • حساب على Google Workspace يمكنه الوصول إلى Google Chat
  • هو تطبيق Chat منشور. لإنشاء تطبيق في Chat، اتّبِع quickstart.
  • تم ضبط التفويض لتطبيق Chat. للحصول على تفاصيل حول حالة القراءة للمستخدم في إحدى المساحات، يجب توفُّر مصادقة المستخدم من خلال نطاق التفويض chat.users.readstate أو chat.users.readstate.readonly.

معرفة حالة قراءة سلسلة محادثات المستخدم المتصل

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

  • حدِّد نطاق التفويض 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. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • THREAD: اسم سلسلة محادثات يمكنك الحصول عليها من طريقة spaces.messages.get في Chat API.

  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. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • THREAD: اسم سلسلة محادثات يمكنك الحصول عليها من طريقة spaces.messages.get في Chat API.

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

    node chat_threadReadState_get.js
    

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

يستدعي هذا المثال واجهة Chat API باستخدام Advanced Chat Service.

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

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

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