يوضّح هذا الدليل طريقة استخدام طريقة delete
في المورد 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
، حذف الرسائل التي أنشأها هذا المستخدم. إذا كنت مدير مساحة، قد تتمكّن من حذف الرسائل الأخرى. لمزيد من المعلومات، يُرجى الاطّلاع على مقالة التعرّف على دورك كمدير مساحة. - يمكن لمصادقة التطبيق،
من خلال نطاق التفويض
chat.bot
، حذف الرسائل التي أنشأها هذا التطبيق باستخدام حساب الخدمة الخاص به.
- يمكن لمصادقة المستخدم، باستخدام نطاق التفويض
حذف رسالة تتضمن مصادقة المستخدم
لحذف رسالة تتضمن مصادقة المستخدم، عليك اجتياز ما يلي في طلبك:
- حدِّد نطاق التفويض
chat.messages
. - عليك استدعاء
طريقة
delete
في الموردMessage
. - اضبط
name
على اسم المورد في الرسالة المطلوب حذفها.
يحذف المثال التالي رسالة تتضمن مصادقة المستخدم:
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_message_delete_user.py
. أدرِج الرمز التالي في
chat_message_delete_user.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"] def main(): ''' Authenticates with Chat API via user credentials, then deletes 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().delete( # The message to delete. # # 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()
في الرمز، استبدل ما يلي:
SPACE
: اسم مساحة، يمكنك الحصول عليه من طريقةspaces.list
في Chat API أو من عنوان URL لمساحة.MESSAGE
: اسم الرسالة الذي يمكنك الحصول عليه من نص الاستجابة الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_message_delete_user.py
إذا كانت الاستجابة ناجحة، يكون نص الاستجابة فارغًا، ما يشير إلى أنه تم حذف الرسالة.
حذف رسالة تتضمن مصادقة التطبيق
لحذف رسالة باستخدام مصادقة التطبيق، عليك اجتياز ما يلي في طلبك:
- حدِّد نطاق التفويض
chat.bot
. - عليك استدعاء
طريقة
delete
في الموردMessage
. - اضبط
name
على اسم المورد في الرسالة المطلوب حذفها.
يحذف المثال التالي رسالة تحتوي على مصادقة التطبيق:
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_delete_message_app.py
. أدرِج الرمز التالي في
chat_delete_message_app.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Delete a Chat message. result = chat.spaces().messages().delete( # The message to delete. # # 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. # When deleting a message, the response body is empty. print(result)
في الرمز، استبدل ما يلي:
SPACE
:name
من المساحة التي تم نشر الرسالة فيها، ويمكنك الحصول عليها من خلال طريقةspaces.list
في Chat API أو من عنوان URL للمساحة.MESSAGE
: اسم الرسالة الذي يمكنك الحصول عليه من نص الاستجابة الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_delete_message_app.py
إذا كانت الاستجابة ناجحة، يكون نص الاستجابة فارغًا، ما يشير إلى أنه تم حذف الرسالة.