AI-generated Key Takeaways
-
This guide explains how to use the
update()method to mark Google Chat spaces as read or unread. -
The
SpaceReadStateresource stores a user's last read message details in a space. -
To update read state, use the
UpdateSpaceReadState()method with thelastReadTimefield. -
You need a Google Workspace account and a configured Google Chat API to use this functionality.
-
Node.js code samples and related topics are provided for practical implementation.
This guide explains how to use the
update()
method on the SpaceReadState resource of the Google Chat API to mark spaces as
read or unread.
The
SpaceReadState resource
is a singleton resource that represents details about a
specified user's last read message in a Google Chat space.
Prerequisites
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 Cloud 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
credentials.jsonto your local directory.
- Choose an authorization scope that supports user authentication.
Update the calling user's space read state
To update a user's read state within a space, include the following in your request:
- Specify the
chat.users.readstateauthorization scope. - Call the
UpdateSpaceReadState()method. - Pass
updateMaskwith the valuelastReadTime. - Pass
spaceReadStateas an instance ofSpaceReadStatewith the following:- The
namefield set to the space read state to update, which includes a user ID or alias and a space ID. Updating space read state only supports updating the read state of the calling user, which can be specified by setting one of the following:- The
mealias. For example,users/me/spaces/SPACE/spaceReadState. - The calling user's Workspace email address. For example,
users/user@example.com/spaces/SPACE/spaceReadState. - The calling user's user ID. For example,
users/USER/spaces/SPACE/spaceReadState.
- The
- The
lastReadTimefield set to the updated value of the time when the user's space read state was updated. Usually this corresponds with either the timestamp of the last read message, or a timestamp specified by the user to mark the last read position in a space. When thelastReadTimeis before the latest message create time, the space appears as unread in the UI. To mark the space as read, setlastReadTimeto any value later (larger) than the latest message create time. ThelastReadTimeis coerced to match the latest message create time. Note that the space read state only affects the read state of messages that are visible in the space's top-level conversation. Replies in threads are unaffected by this timestamp, and instead rely on the thread read state.
- The
The following example updates the calling user's space read state:
Node.js
To run this sample, replace SPACE_NAME with the ID from
the space's
name.
You can obtain the ID by calling the
ListSpaces()
method or from the space's URL.
The Google Chat API updates the specified space read state and returns
an instance of
SpaceReadState.