تعديل رسالة

يشرح هذا الدليل كيفية استخدام طريقة patch على مورد Message الخاص بـ Google Chat API لتعديل رسالة نصية أو رسالة بطاقة في مساحة تعديل لتغيير سمات الرسالة، مثل ما تقوله، أو محتوى بنجاح. يمكنك أيضًا إضافة رسالة نصية في البداية في أو إلحاق بطاقة برسالة نصية.

تدعم Chat API أيضًا طريقة update، ولكننا ننصح بشدة بالاتصال طريقة patch لأنه يستخدم طلب HTTP PATCH أثناء يستخدم update PUT طلب HTTP. لمزيد من المعلومات، يُرجى مراجعة قسم PATCH وPUT في AIP-134.

في Chat API، يتم تمثيل رسالة Chat من خلال مرجع Message يمكن لمستخدمي Chat إرسال الرسائل التي تحتوي على نصوص فقط، يمكن لتطبيقات Chat استخدام العديد من ميزات المراسلة الأخرى، بما في ذلك تعرض واجهات مستخدم ثابتة أو تفاعلية، تجمع المعلومات من والمستخدمين، وتسليم الرسائل بشكل خاص. لمعرفة المزيد من المعلومات عن المراسلة الميزات المتوفرة في Chat API، يمكنك الاطّلاع على نظرة عامة على رسائل Google Chat

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

Python

  • Python 3.6 أو أعلى
  • أداة إدارة حزم pip
  • أحدث مكتبات عملاء Google. لتثبيت التطبيقات أو تحديثها، قم بتشغيل الأمر التالي في واجهة سطر الأوامر:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

تعديل رسالة نصية أو إضافة رسالة نصية في البداية إلى رسالة بطاقة من خلال مصادقة المستخدم

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

  • نطاق تفويض chat.messages.
  • name للرسالة المطلوب تعديلها.
  • updateMask='text'
  • عنصر body يحدد الرسالة المُعدَّلة.

إذا كانت الرسالة المُحدَّثة رسالة بطاقة، ثم تتم إضافة الرسالة النصية إلى رسالة البطاقة (التي تستمر في الظهور).

إليك كيفية تحديث رسالة نصية، أو إضافة رسالة نصية أولاً إلى رسالة البطاقة مع مصادقة المستخدم:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_update_text_message_user.py
  2. أدرِج الرمز التالي في chat_update_text_message_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 updates 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)
    
        # Update a Chat message.
        result = chat.spaces().messages().patch(
    
          # The message to update, and the updated message.
          #
          # 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',
          updateMask='text',
          body={'text': 'Updated message!'}
    
        ).execute()
    
        # Prints details about the updated message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدل ما يلي:

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

    python3 chat_update_text_message_user.py
    

تعديل رسالة نصية أو إضافة رسالة نصية في البداية إلى رسالة بطاقة من خلال المصادقة على التطبيقات

لتحديث رسالة نصية مع مصادقة التطبيقات قم بتمرير ما يلي في طلبك:

  • نطاق تفويض chat.bot.
  • name للرسالة المطلوب تعديلها.
  • updateMask='text'
  • عنصر body يحدد الرسالة المُعدَّلة.

إذا كانت الرسالة المعدّلة رسالة بطاقة، ثم تتم إضافة الرسالة النصية إلى رسالة البطاقة (التي تستمر في الظهور).

إليك كيفية تحديث رسالة نصية في رسالة نصية أو إضافة رسالة نصية في البداية رسالة البطاقة مع مصادقة التطبيقات:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_update_text_message_app.py
  2. أدرِج الرمز التالي في chat_update_text_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)
    
    # Update a Chat message.
    result = chat.spaces().messages().patch(
    
      # The message to update, and the updated message.
      #
      # 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',
      updateMask='text',
      body={'text': 'Updated message!'}
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. في الرمز، استبدل ما يلي:

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

    python3 chat_update_text_message_app.py
    

تعديل رسالة بطاقة أو إضافة بطاقة إلى رسالة نصية

لتحديث رسالة بطاقة، قم بتمرير ما يلي في طلبك:

  • نطاق تفويض chat.bot. يجب تعديل رسالة البطاقة مصادقة التطبيقات.
  • name للرسالة المطلوب تعديلها.
  • updateMask='cardsV2'
  • عنصر body يحدد الرسالة المُعدَّلة.

إذا كانت الرسالة المُحدَّثة الرسائل النصية ثم يتم إلحاق بطاقة بالرسالة النصية (التي يستمر عرضها). إذا كانت الرسالة المحدثة هي نفسها card، فإن البطاقة المعروضة هي تحديث.

إليك كيفية تعديل رسالة إلى رسالة البطاقة:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_update_card_message.py
  2. أدرِج الرمز التالي في chat_update_card_message.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)
    
    # Update a Chat message.
    result = chat.spaces().messages().patch(
    
      # The message to update, and the updated message.
      #
      # 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',
      updateMask='cardsV2',
      body=
      {
        'cardsV2': [{
          'cardId': 'updateCardMessage',
          'card': {
            'header': {
              'title': 'An Updated Card Message!',
              'subtitle': 'Updated with Chat REST API',
              'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
              'imageType': 'CIRCLE'
            },
            'sections': [
              {
                'widgets': [
                  {
                    'buttonList': {
                      'buttons': [
                        {
                          'text': 'Read the docs!',
                          'onClick': {
                            'openLink': {
                              'url': 'https://developers.google.com/chat'
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }]
      }
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. في الرمز، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من الـ طريقة واحدة (spaces.list) في Chat API أو من عنوان URL للمساحة.

    • MESSAGE: اسم رسالة يمكنك الحصول عليه من نص الاستجابة الذي تم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو باستخدام اسم مخصّص المخصص للرسالة عند الإنشاء.

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

    python3 chat_update_card_message.py
    

تعرض واجهة برمجة التطبيقات Chat مثيلاً من Message يوضح بالتفصيل الرسالة التي يتم تحديثها.

تعديل رسالة تحتوي على مسارات حقول متعددة في آنٍ واحد

عند تعديل رسالة، يمكنك تعديل مسارات متعددة لحقول الرسائل على الوقت. على سبيل المثال، في طلب التحديث، يمكنك تحديد تغيير على text وcardsv2 مسار الحقل في الوقت نفسه، والذي يعدِّل كلاً من نص الرسالة وبطاقةها. إذا كانت الرسالة تتضمن نصًا فقط بدون بطاقة، فسيتم استخدام بطاقة تمت إضافته إلى الرسالة. للاطّلاع على مزيد من المعلومات حول مسارات الحقول المتوافقة، الرؤية updateMask مَعلمات.

لتحديث كل من text أو card لرسالة تشتمل على مصادقة المستخدم، قم بتمرير ما يلي في طلبك:

  • نطاق تفويض chat.messages.
  • name للرسالة المطلوب تعديلها.
  • عنصر updateMask يحدد مسارات حقول الرسائل المطلوب تعديلها، ويتم فصلها بفواصل: updateMask='text', 'cardsV2'.

  • body يحدد الرسالة المُعدَّلة، بما في ذلك جميع الحقول المُعدَّلة والمسارات.

في ما يلي كيفية تعديل مسارَي الحقلَين text وcardsV2 في رسالة مع مصادقة المستخدم:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_update_text_message_user.py
  2. أدرِج الرمز التالي في chat_update_text_message_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 updates 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)
    
        # Update a Chat message.
        result = chat.spaces().messages().patch(
    
          # The message to update, and the updated message.
          #
          # 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',
          updateMask='text,cardsV2',
          body=
          {'text': 'Updated message!',
                'cardsV2': [{
                  'cardId': 'updateCardMessage',
                  'card': {
                    'header': {
                      'title': 'An Updated Card Message!',
                      'subtitle': 'Updated with Chat REST API',
                      'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
                      'imageType': 'CIRCLE'
                    },
                    'sections': [
                      {
                        'widgets': [
                          {
                            'buttonList': {
                              'buttons': [
                                {
                                  'text': 'Read the docs!',
                                  'onClick': {
                                    'openLink': {
                                      'url': 'https://developers.google.com/chat'
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    ]
                  }
                }]
          }
    
        ).execute()
    
        # Prints details about the updated message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدل ما يلي:

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

    python3 chat_update_text_message_user.py