คำแนะนำนี้จะอธิบายวิธีใช้เมธอด 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-oauthlib google-auth
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การค้นหาข้อความส่วนตัว จะรองรับทั้ง 2 อย่างต่อไปนี้
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
chat.spaces.readonly
หรือchat.spaces
- การตรวจสอบสิทธิ์แอป
ด้วยขอบเขตการให้สิทธิ์
chat.bot
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
Node.js
- Node.js และ npm
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Node.js หากต้องการติดตั้ง ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
npm install @google-cloud/local-auth @googleapis/chat
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป 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
: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.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 direct message. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
USER
ด้วยname
ของUser
ใน Google Chatสร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_space_find_dm_user.py
Node.js
สร้างไฟล์ชื่อ
find-direct-message-space.js
ในไดเรกทอรีการทำงานใส่รหัสต่อไปนี้ใน
find-direct-message-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Find a direct message Chat space for a user. * @return {!Promise<!Object>} */ async function findDirectMessageSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces.readonly', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.findDirectMessage( {name: 'users/USER'}); } findDirectMessageSpace().then(console.log);
ในโค้ด ให้แทนที่
USER
ด้วยname
ของUser
ใน Google Chatเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
node find-direct-message-space.js
Chat API จะส่งคืนอินสแตนซ์ของ Space
ที่มีรายละเอียด DM ที่ระบุ
ค้นหาข้อความส่วนตัวที่มีการตรวจสอบสิทธิ์แอป
วิธีค้นหาข้อความส่วนตัวด้วยการตรวจสอบสิทธิ์แอป
Python
- สร้างไฟล์ชื่อ
chat_space_find_dm_app.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
chat_space_find_dm_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) # 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
Node.js
สร้างไฟล์ชื่อ
app-find-direct-message-space.js
ในไดเรกทอรีการทำงานใส่รหัสต่อไปนี้ใน
app-find-direct-message-space.js
:const chat = require('@googleapis/chat'); /** * Find a direct message Chat space for a user. * @return {!Promise<!Object>} */ async function findDirectMessageSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.bot', ]; const auth = new chat.auth.GoogleAuth({ scopes, keyFilename: 'credentials.json', }); const authClient = await auth.getClient(); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.findDirectMessage( {name: 'users/USER'}); } findDirectMessageSpace().then(console.log);
ในโค้ด ให้แทนที่
USER
ด้วยname
ของUser
ใน Google Chatเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
node app-find-direct-message-space.js
Chat API จะส่งคืนอินสแตนซ์ของ Space
ที่มีรายละเอียด DM ที่ระบุ
หัวข้อที่เกี่ยวข้อง
- สร้างพื้นที่ทำงาน
- ตั้งค่าพื้นที่ทำงาน
- ดูรายละเอียดเกี่ยวกับพื้นที่ทำงาน
- แสดงรายการพื้นที่ทำงาน
- อัปเดตพื้นที่ทำงาน
- ลบพื้นที่ทำงาน