حذف رسالة

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

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

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

Python

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

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • مشروع على Google Cloud مع تفعيل وإعداد Google Chat API لمعرفة الخطوات، راجِع إنشاء تطبيق Google Chat.
  • تم ضبط التفويض لتطبيق Chat. يتوافق حذف رسالة مع طريقتَي المصادقة التاليتَين:

حذف رسالة تتضمن مصادقة المستخدم

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

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

يؤدي المثال التالي إلى حذف رسالة تتضمن مصادقة المستخدم:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_message_delete_user.py.
  2. أدرِج الرمز التالي في chat_message_delete_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"]
    
    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 response to the Chat API call.
        # When deleting a message, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدل ما يلي:

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

    python3 chat_message_delete_user.py
    

إذا كانت الاستجابة ناجحة، يكون نص الاستجابة فارغًا، مما يشير إلى أن الرسالة محذوفة.

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

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

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

يؤدي المثال التالي إلى حذف رسالة تتضمّن مصادقة التطبيقات:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_delete_message_app.py.
  2. أدرِج الرمز التالي في chat_delete_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)
    
    # 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)
    
  3. في الرمز، استبدل ما يلي:

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

    python3 chat_delete_message_app.py
    

إذا كانت الاستجابة ناجحة، يكون نص الاستجابة فارغًا، مما يشير إلى أن الرسالة محذوفة.