This guide explains how to use the getThreadReadState
method on the
ThreadReadState
resource of the Google Chat API to get details about a user's
read state within a message thread. To get the read state of a message in a
space, see
Get details about a user's space read state.
The
ThreadReadState
resource
is a singleton resource that represents details about a
specified user's last read message in a Google Chat message thread.
Prerequisites
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Python Google API Client Library.
-
Create OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
Node.js
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Node.js Google API Client Library.
-
Create OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
Apps Script
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Create a standalone Apps Script project, and turn on the Advanced Chat Service.
- Choose an authorization scope that supports user authentication.
Get the calling user's thread read state
To get details about a user's read state within a message thread, include the following in your request:
- Specify the
chat.users.readstate
orchat.users.readstate.readonly
authorization scope. - Call the
getThreadReadState
method on theThreadReadState
resource. - Pass the
name
of the thread read state to get, which includes a user ID or alias and a space ID. Getting thread read state only supports getting the read state of the calling user, which can be specified by setting one of the following:- The
me
alias. For example,users/me/spaces/SPACE/threads/THREAD/threadReadState
. - The calling user's Workspace email address. For example,
users/user@example.com/spaces/SPACEthreads/THREAD/threadReadState
. - The calling user's user ID. For example,
users/USER/spaces/SPACE/threads/THREAD/threadReadState
.
- The
The following example gets the calling user's thread read state:
Python
- In your working directory, create a file named
chat_threadReadState_get.py
. Include the following code in
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()
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.THREAD
: a thread name, which you can obtain from thespaces.messages.get
method in the Chat API.
In your working directory, build and run the sample:
python3 chat_threadReadState_get.py
Node.js
- In your working directory, create a file named
chat_threadReadState_get.js
. Include the following code in
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);
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.THREAD
: a thread name, which you can obtain from thespaces.messages.get
method in the Chat API.
In your working directory, build and run the sample:
node chat_threadReadState_get.js
Apps Script
This example calls the Chat API using the Advanced Chat Service.
Add the
chat.users.readstate.readonly
authorization scope to the Apps Script project'sappsscript.json
file:"oauthScopes": [ "https://www.googleapis.com/auth/chat.users.readstate.readonly" ]
Add a function like this one to the Apps Script project's code:
/** * 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); } }
The Google Chat API gets the specified thread read state and returns
an instance of
ThreadReadState
resource.