إدراج الرسائل

يوضّح هذا الدليل طريقة استخدام طريقة list في مورد Message الخاص بـ Google Chat API للاطّلاع على قائمة رسائل مقسّمة على صفحات وقابلة للفلترة في مساحة.

يمثّل المورد Message رسالة نصية أو بطاقة في Google Chat. يمكنك create أو get أو update أو delete رسالة في Google Chat API من خلال طلب الطرق المناسبة. لمزيد من المعلومات عن الرسائل النصية ورسائل البطاقات، راجِع نظرة عامة على رسائل Google Chat.

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

Python

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

    pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
    
  • أحد تطبيقات Chat المنشورة. لإنشاء تطبيق Chat ونشره، يمكنك الاطّلاع على إنشاء تطبيق Google Chat.

  • تم ضبط التفويض لتطبيق Chat. تتطلّب رسائل البيانات مصادقة المستخدم مع نطاق التفويض chat.messages.readonly أو chat.messages.

إدراج الرسائل

لإدراج الرسائل باستخدام مصادقة المستخدم، عليك اجتياز ما يلي في طلبك:

يعرض المثال التالي الرسائل في مساحة Chat التي تم إرسالها بعد 16 آذار (مارس) 2023:

Python

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

    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # 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 lists messages in a space sent after March 16, 2023.
        '''
    
        # 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().list(
    
              # The space for which to list messages.
              parent = 'spaces/SPACE',
    
              # An optional filter that returns messages
              # created after March 16, 2023.
              filter = 'createTime > "2023-03-16T00:00:00-00:00"'
    
          ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدِل SPACE باسم مساحة، والذي يمكنك الحصول عليه من طريقة spaces.list في Chat API أو من عنوان URL لمساحة.

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

    python3 chat_messages_list.py
    

تعرض Google Chat API قائمة بالرسائل المُرسَلة في المساحة المحدَّدة بعد 16 آذار (مارس) 2023.