This page explains how to build a Google Workspace add-on that works in Google Chat using Google Apps Script. In Google Chat, add-ons appear to users as Google Chat apps. To learn more, see the Extend Google Chat overview.
The Chat app lets you directly message and responds by echoing your messages.
The following diagram shows the architecture and messaging pattern:
In the preceding diagram, a user interacting with an Apps Script Chat app has the following flow of information:
- A user sends a message to a Chat app, either in a direct message or in a Chat space.
- The Chat app logic that's implemented in Apps Script, which resides in Google Cloud, receives and processes the message.
- Optionally, the Chat app logic can integrate with Google Workspace services, such as a Calendar or Sheets, or other Google Services, such as Google Maps or YouTube.
- The Chat app logic sends a response back to the Chat app service in Chat.
- The response is delivered to the user.
Objectives
- Set up your environment.
- Set up the script.
- Configure a Google Workspace add-on for the Chat app.
- Test the Chat app.
Prerequisites
- A Business or Enterprise Google Workspace account with access to Google Chat.
- A Google Cloud project. To create one, see Create a Google Cloud project.
Set up your environment
Open your Cloud project in the Google Cloud console
If it's not open already, open the Cloud project that you intend to use for this sample:
- In the Google Cloud console, go to the Select a project page.
- Select the Google Cloud project you want to use. Or, click Create project and follow the on-screen instructions. If you create a Google Cloud project, you might need to turn on billing for the project.
Turn on the Chat API
Before using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project.In the Google Cloud console, enable the Google Chat API.
Configure the OAuth consent screen
All apps using OAuth 2.0 require a consent screen configuration. Configuring your app's OAuth consent screen defines what is displayed to users and app reviewers, and registers your app so you can publish it later.
- In the Google Cloud console, go to Menu > APIs & Services > OAuth consent screen.
- For User type select Internal, then click Create.
- Complete the app registration form, then click Save and Continue.
For now, you can skip adding scopes and click Save and Continue. In the future, when you create an app for use outside of your Google Workspace organization, you must change the User type to External, and then, add the authorization scopes that your app requires.
- Review your app registration summary. To make changes, click Edit. If the app registration looks OK, click Back to Dashboard.
Set up the script
To set up the script, you make a copy of the quickstart project and then set your Cloud project in Apps Script.
Make a copy of the quickstart project
- Click the following button to open the Add-on in Google Chat
Apps Script project.
Open the project - Click Overview.
- On the overview page, click Make a copy.
Name your copy of the Apps Script project:
Click Copy of Add-on Chat app.
In Project title, type
Add-on Chat app
Click Rename.
Copy the Cloud project number
- In the Google Cloud console, go to Menu > IAM & Admin > Settings.
- In the Project number field, copy the value.
Set the Apps Script project's Cloud project
- In the Chat app Apps Script project, click Project Settings .
- Under Google Cloud Platform (GCP) Project, click Change project.
- In GCP project number, paste the Google Cloud project number.
- Click Set project.
You now have working app code that you can try out (as described in the following steps) and then customize to meet your requirements.
Make sure that you're signed in to the correct Google Account when you open the Apps Script project. The current account can sometimes switch to your default account without you noticing.
Create a test deployment
You need a deployment ID for this Apps Script project, so that you can use it in the next step.
To get the head deployment ID, do the following:
- In the Chat app Apps Script project, click Deploy > Test deployments.
- Copy the Head deployment ID for use in a later step and click Done.
Configure the add-on
Using your Apps Script deployment, follow these steps to create a add-on and deploy the Google Chat app:
- In the
console,
search for
Google Chat API
, and click Google Chat API. - Click Manage.
Click Configuration and set up the Chat app:
- In the App name field, enter
Add-on Chat app
. - In the Avatar URL field, enter
https://developers.google.com/chat/images/quickstart-app-avatar.png
. - In the Description field, enter
Add-on Chat app
. - Under Functionality, select Receive 1:1 messages and Join spaces and group conversations.
- Under Connection settings, select Apps Script project.
- In the Deployment ID field, paste the Head deployment ID that you previously copied.
- Under Visibility, select Specific people and groups in your domain, and enter your email.
- In the App name field, enter
Click Save.
The Chat app is ready to respond to messages.
Test the Chat app
To test your Chat app, open a direct message space with the Chat app and send a message:
Open Google Chat using the Google Workspace account that you provided when you added yourself as a trusted tester.
- Click New chat.
- In the Add 1 or more people field, type the name of your Chat app.
Select your Chat app from the results. A direct message opens.
In the new direct message with the app, type
Hello
and pressenter
.The Chat app thanks you for adding it and echoes your message.
To add trusted testers and learn more about testing interactive features, see Test interactive features for Google Chat apps.
Troubleshoot
When a Google Chat app or card returns an error, the Chat interface surfaces a message saying "Something went wrong." or "Unable to process your request." Sometimes the Chat UI doesn't display any error message, but the Chat app or card produces an unexpected result; for example, a card message might not appear.
Although an error message might not display in the Chat UI, descriptive error messages and log data are available to help you fix errors when error logging for Chat apps is turned on. For help viewing, debugging, and fixing errors, see Troubleshoot and fix Google Chat errors.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, we recommend that you delete the Cloud project.
- In the Google Cloud console, go to the Manage resources page. Click Menu > IAM & Admin > Manage Resources.
- In the project list, select the project you want to delete and then click Delete .
- In the dialog, type the project ID and then click Shut down to delete the project.