يوضّح هذا الدليل كيفية استخدام طريقة upload في مورد Media ضمن Google Chat API لتحميل وسائط (ملف) إلى Google Chat ثم إرفاقها برسالة.
عندما يرسل المستخدم رسالة إلى تطبيقك، يرسل Google Chat
حدث تفاعل MESSAGE.
يتضمّن حدث التفاعل الذي يتلقّاه تطبيقك نص طلب، وهو حمولة JSON التي تمثّل حدث التفاعل، بما في ذلك أي مرفقات. تختلف البيانات في المرفق حسب ما إذا كان المرفق عبارة عن محتوى تم تحميله (ملف محلي) أو ملف مخزّن على Drive. يمثّل
Media المورد
ملفًا تم تحميله إلى Google Chat، مثل الصور والفيديوهات والمستندات.
يمثّل المورد
Attachment
مثيلاً لوسائط، أي ملف، مرفق برسالة. يتضمّن المرجع Attachment
البيانات الوصفية حول المرفق، مثل
مكان حفظه.
المتطلبات الأساسية
Python
- حساب Google Workspace Business أو Enterprise مع إمكانية الوصول إلى Google Chat
- إعداد البيئة:
- أنشئ مشروعًا على Google Cloud.
- ضبط شاشة طلب الموافقة المتعلّقة ببروتوكول OAuth
- فعِّل واجهة Google Chat API وأعِدّها من خلال إضافة اسم ورمز ووصف لتطبيق Chat.
- ثبِّت مكتبة عميل Google API للغة Python.
-
إنشاء بيانات اعتماد معرِّف عميل OAuth لتطبيق على جهاز كمبيوتر لتشغيل النموذج في هذا الدليل، احفظ بيانات الاعتماد كملف JSON باسم
credentials.jsonفي الدليل المحلي.
- اختَر نطاق تفويض يتيح مصادقة المستخدم.
تحميل الملف كمرفق
لتحميل وسائط وإرفاقها برسالة، أدرِج ما يلي في طلبك:
- حدِّد نطاق تفويض
chat.messages.createأوchat.messages. - استدعاء طرق Google Chat التالية:
- لتحميل الملف، استدعِ الطريقة
uploadفي الموردMedia.- اضبط
parentعلى اسم المساحة التي تستضيف الملف. - في
body(نص الطلب)، اضبطfilenameعلى اسم مرفق الملف الذي تم تحميله. - اضبط
media_bodyكمثيل للملف الذي سيتم تحميله.
- اضبط
- لإنشاء رسالة مع إرفاق الملف الذي تم تحميله، استدعِ الطريقة
createفيMessages.
- لتحميل الملف، استدعِ الطريقة
يحمّل المثال التالي ملف صورة بتنسيق PNG ويرفقه برسالة.
Python
- في دليل العمل، أنشئ ملفًا باسم
chat_media_and_attachment_upload.py. أدرِج الرمز التالي في
chat_media_and_attachment_upload.py:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload # 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.create"] def main(): ''' Authenticates with Chat API via user credentials, then uploads a file as media, creates a message, and attaches the file to the message. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. service = build('chat', 'v1', credentials=creds) # Upload a file to Google Chat. media = MediaFileUpload('test_image.png', mimetype='image/png') # Create a message and attach the uploaded file to it. attachment_uploaded = service.media().upload( # The space to upload the attachment in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # The filename of the attachment, including the file extension. body={'filename': 'test_image.png'}, # Media resource of the attachment. media_body=media ).execute() print(attachment_uploaded) # Create a Chat message with attachment. result = service.spaces().messages().create( # The space to create the message in. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Must match the space name that the attachment is uploaded to. parent='spaces/SPACE', # The message to create. body={ 'text': 'Hello, world!', 'attachment': [attachment_uploaded] } ).execute() print(result) if __name__ == '__main__': main()في الرمز، استبدِل
SPACEباسم المساحة التي تريد تحميل المرفق فيها، ويمكنك الحصول على هذا الاسم من خلال الطريقةspaces.listفي Chat API أو من عنوان URL الخاص بمساحة.في دليل العمل، أنشئ النموذج وشغِّله:
python3 chat_media_and_attachment_upload.py
تعرض Chat API نص استجابة يحتوي على attachmentDataRef مع تفاصيل حول الملف الذي تم تحميله.
الحدود والاعتبارات
عند الاستعداد لتحميل الملفات وإرفاقها بالرسائل، يُرجى الانتباه إلى الحدود والاعتبارات التالية:
- يمكنك تحميل ملفات يصل حجمها إلى 200 ميغابايت.
- بعض أنواع الملفات غير متوافقة ولا يمكن تحميلها. لمعرفة التفاصيل، يُرجى الاطّلاع على أنواع الملفات المحظورة في Google Chat.
- يجب أن تحذف رسالتك التطبيقات المصغّرة الخاصة بالملحقات.