Z tego przewodnika dowiesz się, jak używać metody getThreadReadState
na stronie
ThreadReadState
zasób interfejsu Google Chat API, który zawiera szczegółowe informacje o użytkowniku
stanu odczytu w wątku. Aby poznać stan przeczytania wiadomości w
kosmos, zobacz
Uzyskiwanie informacji o stanie odczytu pokoju użytkownika
ThreadReadState
zasób
to pojedynczy zasób, który przedstawia szczegółowe informacje
ostatniej przeczytanej wiadomości użytkownika w wątku wiadomości Google Chat.
Wymagania wstępne
Python
- Firmy lub przedsiębiorstwa Konto Google Workspace z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth
- Włącz i skonfiguruj interfejs Google Chat API pod nazwą. ikonę i opis aplikacji Google Chat.
- Zainstaluj Python Biblioteka klienta interfejsów API Google.
-
Utwórz dane logowania na podstawie identyfikatora klienta OAuth dla aplikacji komputerowej. Aby uruchomić próbkę w tym
Przewodnika, zapisz dane logowania w pliku JSON o nazwie
client_secrets.json
katalog lokalny.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Node.js
- Firmy lub przedsiębiorstwa Konto Google Workspace z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth
- Włącz i skonfiguruj interfejs Google Chat API pod nazwą. ikonę i opis aplikacji Google Chat.
- Zainstaluj Node.js Biblioteka klienta interfejsów API Google.
-
Utwórz dane logowania na podstawie identyfikatora klienta OAuth dla aplikacji komputerowej. Aby uruchomić próbkę w tym
Przewodnika, zapisz dane logowania w pliku JSON o nazwie
client_secrets.json
katalog lokalny.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Google Apps Script
- Firmy lub przedsiębiorstwa Konto Google Workspace z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth
- Włącz i skonfiguruj interfejs Google Chat API pod nazwą. ikonę i opis aplikacji Google Chat.
- Utwórz samodzielny projekt Apps Script. i włącz zaawansowaną usługę czatu.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Pobieranie stanu odczytu wątku użytkownika wywołującego
Aby uzyskać szczegółowe informacje o stanie odczytu użytkownika w wątku, umieść w nim parametr :
- Określ
chat.users.readstate
lubchat.users.readstate.readonly
zakresu autoryzacji. - Wywołaj funkcję
Metoda
getThreadReadState
wThreadReadState
zasób. - Przekazuj wartość
name
stanu odczytu wątku, która zawiera identyfikator użytkownika lub alias i identyfikator pokoju. Uzyskiwanie stanu odczytu wątku obsługuje tylko pobieranie odczytu stanu użytkownika wywołującego, który można określić, ustawiając jedną z :- Alias
me
. Przykład:users/me/spaces/SPACE/threads/THREAD/threadReadState
- Adres e-mail użytkownika nawiązującego połączenie z Workspace. Przykład:
users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
- Identyfikator użytkownika wywołującego. Przykład:
users/USER/spaces/SPACE/threads/THREAD/threadReadState
- Alias
Ten przykład pokazuje stan odczytania wątku przez użytkownika wywołującego:
Python
- W katalogu roboczym utwórz plik o nazwie
chat_threadReadState_get.py
Umieść w pliku
chat_threadReadState_get.py
ten kod: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()
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, która który znajdziesz w Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.THREAD
: nazwa wątku, która który znajdziesz w Metodaspaces.messages.get
w interfejsie Chat API.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_threadReadState_get.py
Node.js
- W katalogu roboczym utwórz plik o nazwie
chat_threadReadState_get.js
Umieść w pliku
chat_threadReadState_get
ten kod: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);
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, która który znajdziesz w Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.THREAD
: nazwa wątku, która który znajdziesz w Metodaspaces.messages.get
w interfejsie Chat API.
W katalogu roboczym skompiluj i uruchom przykład:
node chat_threadReadState_get.js
Google Apps Script
W tym przykładzie wywołujemy interfejs Chat API za pomocą parametru Zaawansowana usługa czatu.
Dodaj zakres autoryzacji
chat.users.readstate.readonly
do Plikappsscript.json
projektu Apps Script:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
Dodaj funkcję taką jak ta do kod:
/** * 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); } }
Interfejs Google Chat API pobiera określony stan odczytu wątku i zwraca go
wystąpienie
ThreadReadState
zasób.
Powiązane artykuły
- Zaktualizuj stan odczytu danych użytkownika w pokoju
- Uzyskiwanie stanu odczytu pokoju użytkownika dzwoniącego