คู่มือนี้อธิบายวิธีใช้เมธอด upload ในทรัพยากร Media ของ Google Chat API เพื่ออัปโหลดสื่อ (ไฟล์) ไปยัง Google Chat แล้วแนบสื่อดังกล่าวกับข้อความ
เมื่อผู้ใช้ส่งข้อความไปยังแอป Google Chat จะส่งเหตุการณ์การโต้ตอบ
MESSAGE
เหตุการณ์การโต้ตอบที่แอปได้รับจะมีเนื้อหาคำขอ ซึ่งเป็นเพย์โหลด JSON ที่แสดงเหตุการณ์การโต้ตอบ รวมถึงไฟล์แนบต่างๆ ข้อมูลในไฟล์แนบจะแตกต่างกันไปตามประเภทของไฟล์แนบ ไม่ว่าจะเป็นเนื้อหาที่อัปโหลด (ไฟล์ในเครื่อง) หรือไฟล์ที่จัดเก็บไว้ในไดรฟ์ ทรัพยากร
Mediaแสดงถึงไฟล์ที่อัปโหลดไปยัง Google Chat เช่น รูปภาพ วิดีโอ และเอกสาร
ทรัพยากร
Attachmentแสดงถึงอินสแตนซ์ของสื่อ ซึ่งก็คือไฟล์ที่แนบกับข้อความ
ทรัพยากร Attachment จะมีข้อมูลเมตาเกี่ยวกับไฟล์แนบ เช่น ตำแหน่งที่บันทึกไฟล์
ข้อกำหนดเบื้องต้น
Python
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ 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ทรัพยากร- ตั้งค่า
attachmentเป็นการตอบกลับจากการเรียกใช้uploadเมธอด ในMediaทรัพยากร ช่องattachmentรับรายการ
- ตั้งค่า
- หากต้องการอัปโหลดไฟล์ ให้เรียกใช้
ตัวอย่างต่อไปนี้จะอัปโหลดไฟล์รูปภาพ PNG และแนบไฟล์ดังกล่าวกับข้อความ
Python
- สร้างไฟล์ชื่อ
chat_media_and_attachment_upload.pyในไดเรกทอรีงาน ใส่โค้ดต่อไปนี้ใน
chat_media_and_attachment_upload.pyfrom 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 MB ได้
- ระบบไม่รองรับไฟล์บางประเภทและอัปโหลดไฟล์ดังกล่าวไม่ได้ ดูรายละเอียดได้ที่ ประเภทไฟล์ที่ถูกบล็อกใน Google Chat
- ข้อความต้องไม่มี วิดเจ็ตเสริม