คู่มือนี้จะอธิบายวิธีใช้เมธอด updateSpaceReadState
ใน
ทรัพยากร SpaceReadState
ของ Google Chat API เพื่อทำเครื่องหมายพื้นที่ทำงานว่าอ่านแล้วหรือยังไม่อ่าน
แหล่งข้อมูล SpaceReadState
รายการ
คือทรัพยากรแบบ Singleton ที่แสดงรายละเอียดเกี่ยวกับ
ข้อความที่อ่านล่าสุดของผู้ใช้ที่ระบุในพื้นที่ใน Google Chat
ข้อกำหนดเบื้องต้น
Python
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API โดยใช้ชื่อ ไอคอนและคำอธิบายสำหรับแอป Chat
- ติดตั้ง งูหลาม ไลบรารีของไคลเอ็นต์ Google API
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON ชื่อ
client_secrets.json
ลงในไฟล์ ไดเรกทอรีในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
Node.js
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API โดยใช้ชื่อ ไอคอนและคำอธิบายสำหรับแอป Chat
- ติดตั้ง Node.js ไลบรารีของไคลเอ็นต์ Google API
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON ชื่อ
client_secrets.json
ลงในไฟล์ ไดเรกทอรีในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
Apps Script
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API โดยใช้ชื่อ ไอคอนและคำอธิบายสำหรับแอป Chat
- สร้างโปรเจ็กต์ Apps Script แบบสแตนด์อโลน แล้วเปิดบริการ Chat ขั้นสูง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
อัปเดตสถานะการอ่านพื้นที่ทำงานของผู้ใช้ที่โทร
ในการอัปเดตสถานะการอ่านของผู้ใช้ภายในพื้นที่ทำงาน ให้ใส่ข้อมูลต่อไปนี้ใน คำขอของคุณ:
- ระบุขอบเขตการให้สิทธิ์
chat.users.readstate
- เรียกใช้
updateSpaceReadState
วิธี ใน แหล่งข้อมูลSpaceReadState
- ส่ง
name
ของสถานะการอ่านพื้นที่ทำงานเพื่อรับ ซึ่งประกอบด้วยรหัสผู้ใช้หรือ ชื่อแทนและรหัสพื้นที่ทำงาน การรับสถานะการอ่านพื้นที่ทำงานรองรับเฉพาะการรับการอ่าน สถานะของผู้ใช้ที่โทร ซึ่งสามารถระบุได้โดยการตั้งค่าอย่างใดอย่างหนึ่ง ดังต่อไปนี้:- ชื่อแทน
me
ตัวอย่างเช่นusers/me/spaces/SPACE/spaceReadState
- อีเมล Workspace ของผู้ใช้ที่โทร ตัวอย่างเช่น
users/user@example.com/spaces/SPACE/spaceReadState
- รหัสผู้ใช้ของผู้ใช้ที่โทร ตัวอย่างเช่น
users/USER/spaces/SPACE/spaceReadState
- ชื่อแทน
- ส่ง
updateMask
ซึ่งระบุแง่มุมต่างๆ ของสถานะการอ่านพื้นที่ทำงานสำหรับ อัปเดต ซึ่งรองรับเส้นทางฟิลด์ต่อไปนี้:lastReadTime
: เวลาที่อัปเดตสถานะการอ่านพื้นที่ทำงานของผู้ใช้ โดยปกติแล้วจะสอดคล้องกับการประทับเวลาของข้อความที่อ่านล่าสุด หรือการประทับเวลาที่ผู้ใช้ระบุเพื่อทำเครื่องหมายตำแหน่งที่อ่านล่าสุดใน พื้นที่ทำงาน เมื่อlastReadTime
อยู่ก่อนเวลาสร้างข้อความล่าสุด พารามิเตอร์ จะปรากฏว่ายังไม่อ่านใน UI หากต้องการทำเครื่องหมายพื้นที่ทำงานว่าอ่านแล้ว ให้ตั้งค่าlastReadTime
เป็นค่าใดๆ ก็ได้ในภายหลัง (มากกว่า) ที่สร้างข้อความล่าสุด มีการเปลี่ยนlastReadTime
ให้ตรงกับเวลาที่สร้างข้อความล่าสุด โปรดทราบว่าสถานะอ่านของพื้นที่ทำงานจะมีผลกับสถานะอ่านของข้อความที่ จะปรากฏในการสนทนาระดับบนสุดของพื้นที่ทำงาน การตอบกลับในชุดข้อความคือ จะไม่ได้รับผลกระทบจากการประทับเวลานี้ และอาศัยสถานะการอ่านของชุดข้อความแทน
ตัวอย่างต่อไปนี้อัปเดตสถานะการอ่านพื้นที่ทำงานของผู้ใช้ที่เรียกใช้
Python
- ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ
chat_spaceReadState_update.py
รวมรหัสต่อไปนี้ใน
chat_spaceReadState_update.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.users.readstate"] def main(): ''' Authenticates with Chat API via user credentials, then updates the space read state for the calling user. ''' # 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.users().spaces().updateSpaceReadState( # The space read state to update. # # Replace USER with the calling user's ID, Workspace email, # or the alias me. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. name='users/me/spaces/SPACE/spaceReadState', updateMask='lastReadTime', body={'lastReadTime': f'{datetime.datetime(2000, 1, 3).isoformat()}Z'} ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
SPACE
: ชื่อพื้นที่ทำงาน ที่คุณจะได้รับจากspaces.list
วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้
python3 chat_spaceReadState_update.py
Node.js
- ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ
chat_spaceReadState_update.js
รวมรหัสต่อไปนี้ใน
chat_spaceReadState_update
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @return {!Promise<!Object>} */ async function updateSpaceReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); /** * Build a service endpoint for Chat API. */ const chatClient = await chat.chat({version: 'v1', auth: authClient}); /** * Use the service endpoint to call Chat API. */ return await chatClient.users.spaces.updateSpaceReadState({ /** * The space read state to update. * * Replace USER with the calling user's ID, Workspace email, * or the alias me. * * Replace SPACE with a space name. * Obtain the space name from the spaces resource of Chat API, * or from a space's URL. */ name: 'users/me/spaces/SPACE/spaceReadState', updateMask: 'lastReadTime', requestBody: { lastReadTime: '{datetime.datetime(2000, 1, 3).isoformat()}Z' } }); } /** * Use the service endpoint to call Chat API. */ getSpaceReadState().then(console.log);
ในโค้ด ให้แทนที่
SPACE
: ชื่อพื้นที่ทำงาน ที่คุณจะได้รับจากspaces.list
วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้
node chat_spaceReadState_update.js
Apps Script
ตัวอย่างนี้เรียกใช้ Chat API โดยใช้ บริการ Chat ขั้นสูง
เพิ่มขอบเขตการให้สิทธิ์
chat.users.readstate
ไปยัง ไฟล์appsscript.json
ของโครงการ Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate" ]
เพิ่มฟังก์ชันเช่นนี้ลงใน รหัส:
/** * Authenticates with Chat API via user credentials, * then updates the space read state for the calling user. * @param {string} spaceReadStateName The resource name of the space read state. */ function updateSpaceReadState(spaceReadStateName) { try { const time = new Date('January 1, 2000')).toJSON(); const body = {'lastReadTime': time}; Chat.Users.Spaces.updateSpaceReadState(spaceReadStateName, body); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to update read state with error %s', err.message); } }
Google Chat API จะอัปเดตสถานะการอ่านและการส่งคืนพื้นที่ทำงานที่ระบุ
ตัวอย่าง
แหล่งข้อมูล SpaceReadState