คู่มือนี้อธิบายวิธีใช้เมธอด findDirectMessage
ในทรัพยากร Space
ของ Google Chat API เพื่อดูรายละเอียดเกี่ยวกับพื้นที่ข้อความส่วนตัว (DM)
แหล่งข้อมูล Space
เป็นพื้นที่ที่ผู้คนและแอป Chat จะใช้ส่งข้อความ แชร์ไฟล์ และทำงานร่วมกันได้ พื้นที่ทำงานมีหลายประเภทดังนี้
- ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คนกับผู้ใช้กับแอป Chat
- การแชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปกับแอป Chat
- พื้นที่ทำงานที่มีชื่อเป็นพื้นที่ถาวรที่ผู้คนจะส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน
การตรวจสอบสิทธิ์ด้วย การตรวจสอบสิทธิ์แอป ช่วยให้แอป Chat ได้รับ DM ที่แอป Chat มีสิทธิ์เข้าถึงใน Google Chat (เช่น DM ที่แอปเป็นสมาชิก) การตรวจสอบสิทธิ์ด้วยการตรวจสอบสิทธิ์ผู้ใช้จะแสดงผล DM ที่ผู้ใช้ที่ตรวจสอบสิทธิ์แล้วมีสิทธิ์เข้าถึง
ข้อกำหนดเบื้องต้น
Python
- Python 3.6 ขึ้นไป
- เครื่องมือการจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้างและเผยแพร่ แอป Chat โปรดดูที่ สร้างแอป Google Chat
มีการกำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การค้นหาข้อความส่วนตัวจะ รองรับทั้ง 2 อย่างต่อไปนี้
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
chat.spaces.readonly
หรือchat.spaces
- การตรวจสอบสิทธิ์แอป
ด้วยขอบเขตการให้สิทธิ์
chat.bot
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
ค้นหาข้อความส่วนตัว
หากต้องการค้นหาข้อความส่วนตัวใน Google Chat ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุขอบเขตการให้สิทธิ์
chat.bot
เมื่อใช้การตรวจสอบสิทธิ์ผู้ใช้ ให้ระบุขอบเขตการให้สิทธิ์chat.spaces.readonly
หรือchat.spaces
- เรียกใช้เมธอด
findDirectMessage
ในทรัพยากรUser
โดยส่งname
ของผู้ใช้รายอื่นใน DM เพื่อส่งคืน เมื่อใช้การตรวจสอบสิทธิ์ผู้ใช้ วิธีนี้จะแสดงผล DM ระหว่างผู้ใช้ที่เรียกใช้กับผู้ใช้ที่ระบุ เมื่อใช้การตรวจสอบสิทธิ์แอป การดำเนินการนี้จะแสดงผล DM ระหว่างแอปการโทรและผู้ใช้ที่ระบุ - หากต้องการเพิ่มผู้ใช้ที่เป็นมนุษย์เป็นสมาชิกพื้นที่ทำงาน ให้ระบุ
users/{user}
โดยที่{user}
จะเป็น{person_id}
สำหรับperson
จาก People API หรือรหัสของuser
ใน Directory API เช่น หากบุคคล API ของresourceName
คือpeople/123456789
คุณสามารถเพิ่มผู้ใช้ไปยังพื้นที่ทำงานได้โดยการรวมสมาชิกที่มีusers/123456789
เป็นmember.name
ค้นหาข้อความส่วนตัวที่มีการตรวจสอบสิทธิ์ของผู้ใช้
วิธีค้นหาข้อความส่วนตัวด้วยการตรวจสอบสิทธิ์ผู้ใช้
Python
- ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ
chat_space_find_dm_user.py
ใส่รหัสต่อไปนี้ใน
chat_space_find_dm_user.py
:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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.spaces.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then returns details about a specified DM. ''' # 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().findDirectMessage( # The other user in the direct message (DM) to return. # # Replace USER with a user name. name='users/USER' ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
USER
ด้วยname
ของUser
ใน Google Chatในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_space_find_dm_user.py
Chat API จะส่งคืนอินสแตนซ์ของ Space
ที่มีรายละเอียด DM ที่ระบุ
ค้นหาข้อความส่วนตัวที่มีการตรวจสอบสิทธิ์แอป
วิธีค้นหาข้อความส่วนตัวด้วยการตรวจสอบสิทธิ์แอป
Python
- ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ
chat_space_find_dm_app.py
ใส่รหัสต่อไปนี้ใน
chat_space_find_dm_app.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Use the service endpoint to call Chat API. result = chat.spaces().findDirectMessage( # The other user in the direct message (DM) to return. # # Replace USER with a user name. name='users/USER' ).execute() print(result)
ในโค้ด ให้แทนที่
USER
ด้วยname
ของUser
ใน Google Chatในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_space_find_dm_app.py
Chat API จะส่งคืนอินสแตนซ์ของ Space
ที่มีรายละเอียด DM ที่ระบุ
หัวข้อที่เกี่ยวข้อง
- สร้างพื้นที่ทำงาน
- ตั้งค่าพื้นที่ทำงาน
- ดูรายละเอียดเกี่ยวกับพื้นที่ทำงาน
- แสดงพื้นที่ทำงาน
- อัปเดตพื้นที่ทำงาน
- ลบพื้นที่ทำงาน