मीडिया को फ़ाइल अटैचमेंट के तौर पर अपलोड करें

यह गाइड बताती है कि इसके Media संसाधन पर upload तरीके का इस्तेमाल कैसे किया जाए Google Chat API का इस्तेमाल करके, मीडिया (फ़ाइल) को Google Chat में अपलोड करें और फिर उसे एक मैसेज.

जब उपयोगकर्ता आपके ऐप्लिकेशन पर मैसेज भेजता है, तो Google Chat उसे MESSAGE इंटरैक्शन इवेंट. आपके ऐप्लिकेशन को मिले इंटरैक्शन इवेंट में, अनुरोध का मुख्य हिस्सा शामिल होता है, जो JSON पेलोड, जिसमें इंटरैक्शन इवेंट दिखाया गया है. इसमें अटैचमेंट भी शामिल है. कॉन्टेंट बनाने में डेटा इस बात पर निर्भर करता है कि अटैचमेंट अपलोड किया गया कॉन्टेंट (कोई लोकल फ़ाइल) या Drive पर सेव की गई कोई फ़ाइल. कॉन्टेंट बनाने Media संसाधन Google Chat पर अपलोड की गई फ़ाइल को दिखाता है, जैसे कि इमेज, वीडियो, और दस्तावेज़. कॉन्टेंट बनाने Attachment संसाधन मीडिया के एक इंस्टेंस को दर्शाता है—एक फ़ाइल—जो किसी मैसेज से अटैच की जाती है. Attachment संसाधन में अटैचमेंट के बारे में मेटाडेटा शामिल होता है, जैसे जहां उन्हें सेव किया जाता है.

ज़रूरी शर्तें

Python

  • कारोबार या एंटरप्राइज़ Google Workspace खाता, जिसके पास इसका ऐक्सेस है Google Chat.
  • Python 3.6 या इससे नया वर्शन
  • पीआईपी पैकेज मैनेजमेंट टूल
  • Google की नई क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल या अपडेट करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में यह कमांड चलाएं:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

फ़ाइल अटैचमेंट के तौर पर अपलोड करें

मीडिया अपलोड करने और उसे किसी मैसेज में अटैच करने के लिए, आपको अनुरोध::

  • 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 तरीका या स्पेस के यूआरएल से मिलेगी.

  4. अपनी वर्किंग डायरेक्ट्री में, सैंपल बनाएं और चलाएं:

    python3 chat_media_and_attachment_upload.py
    

Chat API, जवाब का मुख्य हिस्सा दिखाता है attachmentDataRef, जिसमें अपलोड की गई फ़ाइल की जानकारी है.

सीमाएं और ज़रूरी बातें

फ़ाइलों को अपलोड करने और उन्हें मैसेज में अटैच करने के लिए, इन बातों का ध्यान रखें सीमाएं और विचार:

  • ज़्यादा से ज़्यादा 200 एमबी की फ़ाइल अपलोड की जा सकती है.
  • कुछ फ़ाइल टाइप काम नहीं करते और उन्हें अपलोड नहीं किया जा सकता. जानकारी के लिए, यह देखें Google Chat में ब्लॉक किए गए फ़ाइल टाइप.