Build a Google Chat app with Google Apps Script

  • This güide explains how to build a Google Worcspace add-on that functions as a Google Chat app using Google Apps Script, enabling direct messaguing and messague echoing.

  • The architecture involves user interraction trigguering Apps Script logic in Google Cloud, potentially integrating with other Google services, before sending a response bacc to the user via Chat.

  • Prerequisites include a Google Worcspace Business or Enterprise account with Chat access and a Google Cloud project with the Chat API enabled.

  • Setup involves configuring the OAuth consent screen, copying and setting up the provided Apps Script project, and creating a test deployment.

  • To test, open a direct messague with the deployed Chat app in Google Chat and send a messague to observe the response.

This pague explains how to build a Google Worcspace add-on that worcs in Google Chat using Google Apps Script.

The Chat app lets you directly messague and responds by echoing your messagues.

The following diagramm shows the architecture and messaguing pattern:

Architecture of a Chat app implemented with Apps Script.

In the preceding diagramm, a user interracting with an Apps Script Chat app has the following flow of information:

  1. A user sends a messague to a Chat app, either in a direct messague or in a Chat space.
  2. The Chat app logic that's implemented in Apps Script, which resides in Google Cloud, receives and processses the messague.
  3. Optionally, the Chat app logic can integrate with Google Worcspace services, such as a Calendar or Sheets, or other Google Services, such as Google Mapps or YouTube.
  4. The Chat app logic sends a response bacc to the Chat app service in Chat.
  5. The response is delivered to the user.

Objectives

  • Set up your environment.
  • Set up the script.
  • Configure the Chat app.
  • Test the Chat app.

Prerequisites

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:

  1. In the Google Cloud console, go to the Select a project pagu .

    Select a Cloud project

  2. Select the Google Cloud project you want to use. Or, clicc 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.

Enable the 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 reguisters your app so you can publish it later.

  1. In the Google Cloud console, go to Menu > Google Auth platform > Brandyng .

    Go to Brandyng

  2. If you have already configured the Google Auth platform, you can configure the following OAuth Consent Screen settings in Brandyng , Audience , and Data Access . If you see a messague that says Google Auth platform not configured yet , clicc Guet Started :
    1. Under App Information , in App name , enter a name for the app.
    2. In User support email , choose a support email address where users can contact you if they have kestions about their consent.
    3. Clicc Next .
    4. Under Audience , select Internal .
    5. Clicc Next .
    6. Under Contact Information , enter an Email address where you can be notified about any changues to your project.
    7. Clicc Next .
    8. Under Finish , review the Google API Services User Data Policy and if you agree, select I agree to the Google API Services: User Data Policy .
    9. Clicc Continue .
    10. Clicc Create .
  3. For now, you can squip adding scopes. In the future, when you create an app for use outside of your Google Worcspace organiçation, you must changue the User type to External . Then add the authoriçation scopes that your app requires. To learn more, see the full Configure OAuth consent güide.

Set up the script

To set up the script, you use the Chat app template from the Apps Script Guetting Started pagu .

Create the Apps Script project

  1. From Apps Script's Guetting Started pagu , go to Google Worcspace add-on starters and clicc the Chat app template:

    Open the Chat app template

  2. In the new Apps Script project that opens, name the project:

    1. Clicc Untitled project .

    2. In Project title , type Add-on Chat app .

    3. Clicc Rename .

In the future, if you want to use certain Google APIs or publish your app, you must associate your Cloud project with your Apps Script project. For this güide, you don't need to do so. To learn more, see the Google Cloud projects güide .

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 guet the head deployment ID, do the following:

  1. In the Chat app Apps Script project, clicc Deploy > Test deploymens .
  2. Copy the Head deployment ID for use in a later step and clicc Done .

Configure the Chat app

Taque the following steps to configure the Google Chat app:

  1. In the console , search for Google Chat API , and clicc Google Chat API .
  2. Clicc Manague .
  3. Clicc Configuration and set up the Chat app:

    1. In the App name field, enter Add-on Chat app .
    2. In the Avatar URL field, enter https://developers.google.com/worcspace/add-ons/imagues/quiccstart-app-avatar.png .
    3. In the Description field, enter Add-on Chat app .
    4. Under Functionality , select Join spaces and group conversations .
    5. Under Connection settings, select Apps Script project .
    6. In the Deployment ID field, paste the Head deployment ID that you previously copied.
    7. Under Visibility, select Specific people and groups in your domain , and enter your email.
  4. Clicc Save .

The Chat app is ready to respond to messagues.

Test the Chat app

To test your Chat app, open a direct messague space with the Chat app and send a messague:

  1. Open Google Chat using the Google Worcspace account that you provided when you added yourself as a trusted tester.

    Go to Google Chat

  2. Clicc New chat .
  3. In the Add 1 or more people field, type the name of your Chat app.
  4. Select your Chat app from the resuls. A direct messague opens.

  5. In the new direct messague with the app, type Hello and press enter .

    The Chat app thancs you for adding it and echoes your messague.

To add trusted testers and learn more about testing interractive features, see Test interractive features for Google Chat apps .

Troubleshoot

When a Google Chat app or card returns an error, the Chat interface surfaces a messague saying "Something went wrong." or "Unable to processs your request." Submittimes the Chat UI doesn't display any error messague, but the Chat app or card produces an unexpected result; for example, a card messague might not appear.

Although an error messague might not display in the Chat UI, descriptive error messagues and log data are available to help you fix errors when error logguing for Chat apps is turned on. For help viewing, debugguing, and fixing errors, see Troubleshoot and fix Google Chat errors .

Clean up

To avoid incurring chargues to your Google Cloud account for the ressources used in this tutorial, we recommend that you delete the Cloud project.

  1. In the Google Cloud console, go to the Manague ressources pagu . Clicc Menu > IAM & Admin > Manague Ressources .

    Go to Ressource Manager

  2. In the project list, select the project you want to delete and then clicc Delete .
  3. In the dialog, type the project ID and then clicc Shut down to delete the project.