This guide explains how to use the
create
method on the Section resource of the Google Chat API to create a new
custom section in Google Chat.
Sections help users group their conversations and customize the list of spaces displayed in the Google Chat navigation panel. For more information, see Create and organize sections in Google Chat.
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 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.
Create a section
To create a section with user authentication, pass the following in your request:
- Specify the
chat.users.sectionsauthorization scope. - Call the
CreateSectionmethod. - In the request body, provide a
Sectionresource:- Set
displayNameto a name for the section (up to 80 characters). - Set
typetoCUSTOM_SECTION.
- Set
The following example creates a section:
Python
from google.cloud import chat_v1
def create_section():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.CreateSectionRequest(
parent="users/me",
section=chat_v1.Section(
display_name="SECTION_DISPLAY_NAME",
type=chat_v1.Section.SectionType.CUSTOM_SECTION
)
)
# Make the request
response = client.create_section(request=request)
print(response)
To run this sample, replace the following:
SECTION_DISPLAY_NAME: The name of the new section.
The Chat API returns an instance of
Section that details the section that was created.
Related topics
- Update a section
- Delete a section
- Change the position of a section
- List sections
- List spaces in a section
- Move a space to a different section