อัปโหลดสื่อเป็นไฟล์แนบ

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด upload ในแหล่งข้อมูล Media ของ Google Chat API เพื่ออัปโหลดสื่อ (ไฟล์) ไปยัง Google Chat แล้วแนบไปกับ ข้อความ

เมื่อผู้ใช้ส่งข้อความถึงแอปของคุณ Google Chat จะส่ง เหตุการณ์การโต้ตอบ MESSAGE เหตุการณ์ เหตุการณ์การโต้ตอบที่แอปของคุณได้รับจะมีเนื้อหาของคำขอ ซึ่งก็คือ เพย์โหลด JSON ที่แสดงเหตุการณ์การโต้ตอบ รวมถึงไฟล์แนบอื่นๆ ข้อมูลในไฟล์แนบจะแตกต่างกันไป ขึ้นอยู่กับว่าไฟล์แนบนั้น เนื้อหาที่อัปโหลด (ไฟล์ในเครื่อง) หรือไฟล์ที่เก็บไว้ในไดรฟ์ แหล่งข้อมูล Media รายการ แสดงไฟล์ที่อัปโหลดไปยัง Google Chat เช่น รูปภาพ วิดีโอ และเอกสาร แหล่งข้อมูล Attachment รายการ หมายถึงอินสแตนซ์ของสื่อ ซึ่งก็คือไฟล์ที่แนบมากับข้อความ Attachment จะมีข้อมูลเมตาเกี่ยวกับไฟล์แนบ เช่น ตำแหน่งที่บันทึกไว้

ข้อกำหนดเบื้องต้น

Python

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Python 3.6 ขึ้นไป
  • เครื่องมือจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ 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

ตัวอย่างต่อไปนี้จะอัปโหลดไฟล์รูปภาพ 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 พร้อมรายละเอียดเกี่ยวกับไฟล์ที่อัปโหลด

ข้อจำกัดและข้อควรพิจารณา

ให้คำนึงถึงสิ่งต่อไปนี้ขณะเตรียมอัปโหลดไฟล์และแนบไฟล์ไปกับข้อความ ข้อจำกัดและข้อควรพิจารณา