The Advanced Chat service lets you use the Google Chat API in Apps Script. This API allows scripts to find, create, and modify Chat spaces, add or remove members to spaces, and read or post messages with text, cards, attachments, and reactions.
Prerequisites
- An Apps Script Google Chat app configured on the Chat API configuration page in the Google Cloud console. The app's Apps Script project must use a standard Google Cloud project instead of the default one created automatically for Apps Script projects. To create a compatible Google Chat app, see Build a Google Chat app with Apps Script.
- Authentication configured for the Chat app. Performing an action on behalf of a user requires user authentication. Performing an action as the Chat app requires app authentication with a service account. To check which form of authentication a Chat API method supports, see Types of required authentication for Google Chat API calls.
Reference
For more information about this service, see the Chat API reference documentation. Like all advanced services in Apps Script, the Chat service uses the same objects, methods, and parameters as the public API.
Sample code
These samples show you how to perform common Google Chat API actions using the advanced service.
Post a message with user credentials
The following example demonstrates how to post a message to a Chat space on behalf of the user.
Add the
chat.messages.create
authorization scope to the Apps Script project'sappsscript.json
file:"oauthScopes": [ "https://www.googleapis.com/auth/chat.messages.create" ]
Add a function like this one to the Apps Script project's code:
Post a message with app credentials
The following example demonstrates how to post a message to a
Chat space on behalf of the app. Using the advanced
Chat service with a service account doesn't require you to
specify authorization scopes in appsscript.json
. For details about
authentication with service accounts, see
Authenticate as a Google Chat app.
Get a space
The following example demonstrates how to get information about a Chat space.
Add the
chat.spaces.readonly
authorization scope to the Apps Script project'sappsscript.json
file:"oauthScopes": [ "https://www.googleapis.com/auth/chat.spaces.readonly" ]
Add a function like this one to the Apps Script project's code:
Create a space
The following example demonstrates how to create a Chat space.
Add the
chat.spaces.create
authorization scope to the Apps Script project'sappsscript.json
file:"oauthScopes": [ "https://www.googleapis.com/auth/chat.spaces.create" ]
Add a function like this one to the Apps Script project's code:
List memberships
The following example demonstrates how to list all the members of a Chat space.
Add the
chat.memberships.readonly
authorization scope to the Apps Script project'sappsscript.json
file:"oauthScopes": [ "https://www.googleapis.com/auth/chat.memberships.readonly" ]
Add a function like this one to the Apps Script project's code:
Troubleshoot
If you encounter Error 400: invalid_scope
with the error message
Some requested scopes cannot be shown
,
it means you haven't specified any authorization scopes in the
Apps Script project's appsscript.json
file. In most cases,
Apps Script automatically determines what scopes a script needs,
but when you use the Chat advanced service, you must manually add
the authorization scopes that your script uses to your
Apps Script project's manifest file. See
Setting explicit scopes.
To resolve the error, add the appropriate authorization scopes
to the Apps Script project's appsscript.json
file as part of
the oauthScopes
array. For example, to call the
spaces.messages.create
method, add the following:
"oauthScopes": [
"https://www.googleapis.com/auth/chat.messages.create"
]
Limits and considerations
The Advanced Chat service doesn't support:
- The Chat API method
media.download
. - Chat API methods available in Developer Preview
To download a message attachment or call a developer preview method, use
UrlFetchApp
instead.