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

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.

  1. Add the chat.messages.create authorization scope to the Apps Script project's appsscript.json file:
"oauthScopes": [  
  "https://www.googleapis.com/auth/chat.messages.create"  
]  
  1. 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.

  1. Add the chat.spaces.readonly authorization scope to the Apps Script project's appsscript.json file:
"oauthScopes": [  
  "https://www.googleapis.com/auth/chat.spaces.readonly"  
]  
  1. 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.

  1. Add the chat.spaces.create authorization scope to the Apps Script project's appsscript.json file:
"oauthScopes": [  
  "https://www.googleapis.com/auth/chat.spaces.create"  
]  
  1. 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.

  1. Add the chat.memberships.readonly authorization scope to the Apps Script project's appsscript.json file:
"oauthScopes": [  
  "https://www.googleapis.com/auth/chat.memberships.readonly"  
]  
  1. 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:

To download a message attachment or call a developer preview method, useUrlFetchApp instead.