يوضّح هذا الدليل طريقة استخدام طريقة list
على مورد Message
لواجهة Google Chat API
للاطّلاع على قائمة رسائل مقسّمة إلى صفحات قابلة للفلترة في مساحة.
يمثّل
مورد 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.messages.readonly
أوchat.messages
. - استدعِ
طريقة
list
في الموردMessage
.
يعرض المثال التالي الرسائل التي تم إرسالها في "مساحة Chat" بعد 16 آذار (مارس) 2023:
Python
- في دليل العمل، أنشِئ ملفًا باسم
chat_messages_list.py
. ضمِّن الرمز التالي في
chat_messages_list.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 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 the list of messages. print(result) if __name__ == '__main__': main()
في الرمز، استبدِل
SPACE
باسم مساحة، الذي يمكنك الحصول عليه من طريقةspaces.list
في Chat API أو من عنوان URL الخاص بالمساحة.في دليل العمل، أنشئ النموذج وقم بتشغيله:
python3 chat_messages_list.py
تعرض Google Chat API قائمة بالرسائل المرسَلة في المساحة المحدّدة بعد 16 آذار (مارس) 2023.