This page explains how to create and interact with a Chat app using Google Cloud Functions.
To build the app, you write and deploy a Cloud Function that the app uses to process a response to a message event from Google Chat. The response is a card that displays the sender's name and avatar image, as demonstrated in the following image:
Objectives
- Set up your environment.
- Create and deploy a Cloud Function.
- Publish the app to Google Chat.
- Test the app.
Prerequisites
- A Google Workspace account with access to Google Chat.
- A Google Cloud project.
- Make sure that you turn on billing for your Cloud project. Learn how to verify the billing status of your projects.
Set up the environment
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, Cloud Build API, Cloud Functions API, and Cloud Pub/Sub API.
Create and deploy a Cloud Function
Create and deploy a Cloud Function that generates a Chat card with the sender's display name and avatar image. When the Chat app receives a message, it runs the function and responds with the card.
To create and deploy the function for your Chat app, complete the following steps:
Node.js
In the Google Cloud console, go to the Cloud Functions page:
Make sure that the project for your Chat app is selected.
Click
Create Function.On the Create function page, set up your function:
- In Function name, enter "QuickStartChatApp".
- In Trigger type, select HTTP.
Under Authentication, select Allow unauthenticated invocations.
For more information about authentication in Google Workspace, see Authenticate and authorize Chat apps and API requests.
Click Save.
Click Next.
In Runtime, select Node.js 10.
In Source code, select Inline Editor.
In Entry point, delete the default text and enter
helloChat
.Replace the contents of
index.js
with the following code:Click Deploy.
Python
In the Google Cloud console, go to the Cloud Functions page:
Make sure that the project for your Chat app is selected.
Click
Create Function.On the Create function page, set up your function:
- In Function name, enter "QuickStartChatApp".
- In Trigger type, select HTTP.
Under Authentication, select Allow unauthenticated invocations.
For more information about authentication in Google Workspace, see Authenticate and authorize Chat apps and API requests.
Click Save.
Click Next.
In Runtime, select Python 3.10.
In Source code, select Inline Editor.
In Entry point, delete the default text and enter
hello_chat
.Replace the contents of
main.py
with the following code:Click Deploy.
Java
In the Google Cloud console, go to the Cloud Functions page:
Make sure that the project for your Chat app is selected.
Click
Create Function.On the Create function page, set up your function:
- In Function name, enter "QuickStartChatApp".
- In Trigger type, select HTTP.
Under Authentication, select Allow unauthenticated invocations.
For more information about authentication in Google Workspace, see Authenticate and authorize Chat app and API requests.
Click Save.
Click Next.
In Runtime, select Java 11.
In Source code, select Inline Editor.
In Entry point, delete the default text and enter
HelloChat
.Rename
src/main/java/com/example/Example.java
tosrc/main/java/HelloChat.java
.Replace the contents of
HelloChat.java
with the following code:Replace the contents of
pom.xml
with the following code:Click Deploy.
The Cloud Functions page opens, and your function appears with a deployment progress indicator next to the function name. When the progress indicator disappears and a checkmark appears, your function is deployed.
Publish the app to Google Chat
After the Cloud Function is deployed, follow these steps to turn it into a Google Chat app:
In the Google Cloud console, click Menu > Cloud Functions.
Make sure that the project for which you enabled Cloud Functions is selected.
In the list of functions, click QuickStartChatApp.
On the Function details page, click Trigger.
Under Trigger URL, copy the URL.
Search for "Google Chat API" and click Google Chat API.
Click Manage.
Click Configuration and set up the Google Chat app:
- In App name, enter
Quickstart App
. - In Avatar URL, enter
https://developers.google.com/chat/images/quickstart-app-avatar.png
. - In Description, enter
Quickstart app
. - Under Functionality, select Receive 1:1 messages, Join spaces and group conversations, and Log errors to Cloud Logging.
- Under Connection settings, select App URL and paste the URL for the Cloud Function trigger into the box.
- Under Permissions, select Specific people and groups in your domain and enter your email address.
- In App name, enter
Click Save.
The app is ready to receive and respond to messages on Google Chat.
Test your Chat app
To test your Chat app, send the app a direct message:
- Open Google Chat.
- To send a direct message to the app, click Start a chat , and in the window that appears, click Find apps.
- In the Find apps dialog, search for "Quickstart App".
- To open a direct message with the app, find the Quickstart App and click Add > Chat.
- In the direct message, type
Hello
and pressenter
.
The app returns a card with your display name and avatar picture.
Next steps
To troubleshoot and debug your Chat app, refer to these pages:
- As you build the Chat app, you might need to debug it by reading the app's error logs. To read the logs, in the Google Cloud console, go to the Logs Explorer.
- Troubleshoot.
To add more functionality to your Chat app, refer to these guides:
- Create interactive cards — Card messages support a defined layout, interactive UI elements like buttons, and rich media like images. Use card messages to present detailed information, gather information from users, and guide users to take a next step.
- Support slash commands — Slash commands let
you register and advertise specific commands that users can give your app by
typing a command that begins with a forward slash (
/
), like/help
. - Launch dialogs — Dialogs are windowed, card-based interfaces that your app can open to interact with a user. Multiple cards can be strung together sequentially, which helps users complete multi-step processes, like filling in form data.
To learn more about the Google Chat API, view the reference documentation.