รับข้อมูลเมตาเกี่ยวกับไฟล์แนบของข้อความ

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด get ในทรัพยากร Media ของ Google Chat API เพื่อรับข้อมูลเมตาเกี่ยวกับไฟล์แนบของข้อความ คำตอบคือ อินสแตนซ์ แหล่งข้อมูล Attachment

เมื่อผู้ใช้ส่งข้อความถึงแอปของคุณ 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
    

รับไฟล์แนบของข้อความ

หากต้องการรับข้อมูลเมตาแบบไม่พร้อมกันเกี่ยวกับไฟล์แนบของข้อความใน Google Chat ให้ส่งผ่าน ต่อไปนี้ในคำขอของคุณ

วิธีดูข้อมูลเมตาเกี่ยวกับไฟล์แนบของข้อความมีดังนี้

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_get_message_attachment.py
  2. รวมรหัสต่อไปนี้ใน chat_get_message_attachment.py:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Get a Chat message.
    result = chat.spaces().messages().attachments().get(
    
        # The message to get.
        #
        # 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/attachments/ATTACHMENT'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. ในโค้ด ให้แทนที่ spaces/SPACE/messages/MESSAGE/attachments/ATTACHMENT ด้วยชื่อไฟล์แนบของข้อความ

  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_get_message_attachment.py
    

Chat API จะแสดงผลอินสแตนซ์ Attachment ซึ่งแสดงรายละเอียดข้อมูลเมตาเกี่ยวกับไฟล์แนบของข้อความที่ระบุ