تحميل الوسائط كمرفق ملف

يوضّح هذا الدليل طريقة استخدام طريقة upload على مورد Media لواجهة Google Chat API لتحميل وسائط (ملف) إلى Google Chat ثم إرفاقها برسالة.

عندما يُرسِل المستخدم رسالة إلى تطبيقك، يُرسِل Google Chat حدث تفاعل MESSAGE. يتضمّن حدث التفاعل الذي يتلقّاه تطبيقك نص الطلب، وهو حمولة JSON التي تمثِّل حدث التفاعل، بما في ذلك أي مرفقات. وتختلف البيانات الواردة في المرفق بناءً على ما إذا كان المرفق قد تم تحميله محتوى (ملف محلي) أو ملفًا مخزّنًا على Drive. يمثل مورد Media ملفًا تم تحميله إلى Google Chat، مثل الصور والفيديوهات والمستندات. يمثّل مورد Attachment مثيلاً من الوسائط، أي ملف، مرفق برسالة. يتضمن مورد Attachment البيانات الوصفية حول المرفق، مثل مكان حفظه.

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

Python

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

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • مشروع على Google Cloud تم فيه تفعيل Google Chat API وضبطه لمعرفة الخطوات، يُرجى الاطّلاع على مقالة إنشاء تطبيق Google Chat.
  • تم إعداد التفويض لتطبيق Chat. لتحميل الوسائط كمرفق ملف، يجب مصادقة المستخدم مع نطاق تفويض chat.messages.create أو chat.messages.

التحميل كملف مرفق

لتحميل الوسائط وإرفاقها برسالة، عليك تقديم ما يلي في الطلب:

  • حدِّد نطاق التفويض chat.messages.create أو chat.messages.
  • يمكنك استدعاء طرق Google Chat التالية:
    1. لتحميل الملف، عليك استدعاء طريقة upload في المورد Media.
      • اضبط parent على اسم المساحة التي تستضيف الملف.
      • في body (نص الطلب)، اضبط filename على اسم مرفق الملف الذي تم تحميله.
      • اضبط media_body كمثيل للملف المطلوب تحميله.
    2. لإنشاء رسالة مع إرفاق الملف الذي تم تحميله، يمكنك استدعاء طريقة create في مورد Messages.
      • اضبط attachment كاستجابة من استدعاء طريقة upload على Media. ويقبل الحقل attachment القائمة.

يتم في المثال التالي تحميل ملف صورة بتنسيق PNG وإرفاقه برسالة.

Python

  1. في دليل العمل، أنشِئ ملفًا باسم chat_media_and_attachment_upload.py.
  2. ضمِّن الرمز التالي في 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(
                      'client_secrets.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()
    
  3. في الرمز، استبدِل SPACE باسم المساحة المطلوب تحميل المرفق فيها، والذي يمكنك الحصول عليه من طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة.

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

    python3 chat_media_and_attachment_upload.py
    

تعرض Chat API نص استجابة يتضمّن attachmentDataRef مع تفاصيل حول الملف الذي تم تحميله.

الحدود والاعتبارات

أثناء الاستعداد لتحميل الملفات وإرفاقها بالرسائل، يُرجى مراعاة هذه الحدود والاعتبارات:

  • يمكنك تحميل حجم ملفات يصل إلى 200 ميغابايت.
  • بعض أنواع الملفات غير متوافقة ولا يمكن تحميلها. لمعرفة التفاصيل، يُرجى الاطّلاع على القسم أنواع الملفات المحظورة في Google Chat.