Overview
Google Chat bots provide easy-to-use access points to your organization's data and services. Users can converse with bots within a chat experience. One way to create a Google Chat bot is to use Google Apps Script. This also gives you easy access to other Google services like Drive, Gmail, Calendar, Docs, Sheets, and much more.
In this lab, you use Google Apps Script to create a simple Google Chat bot as a quick introduction to Google Chat bots.
What you'll do
In this lab, you perform the following tasks:
Create a chat app from a template with pre-populated event handlers.
Configure and publish the Google Chat bot.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
- Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.
How to start your lab and sign in to the Google Cloud console
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
The Open Google Cloud console button
Time remaining
The temporary credentials that you must use for this lab
Other information, if needed, to step through this lab
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account.
If necessary, copy the Username below and paste it into the Sign in dialog.
student-00-be974ef61a8b@qwiklabs.net
Copied!
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
rKB8pPR8P6WV
Copied!
You can also find the Password in the Lab Details pane.
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.
Note: Using your own Google Cloud account for this lab may incur extra charges.
Click through the subsequent pages:
Accept the terms and conditions.
Do not add recovery options or two-factor authentication (because this is a temporary account).
Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.
Task 1. Create a chat app from a template
To implement your bot, create a new Google Apps Script project using the Chat app template.
Note: Signing into Google Cloud console sets your project and credentials. Before you click the Google Apps Script editor link in Step 1, be sure you have signed into the Google Cloud console.
Click this Google Apps Script homepage link to open the Google Apps Script online editor.
Under Google Workspace add-on starters, click Chat app (Intermediate version).
Click Untitled project (the current name).
In the Edit project name dialog, rename the project to
Friendly Bot
, and then click Rename.
Events in Google Chat
Most Apps Script bot interactions with Google Chat are event-driven. The interaction between the user, the bot, and Google Chat typically follows a sequence.
A user initiates an action, like adding a bot to a room, starting a direct message (DM) with a bot, or removing the bot from a room.
The action raises an event aimed at the bot in Google Chat.
Google Chat calls the corresponding event handler defined in the bot's script.
Google Chat raises four events that your Apps Script bot can listen for:
ADDED_TO_SPACE
: This event occurs when a human user adds a bot to a room or a DM. In Apps Script, you define anonAddedToSpace()
function to handle this event.REMOVED_FROM_SPACE
: This event occurs when a user removes the bot from a room or DM. This event does not post a response back to Google Chat. In Apps Script, you define anonRemovedFromSpace()
function to handle this event.MESSAGE
: This event occurs when a user messages the bot, either directly in a DM or as an @mention in a room. In Apps Script, you define anonMessage()
function to respond to this event.
Review the code for the MESSAGE
event handler
The Chat App template pre-populates the code file containing the event handlers.
- Click on the
Code.gs
file to review the pre-populated event handlers.
Notice the event handlers for the MESSAGE
, ADDED_TO_SPACE
and REMOVE_FROM_SPACE
events, which execute the specific functions previously mentioned to complete various tasks such as responding to user messages in Google Chat.
Create a chat app from a template
Task 2. Publish and test the chat bot
Before you can run and test the bot, the Google Chat API must be enabled for your Google Cloud project, and your bot must be published.
In this task, you complete the various configuration steps to be able to publish the bot including configuring the OAuth consent screen, updating the Apps Script application to reference the appropriate Google Cloud project, and updating the Google Chat API configuration to publish the bot for testing.
Configure the OAuth consent screen
In the Google Cloud console, click the Navigation menu () in the upper left, and navigate to APIs & Services > OAuth consent screen.
Click Get Started.
For Project configuration page, for App Information configure the following, and then click Next:
Field | Value |
App name | Friendly Bot |
User support email | Select the email ID student-00-be974ef61a8b@qwiklabs.net from the drop-down. This is also your User Email in the left panel of the lab instructions. |
For Audience, select Internal and click Next.
For Contact Information, provide the email address
student-00-be974ef61a8b@qwiklabs.net
, and then click Next.Accept the policy, and click Continue. Then, click Create.
Click Check my progress to verify the objective.
Configure the user consent screen
Identify the Google Cloud project number and update the Apps Script application to reference it
From the Navigation menu (), click Cloud Overview > Dashboard.
In the dashboard, locate the Project Info section.
Record the Project number to use in the next step to configure your project.
Return to the Apps Script editor, and navigate to the Project Settings for the Friendly Bot project ().
Under Google Cloud Platform (GCP) Project, click Change project.
For GCP Project number, enter the project number that you copied earlier. Then, click Set project.
Configure and publish the chat bot
In the Apps Script editor, get the Head Deployment ID for the test deployment by clicking on Deploy > Test Deployments (top-right of the screen), and then clicking Copy next to Head Deployment ID.
In the Google Cloud console, navigate to Navigation Menu () \> APIs & Services > Library.
In the Library, search for
Google Chat API
, and select the API from the list of results.
The Google Chat API should already be enabled in this project. If it is not enabled, click Enable.
Click Manage, and then click the Configuration tab for the Google Chat API.
In the Configuration dialog, set the fields with the following values:
Field | Value |
App name | Friendly Bot |
Avatar URL | https://goo.gl/kv2ENA |
Description | Apps Script lab bot |
Functionality | Enable Join spaces and group conversations |
Connection settings | Check Apps Script, and then paste the Head Deployment ID that you copied in step 1 into the Deployment ID box. |
Visibility | student-00-be974ef61a8b@qwiklabs.net |
Click Save.
After the changes are saved, scroll to the top of the Configuration dialog to update the App Status to
LIVE – available to users
.
You may have to refresh the page to see the App Status field. If the value for App Status is already set to LIVE – available to users
, you can leave that value, but be sure to save the page again in the next step.
- Click Save again.
Test the chat bot
You have made it to final section to test your bot in Google Chat by completing the following steps!
Click the Google Chat link to open Google Chat.
Select Start a chat.
Search for
Friendly bot
.From the results, select the Friendly Bot (Apps Script lab bot) that you created to start a chat.
When the direct message thread opens, you should see a message from the bot thanking you for adding it to a DM:
Thank you for adding me to a direct message, student XXXXX!
This response is initiated by the function named onAddedToSpace
, which responds to the specific event of the bot being added to a chat space.
- Last, enter a message for the bot, such as "Hello bot!".
You should see a message from the bot repeating your message, such as:
You said "Hello bot!"
This response is initiated by the function named onMessage
, which responds to the specific event of users direct messaging the bot.
Solution of Lab
Task 1:
Open: https://script.google.com/home/start
Friendly Bot
Task 2:
Open: https://console.cloud.google.com/auth/overview/create