الحصول على تفاصيل حول رسالة

يشرح هذا الدليل طريقة استخدام طريقة get على مورد Message لواجهة برمجة تطبيقات Google Chat لعرض تفاصيل حول رسالة نصية أو رسالة بطاقة.

يمثّل مورد Message رسالة نص أو بطاقة في Google Chat. يمكنك create أو get أو update أو delete رسالة في Google Chat API من خلال استدعاء الطرق المطابِقة. لمزيد من المعلومات عن الرسائل النصية ورسائل البطاقات، راجِع نظرة عامة على رسائل 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.messages.readonly أو chat.messages، تلقّي الرسائل التي يمكن للمستخدم الوصول إليها.
    • يمكن لمصادقة التطبيق، باستخدام نطاق التفويض chat.bot، تلقّي الرسائل المُرسَلة إلى التطبيق.

تلقّي رسالة تتضمّن مصادقة المستخدم

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

  • حدِّد نطاق التفويض chat.messages.readonly أو chat.messages.
  • استدعِ طريقة get في المورد Message.
  • اضبط name على اسم مورد الرسالة التي تريد الحصول عليها.

يتلقى المثال التالي رسالة تتضمن مصادقة المستخدم:

Python

  1. في دليل العمل، أنشِئ ملفًا باسم chat_message_get_user.py.
  2. ضمِّن الرمز التالي في chat_message_get_user.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.messages.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets a message.
        '''
    
        # 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.spaces().messages().get(
    
            # The message to get.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MESSAGE with a message name.
            # Obtain the message name from the response body returned
            # after creating a message asynchronously with Chat REST API.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة.
    • MESSAGE: اسم رسالة يمكنك الحصول عليه من نص الردّ الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن مع Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
  4. في دليل العمل، أنشئ النموذج وقم بتشغيله:

    python3 chat_message_get_user.py
    

تعرِض Chat API مثيل Message الذي يوضِّح الرسالة المحدَّدة.

تلقّي رسالة تتضمّن مصادقة التطبيقات

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

  • حدِّد نطاق تفويض chat.bot.
  • استدعِ طريقة get في المورد Message.
  • اضبط name على اسم مورد الرسالة التي تريد الحصول عليها.

يتلقى المثال التالي رسالة تتضمن مصادقة التطبيق:

Python

  1. في دليل العمل، أنشِئ ملفًا باسم chat_get_message_app.py.
  2. ضمِّن الرمز التالي في chat_get_message_app.py:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Get a Chat message.
    result = chat.spaces().messages().get(
    
        # The message to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        #
        # Replace MESSAGE with a message name.
        # Obtain the message name from the response body returned
        # after creating a message asynchronously with Chat REST API.
        name='spaces/SPACE/messages/MESSAGE'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. في التعليمة البرمجية، استبدل ما يلي:

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

    • MESSAGE: اسم الرسالة الذي يمكنك الحصول عليه من نص الردّ الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن مع Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها

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

    python3 chat_get_message_app.py
    

تعرِض Chat API مثيل Message الذي يوضِّح الرسالة المحدَّدة.