คำแนะนำนี้จะอธิบายวิธีใช้เมธอด list
ในแหล่งข้อมูล Message
ของ
Google Chat API เพื่อดูรายการข้อความที่ใส่เลขหน้าและสามารถกรองได้ในพื้นที่ทำงาน
แหล่งข้อมูล Message
รายการ
แสดงถึง
ข้อความ
หรือ
การ์ด
ใน Google Chat คุณสามารถ
create
, get
, update
หรือ delete
ข้อความใน Google Chat API โดยการเรียกใช้
ที่สัมพันธ์กัน หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับข้อความ SMS และการ์ด โปรดดูที่
ภาพรวมข้อความ Google Chat
ข้อกำหนดเบื้องต้น
Python
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- Python 3.6 ขึ้นไป
- เครื่องมือจัดการแพ็กเกจ pip
- ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย
เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API โดยใช้ชื่อ ไอคอนและคำอธิบายสำหรับแอป Chat ของคุณ
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON (
client_secrets.json
) ลงใน ไดเรกทอรีในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
แสดงรายการข้อความ
หากต้องการแสดงข้อความที่มี การตรวจสอบสิทธิ์ผู้ใช้ ส่งต่อสิ่งต่อไปนี้ในคำขอของคุณ
- ระบุขอบเขตการให้สิทธิ์
chat.messages.readonly
หรือchat.messages
- เรียกใช้
list
วิธี ใน แหล่งข้อมูลMessage
ตัวอย่างต่อไปนี้แสดงข้อความในพื้นที่ใน Chat ที่ส่งหลังจาก 16 มีนาคม 2023
Python
- สร้างไฟล์ชื่อ
chat_messages_list.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_messages_list.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # 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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists messages in a space sent after March 16, 2023. ''' # 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. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().messages().list( # The space for which to list messages. parent = 'spaces/SPACE', # An optional filter that returns messages # created after March 16, 2023. filter = 'createTime > "2023-03-16T00:00:00-00:00"' ).execute() # Prints the list of messages. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงาน ซึ่ง ที่คุณจะได้รับจากspaces.list
วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงานในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้
python3 chat_messages_list.py
Google Chat API จะแสดงรายการข้อความที่ส่งในพื้นที่ทำงานที่ระบุหลังจาก 16 มีนาคม 2023