คำแนะนำนี้จะอธิบายวิธีใช้เมธอด get
ในทรัพยากร Message
ของ Google Chat API เพื่อแสดงรายละเอียดเกี่ยวกับข้อความหรือข้อความในการ์ด
แหล่งข้อมูล Message
แสดงถึงข้อความหรือข้อความบนการ์ดใน Google Chat คุณอาจcreate
, get
, update
หรือdelete
ข้อความใน Google Chat API ได้โดยเรียกใช้เมธอดที่เกี่ยวข้อง หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับ SMS และข้อความการ์ด โปรดดูภาพรวมข้อความ Google Chat
ข้อกำหนดเบื้องต้น
Python
- Python 3.6 ขึ้นไป
- เครื่องมือการจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การรับข้อความจะรองรับการตรวจสอบสิทธิ์ทั้ง 2 วิธีดังนี้
- การตรวจสอบสิทธิ์ผู้ใช้ซึ่งมีขอบเขตการให้สิทธิ์
chat.messages.readonly
หรือchat.messages
จะรับข้อความที่ผู้ใช้มีสิทธิ์เข้าถึงได้ - การตรวจสอบสิทธิ์แอปซึ่งมีขอบเขตการให้สิทธิ์
chat.bot
จะรับข้อความที่ส่งไปยังแอปได้
- การตรวจสอบสิทธิ์ผู้ใช้ซึ่งมีขอบเขตการให้สิทธิ์
รับข้อความที่มีการตรวจสอบสิทธิ์ผู้ใช้
หากต้องการดูรายละเอียดเกี่ยวกับข้อความที่มีการตรวจสอบสิทธิ์ผู้ใช้ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.messages.readonly
หรือchat.messages
- เรียกเมธอด
get
ในทรัพยากรMessage
- ตั้งค่า
name
เป็นชื่อทรัพยากรของข้อความที่จะรับ
ตัวอย่างต่อไปนี้ได้รับข้อความที่มีการตรวจสอบสิทธิ์ผู้ใช้
Python
- สร้างไฟล์ชื่อ
chat_message_get_user.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
chat_message_get_user.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 gets a 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. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().messages().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' ).execute() # Prints details about the message. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่ส่วนต่อไปนี้
SPACE
: ชื่อพื้นที่ทำงาน ซึ่งคุณจะดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานMESSAGE
: ชื่อข้อความที่คุณจะได้รับจากเนื้อหาการตอบกลับที่แสดงหลังจากสร้างข้อความแบบไม่พร้อมกันด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความเมื่อสร้าง
สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_message_get_user.py
Chat API จะส่งคืนอินสแตนซ์ของ Message
ซึ่งแสดงรายละเอียดของข้อความที่ระบุ
รับข้อความที่มีการตรวจสอบสิทธิ์แอป
หากต้องการดูรายละเอียดเกี่ยวกับข้อความด้วยการตรวจสอบสิทธิ์แอป ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.bot
- เรียกเมธอด
get
ในทรัพยากรMessage
- ตั้งค่า
name
เป็นชื่อทรัพยากรของข้อความที่จะรับ
ตัวอย่างต่อไปนี้ได้รับข้อความที่มีการตรวจสอบสิทธิ์แอป
Python
- สร้างไฟล์ชื่อ
chat_get_message_app.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
chat_get_message_app.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().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' ).execute() # Print Chat API's response in your command line interface. print(result)
ในโค้ด ให้แทนที่ส่วนต่อไปนี้
SPACE
:name
ของพื้นที่ทำงานที่มีการโพสต์ข้อความ ซึ่งคุณจะรับจากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานก็ได้MESSAGE
: ชื่อข้อความที่คุณจะได้รับจากเนื้อหาการตอบกลับที่แสดงหลังจากสร้างข้อความแบบไม่พร้อมกันด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความเมื่อสร้าง
สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_get_message_app.py
Chat API จะส่งคืนอินสแตนซ์ของ Message
ซึ่งแสดงรายละเอียดของข้อความที่ระบุ