This guide explains how to use the patch
method on the Space
resource of
the Google Chat API to update a space. Update a space to change attributes about a
space, like its user-visible display name, description, and guidelines.
The
Space
resource
represents a place where people and Chat apps can send messages,
share files, and collaborate. There are several types of spaces:
- Direct messages (DMs) are conversations between two users or a user and a Chat app.
- Group chats are conversations between three or more users and Chat apps.
- Named spaces are persistent places where people send messages, share files, and collaborate.
Prerequisites
Python
- Python 3.6 or greater
- The pip package management tool
The latest Google client libraries for Python. To install or update them, run the following command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- A Google Cloud project with the Google Chat API enabled and configured. For steps, see Build a Google Chat app.
Authorization configured for the Chat app. Updating a space requires User authentication with the
chat.spaces
authorization scope.
Node.js
- Node.js & npm
The latest Google client libraries for Node.js. To install them, run the following command in your command-line interface:
npm install @google-cloud/local-auth @googleapis/chat
- A Google Cloud project with the Google Chat API enabled and configured. For steps, see Build a Google Chat app.
Authorization configured for the Chat app. Updating a space requires User authentication with the
chat.spaces
authorization scope.
Update a space
To update an existing space in Google Chat, pass the following in your request:
- Specify the
chat.spaces
authorization scope. - Call the
patch
method on theSpace
resource. In your request, you specify the spacename
field, theupdateMask
field with one or more fields to update, and abody
with the updated space information.
You can update things like the display name, space type, history state, and more. To see all the fields you can update, see the reference documentation.
Here's how to update the spaceDetails
field of an existing space:
Python
- In your working directory, create a file named
chat_space_update.py
. Include the following code in
chat_space_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.spaces"] def main(): ''' Authenticates with Chat API via user credentials, then updates the specified space description and guidelines. ''' # 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.spaces().patch( # The space to update, and the updated space details. # # Replace {space} with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. name='spaces/SPACE', updateMask='spaceDetails', body={ 'spaceDetails': { 'description': 'This description was updated with Chat API!', 'guidelines': 'These guidelines were updated with Chat API!' } } ).execute() # Prints details about the updated space. print(result) if __name__ == '__main__': main()
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, build and run the sample:
python3 chat_space_update.py
Node.js
- In your working directory, create a file named
update-space.js
. Include the following code in
update-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Updates a Chat space with the description and guidelines. * @return {!Promise<!Object>} */ async function updateSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.patch({ name: 'spaces/SPACE', updateMask: 'spaceDetails', requestBody: { spaceDetails: { description: 'This description was updated with Chat API!', guidelines: 'These guidelines were updated with Chat API!' }, } }); } updateSpace().then(console.log);
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, run the sample:
node update-space.js
The Google Chat API returns an instance of the
Space
resource reflecting the updates.
Related topics
- Get details about a space.
- List spaces.
- Delete a space.
- Set up a space.
- Find a direct message space.
- Make a space discoverable to specific users.