يوضّح هذا الدليل طريقة استخدام طريقة patch
في مورد Message
الخاص بـ Google Chat API لتعديل رسالة نصية أو رسالة بطاقة في مساحة. ويمكنك تعديل
رسالة لتغيير سمات الرسالة، مثل ما تقولها، أو محتوى
بطاقة. يمكنك أيضًا إضافة رسالة نصية قبل رسالة
بطاقة أو إلحاق بطاقة برسالة نصية.
تتوافق Chat API أيضًا مع
طريقة update
، ولكننا
ننصحك بشدة باستخدام
طريقة patch
لأنّها
تستخدم طلب HTTP يتضمّن PATCH
، بينما تستخدم
update
طلب HTTP
PUT
. لمعرفة المزيد من المعلومات، انظر
قسم PATCH
وPUT
الخاص بـ AIP-134.
يمثّل
المورد 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.bot
.
- يتيح تعديل رسالة نصية استخدام طريقتَي المصادقة التاليتَين:
تعديل رسالة نصية أو إضافة رسالة نصية قبل رسالة بطاقة، وذلك من خلال مصادقة المستخدم
لتحديث رسالة نصية باستخدام مصادقة المستخدم، أكمل ما يلي في الطلب:
- نطاق التفويض
chat.messages
. name
للرسالة المطلوب تعديلها.updateMask='text'
- تمثّل هذه السمة
body
التي تحدّد الرسالة المعدَّلة.
إذا كانت الرسالة المعدّلة هي رسالة بطاقة، ستتم إضافة الرسالة النصية قبل رسالة البطاقة (التي يستمر عرضها).
إليك كيفية تعديل رسالة نصية أو إضافة رسالة نصية إلى رسالة بطاقة باستخدام مصادقة المستخدم:
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_update_text_message_user.py
. أدرِج الرمز التالي في
chat_update_text_message_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 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 created membership. print(result) if __name__ == '__main__': main()
في الرمز، استبدل ما يلي:
SPACE
: اسم مساحة، يمكنك الحصول عليه من طريقةspaces.list
في Chat API أو من عنوان URL للمساحة.MESSAGE
: اسم الرسالة الذي يمكنك الحصول عليه من نص الاستجابة الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_update_text_message_user.py
يمكنك تعديل رسالة نصية أو إضافة رسالة نصية قبل رسالة بطاقة باستخدام ميزة مصادقة التطبيق.
لتحديث رسالة نصية باستخدام مصادقة التطبيق، عليك اجتياز ما يلي في طلبك:
- نطاق التفويض
chat.bot
. name
للرسالة المطلوب تعديلها.updateMask='text'
- تمثّل هذه السمة
body
التي تحدّد الرسالة المعدَّلة.
إذا كانت الرسالة المعدّلة هي رسالة بطاقة، ستتم إضافة الرسالة النصية قبل رسالة البطاقة (التي يستمر عرضها).
إليك كيفية تعديل رسالة نصية على رسالة نصية، أو إضافة رسالة نصية إلى رسالة بطاقة باستخدام مصادقة التطبيق:
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_update_text_message_app.py
. أدرِج الرمز التالي في
chat_update_text_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())) # 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)
في الرمز، استبدل ما يلي:
SPACE
: اسم مساحة، يمكنك الحصول عليه من طريقةspaces.list
في Chat API أو من عنوان URL لمساحة.MESSAGE
: اسم الرسالة الذي يمكنك الحصول عليه من نص الاستجابة الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_update_text_message_app.py
تعديل رسالة بطاقة أو إضافة رسالة بطاقة إلى رسالة نصية
لتعديل رسالة بطاقة، عليك تمرير ما يلي في طلبك:
- نطاق التفويض
chat.bot
. يتطلب تحديث رسالة البطاقة مصادقة التطبيق. name
للرسالة المطلوب تعديلها.updateMask='cardsV2'
- تمثّل هذه السمة
body
التي تحدّد الرسالة المعدَّلة.
إذا كانت الرسالة المعدّلة عبارة عن رسالة نصية، سيتم إلحاق بطاقة بالرسالة النصية (ستستمر في عرضها). إذا كانت الرسالة المعدّلة هي بطاقة، يتم تعديل البطاقة المعروضة.
إليك كيفية تحديث رسالة إلى رسالة بطاقة:
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_update_card_message.py
. أدرِج الرمز التالي في
chat_update_card_message.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())) # 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)
في الرمز، استبدل ما يلي:
SPACE
: اسم مساحة، يمكنك الحصول عليه من طريقةspaces.list
في Chat API أو من عنوان URL لمساحة.MESSAGE
: اسم الرسالة الذي يمكنك الحصول عليه من نص الاستجابة الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_update_card_message.py
تعرض Chat API مثيل
Message
الذي يوضِّح الرسالة التي تم تعديلها.
تعديل رسالة باستخدام مسارات حقول متعدّدة في الوقت نفسه
عند تعديل رسالة، يمكنك تعديل مسارات متعددة لحقول الرسائل في
الوقت نفسه. على سبيل المثال، في طلب رسالة تعديل، يمكنك تحديد تغيير على مسارَي
text
وcardsv2
في الوقت نفسه، ما يؤدي إلى تعديل كل من
نص الرسالة وبطاقةها. إذا كانت الرسالة تتضمن نصًا فقط بدون بطاقة،
ستتم إضافة بطاقة إلى الرسالة. لمزيد من المعلومات حول مسارات الحقول المتوافقة،
اطّلِع على
مَعلمات updateMask
.
لتعديل كل من
text
و
card
في رسالة باستخدام
مصادقة المستخدم،
عليك اجتياز ما يلي في طلبك:
- نطاق التفويض
chat.messages
. name
للرسالة المطلوب تعديلها.تمثّل هذه السمة
updateMask
الذي يحدّد مسارات حقول الرسالة المطلوب تعديلها، مفصولة بفواصل:updateMask='text', 'cardsV2'
.تمثّل هذه السمة
body
التي تحدّد الرسالة المعدَّلة، بما في ذلك جميع مسارات الحقول المعدَّلة.
إليك كيفية تعديل مسارَي الحقلين text
وcardsV2
في رسالة باستخدام مصادقة المستخدم:
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_update_text_message_user.py
. أدرِج الرمز التالي في
chat_update_text_message_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 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 created membership. print(result) if __name__ == '__main__': main()
في الرمز، استبدل ما يلي:
SPACE
: اسم مساحة، يمكنك الحصول عليه من طريقةspaces.list
في Chat API أو من عنوان URL للمساحة.MESSAGE
: اسم الرسالة الذي يمكنك الحصول عليه من نص الاستجابة الذي يتم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API أو من خلال تحديد الاسم المخصّص للرسالة عند إنشائها
في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_update_text_message_user.py