Advanced Chat Service (original) (raw)
The Advanced Chat service lets you use theGoogle 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, seeBuild a Google Chat app with Google Apps Script.
- Authentication configured for the Chat app. Performing an action on behalf of a user requiresuser authentication. Performing an action as the Chat app requiresapp authentication with a service account. To check which form of authentication a Chat API method supports, seeTypes of required authentication for Google Chat API calls.
Reference
For more information about this service, see theChat 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 commonGoogle Chat APIactions 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.createauthorization scope to the Apps Script project'sappsscript.jsonfile:
"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, seeAuthenticate as a Google Chat app.
Get a space
The following example demonstrates how to get information about a Chat space.
- Add the
chat.spaces.readonlyauthorization scope to the Apps Script project'sappsscript.jsonfile:
"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.createauthorization scope to the Apps Script project'sappsscript.jsonfile:
"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.readonlyauthorization scope to the Apps Script project'sappsscript.jsonfile:
"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 messageSome 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. SeeSetting 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 thespaces.messages.createmethod, 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 methodmedia.download.
- Chat API methods available inDeveloper Preview
To download a message attachment or call a developer preview method, useUrlFetchApp instead.