Hướng dẫn này giải thích cách sử dụng phương thức getThreadReadState
trên
Tài nguyên ThreadReadState
của API Google Chat để xem thông tin chi tiết về
trạng thái đọc trong một chuỗi thư. Để biết trạng thái đã đọc của thư trong
không gian, xem
Xem thông tin chi tiết về trạng thái đọc không gian của người dùng.
Chiến lược phát hành đĩa đơn
Tài nguyên ThreadReadState
là một tài nguyên singleton biểu thị thông tin chi tiết về một
tin nhắn đọc gần đây nhất của người dùng đã chỉ định trong chuỗi tin nhắn trên Google Chat.
Điều kiện tiên quyết
Python
- Doanh nghiệp Tài khoản Google Workspace có quyền truy cập vào Google Chat.
- Thiết lập môi trường:
- Tạo một dự án trên Google Cloud.
- Định cấu hình màn hình xin phép bằng OAuth.
- Bật và định cấu hình API Google Chat bằng tên, biểu tượng và nội dung mô tả cho ứng dụng Chat.
- Cài đặt Python Thư viện ứng dụng API của Google.
-
Tạo thông tin xác thực mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong
hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên
client_secrets.json
vào thư mục nội bộ.
- Chọn phạm vi uỷ quyền có hỗ trợ việc xác thực người dùng.
Node.js
- Doanh nghiệp Tài khoản Google Workspace có quyền truy cập vào Google Chat.
- Thiết lập môi trường:
- Tạo một dự án trên Google Cloud.
- Định cấu hình màn hình xin phép bằng OAuth.
- Bật và định cấu hình API Google Chat bằng tên, biểu tượng và nội dung mô tả cho ứng dụng Chat.
- Cài đặt Node.js Thư viện ứng dụng API của Google.
-
Tạo thông tin xác thực mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong
hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên
client_secrets.json
vào thư mục nội bộ.
- Chọn phạm vi uỷ quyền có hỗ trợ việc xác thực người dùng.
Apps Script
- Doanh nghiệp Tài khoản Google Workspace có quyền truy cập vào Google Chat.
- Thiết lập môi trường:
- Tạo một dự án trên Google Cloud.
- Định cấu hình màn hình xin phép bằng OAuth.
- Bật và định cấu hình API Google Chat bằng tên, biểu tượng và nội dung mô tả cho ứng dụng Chat.
- Tạo một dự án Apps Script độc lập, rồi bật Dịch vụ trò chuyện nâng cao.
- Chọn phạm vi uỷ quyền có hỗ trợ việc xác thực người dùng.
Nhận trạng thái đọc chuỗi của người dùng đang gọi
Để biết thông tin chi tiết về trạng thái đọc của người dùng trong một chuỗi thư, hãy thêm sau đây trong yêu cầu của bạn:
- Chỉ định
chat.users.readstate
hoặcchat.users.readstate.readonly
phạm vi uỷ quyền. - Gọi
Phương thức
getThreadReadState
trênThreadReadState
tài nguyên. - Truyền
name
của trạng thái đã đọc chuỗi để lấy, bao gồm cả mã nhận dạng người dùng hoặc email đại diện và mã không gian. Việc nhận trạng thái đã đọc của chuỗi chỉ hỗ trợ nhận trạng thái đã đọc trạng thái của người dùng gọi, có thể được chỉ định bằng cách đặt một trong sau:- Bí danh
me
. Ví dụ:users/me/spaces/SPACE/threads/THREAD/threadReadState
. - Địa chỉ email Workspace của người dùng gọi. Ví dụ:
users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
. - Mã nhận dạng người dùng của người dùng thực hiện lệnh gọi. Ví dụ:
users/USER/spaces/SPACE/threads/THREAD/threadReadState
.
- Bí danh
Ví dụ sau đây lấy trạng thái đọc luồng của người dùng đang gọi:
Python
- Trong thư mục đang làm việc, hãy tạo một tệp có tên
chat_threadReadState_get.py
. Đưa mã sau vào
chat_threadReadState_get.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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets the thread 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().threads().getThreadReadState( # The thread read state to get. # # 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. # # Replace THREAD with a thread name. # Obtain the thread name from the messages resource of Chat API. name='users/me/spaces/SPACE/threads/THREAD/threadReadState' ).execute() # Prints the API's response. print(result) if __name__ == '__main__': main()
Trong mã, thay thế các nội dung sau:
SPACE
: tên không gian mà bạn có thể lấy từ Phương thứcspaces.list
trong API Chat hoặc qua URL của một không gian.THREAD
: tên luồng mà bạn có thể lấy từ Phương thứcspaces.messages.get
trong API Chat.
Trong thư mục đang làm việc, hãy tạo và chạy mẫu:
python3 chat_threadReadState_get.py
Node.js
- Trong thư mục đang làm việc, hãy tạo một tệp có tên
chat_threadReadState_get.js
. Đưa mã sau vào
chat_threadReadState_get
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @return {!Promise<!Object>} */ async function getThreadReadState() { /** * Authenticate with Google Workspace * and get user authorization. */ const scopes = [ 'https://www.googleapis.com/auth/chat.users.readstate.readonly', ]; 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.threads.getThreadReadState({ /** * The thread read state to get. * * 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/threads/THREADS/threadReadState' }); } /** * Use the service endpoint to call Chat API. */ getThreadReadState().then(console.log);
Trong mã, thay thế các nội dung sau:
SPACE
: tên không gian mà bạn có thể lấy từ Phương thứcspaces.list
trong API Chat hoặc qua URL của một không gian.THREAD
: tên luồng mà bạn có thể lấy từ Phương thứcspaces.messages.get
trong API Chat.
Trong thư mục đang làm việc, hãy tạo và chạy mẫu:
node chat_threadReadState_get.js
Apps Script
Ví dụ này gọi API Chat bằng cách sử dụng Dịch vụ trò chuyện nâng cao.
Thêm phạm vi uỷ quyền
chat.users.readstate.readonly
vào Tệpappsscript.json
của dự án Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
Thêm một hàm tương tự như hàm này vào dự án Apps Script mã:
/** * Authenticates with Chat API via user credentials, * then gets the thread read state for the calling user. * @param {string} threadReadStateName The resource name of the thread read state. */ function getThreadReadState(threadReadStateName) { try { Chat.Users.Spaces.Threads.getThreadReadState(threadReadStateName); } catch (err) { // TODO (developer) - Handle exception console.log('Failed to get read state with error %s', err.message); } }
API Google Chat nhận trạng thái đọc chuỗi đã chỉ định và trả về
một bản sao của
Tài nguyên ThreadReadState
.
Chủ đề có liên quan
- Cập nhật trạng thái đọc không gian của người dùng thực hiện lệnh gọi.
- Lấy trạng thái đọc không gian của người dùng thực hiện lệnh gọi.