يشرح هذا الدليل كيفية استخدام طريقة list
على المرجع Reaction
.
Google Chat API لعرض التفاعلات مع رسالة معيّنة، مثل 👍 و🚲 و🌎.
تشير رسالة الأشكال البيانية
مرجع Reaction
يمثّل رمزًا تعبيريًا يمكن للمستخدمين استخدامه للتفاعل مع رسالة، مثلاً 👍 أو 🚲
و ⭐.
المتطلبات الأساسية
Python
- نشاط تجاري أو مؤسسة حساب Google Workspace لديه إذن بالوصول إلى Google Chat
- Python 3.6 أو أعلى
- أداة إدارة حزم pip
- أحدث مكتبات عملاء Google. لتثبيت التطبيقات أو تحديثها،
قم بتشغيل الأمر التالي في واجهة سطر الأوامر:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- إعداد البيئة:
- أنشِئ مشروعًا على Google Cloud.
- ضبط شاشة موافقة OAuth
- تفعيل Google Chat API وضبطه باستخدام اسم ورمزه ووصفه لتطبيق Chat.
-
أنشئ بيانات اعتماد معرِّف عميل OAuth لتطبيق متوافق مع الكمبيوتر المكتبي. لتنفيذ النموذج في هذه
فقط، احفظ بيانات الاعتماد كملف JSON (
client_secrets.json
) في الدليل المحلي.
- اختر نطاق تفويض يتيح مصادقة المستخدم.
إدراج التفاعلات
لعرض التفاعلات مع رسالة معيّنة، عليك تضمين ما يلي في طلبك:
- حدِّد
chat.messages.reactions.readonly
أوchat.messages.reactions
نطاق التفويضchat.messages.readonly
أوchat.messages
- عليك استدعاء
[طريقة واحدة (
list
)]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) في صفحةReaction
مورد.
يعرض المثال التالي التفاعلات مع رسالة محدّدة:
Python
- في دليل العمل، أنشِئ ملفًا باسم "
chat_reactions_list.py
". أدرِج الرمز التالي في
chat_reactions_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.reactions.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists reactions to 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().reactions().list( # The message to list reactions to. # # 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. parent = 'spaces/SPACE/messages/MESSAGE' ).execute() # Prints details about the created reactions. print(result) if __name__ == '__main__': main()
في الرمز، استبدل ما يلي:
SPACE
: اسم مساحة يمكنك الحصول عليه من الـ طريقة واحدة (spaces.list
) في Chat API أو من عنوان URL للمساحة.MESSAGE
: اسم رسالة يمكنك الحصول عليه من نص الاستجابة الذي تم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو باستخدام اسم مخصّص المخصص للرسالة عند الإنشاء.
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_reactions_list.py
تعرض واجهة Chat API مصفوفة من التفاعلات المقسّمة على صفحات.