![](https://cdn.qwiklabs.com/assets/labs/start_lab-f45aca49782d4033c3ff688160387ac98c66941d.png)
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Publish and test the bot
/ 30
Test the bot in card-formatted response
/ 30
Test the bot by button clicking on cards
/ 40
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, which you'll name "Attendance Bot". The bot integrates with Gmail to set a user's vacation responder and integrates with Calendar to put a meeting on the user's calendar.
In this lab, you perform the following tasks:
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:
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:
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.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
To implement your bot, create a new Google Apps Script project using the Chat App template.
Click this Google Apps Script homepage link to open the Google Apps Script online editor.
Scroll down to the bottom of the page, and click on Chat App.
Click Untitled project (the current name).
In the Edit project name dialog, rename the project to Attendance Bot
, and then click Rename.
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.
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 an onAddToSpace()
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 an onRemoveFromSpace()
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 an onMessage()
function to respond to this event.CARD_CLICKED
: This event occurs when the user clicks a button with a custom action assigned to it. In Apps Script, you define an onCardClick()
function to respond to this event.MESSAGE
event handlerThe Chat App template pre-populates the code file containing the event handlers.
Code.gs
file to see the pre-populated event handlers.Notice the event handlers for the MESSAGE
, ADDED_TO_SPACE
and REMOVE_FROM_SPACE
events. (You will add a handler for the CARD_CLICKED
events later in this lab.)
Code.gs
file.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 to Google Chat.
Go to the Google Cloud console, click the Navigation menu () in the upper left and navigate to APIs & Services > OAuth consent screen.
Set User Type to Internal, and click Create.
On the next page, (the OAuth consent screen), configure the following:
Field | Value |
---|---|
App name | Attendance Bot |
User support email | Select the email ID |
Developer contact information |
Click Save and Continue.
On the next page (Scopes), leave all the fields empty, and then click Save and Continue.
As shown in the next page (Summary), the OAuth Consent Screen is now created for your app.
Click Back to Dashboard at the bottom of the page.
Click the More icon () in the upper right to expand the menu, and select Project settings.
Record the Project number to use in the next step to configure your project.
Return to the App Script editor, and navigate to the Project Settings for the Attendance 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.
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
. Select the API from the list of results.
The Google Chat API is already enabled in this project. If it is not enabled, click Enable.
Click Manage, and then click the Configuration tab under the Google Chat API section.
In the Configuration dialog, set the fields with the following values:
Field | Value |
---|---|
App name | Attendance Bot |
Avatar URL | https://goo.gl/kv2ENA |
Description | Apps Script lab bot |
Functionality | Select Receive 1:1 messages and Join spaces and group conversations |
Connection settings | Check Apps Script project, and then paste the Head Deployment ID that you copied in step 1 into the Deployment ID field. |
Visibility |
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.
To test your bot in Google Chat, do the following:
Click the Google Chat link to open Google Chat.
Select Start a chat () in the Chat section.
Search for Attendance bot
.
From the results, select the Attendance Bot, Apps Script lab bot that you created, and click Start chat.
When the direct message thread opens, you should see a message from the bot thanking you for adding it to a DM.
Click Check my progress to verify the objective.
In the previous task, your bot responded to Google Chat events with a simple text response. In this task, you update your bot to respond with cards.
Google Chat supports the use of cards for responses. Cards are visual containers that allow you to group sets of user interface widgets together. Cards can display headers, text paragraphs, sets of buttons, images, and key/value text. Your bot can define one or many cards in its JSON response to Google Chat, which then translates your response into the corresponding UI elements.
The following image shows an example card response with three sections that includes a header, a key/value widget, an image widget, and a text button.
In Apps Script, click Editor (), and open the
Code.gs
file.
Locate the existing code for function onMessage(event)
at the top of Code.gs
file, and delete code lines 1 to 17.
You replace these code lines for function onMessage(event)
with new code in the next step.
Code.gs
file:Code.gs
Code.gs
file.By replacing the onMessage()
function and adding the createCardResponse()
function, you accomplish the following:
onMessage()
function reads the user's original message and constructs a response as a simple TextParagragh widget.onMessage()
function then calls createCardResponse()
, which places the TextParagraph widget within a section of a single card.The Attendance bot should respond with a Log your vacation time card.
onMessage()
event handler parses the event object passed to it by Google Chat to extract the user's original message. You can also get other types of information about the event, including the name of the user that initiated the event, their email address, the name of the room that the event occurred in, and much more.For more information about the structure of the event objects sent by Google Chat, you can refer to the Event formats reference guide.
Click Check my progress to verify the objective.
In the previous step, your bot responded to a message from a user—a MESSAGE
event—with a simple card that contained a TextParagragh widget. In this step, you will create a response that includes buttons, where each button has a custom action defined for it.
Card responses can contain one of two types of buttons: TextButton widgets, which display text-only buttons; and ImageButton widgets, which display a button with a simple icon or image without text.
Both TextButton and ImageButton widgets support one of two onClick
behaviors (as defined in the JSON response sent back to Google Chat): either openLink
or action
. As the name implies, openLink
opens a specified link in a new browser tab.
The action
object, however, specifies a custom action for the button to perform. You can specify several arbitrary values in the action object, including a unique actionMethodName
and a set of key / value parameter pairs.
Specifying an action
object for the button creates an interactive card. When the user clicks the button in the message, Google Chat raises a CARD_CLICKED
event and sends a request back to the bot that sent the original message. The bot then needs to handle the event raised from Google Chat and return a response back to the space.
Return to Apps Script, and click Editor() in the Files panel.
In Code.gs
, remove the onMessage()
function again by deleting lines 25-44.
Copy the following code, and paste at line 25.
This code creates two buttons, a Set vacation in Gmail and a Block out day in Calendar button in the card sent to Google Chat.
Code.gs
CARD_CLICKED
event, add the onCardClick()
function to the end of the Code.gs
file.Code.gs
In responding to user clicks, now the bot does one of two things: It sets the user's vacation responder in Gmail to an "out of office" message; or it schedules an all-day meeting on the user's Calendar. To accomplish these tasks, the bot calls the Gmail advanced service and the Calendar Apps Script API.
Code.gs
file.Code.gs
Click Save () to save the
Code.gs
file.
In the left-side menu, next to Services, click Add a service (), and select Gmail API from the list.
Click Add.
Make sure that the Gmail Advanced Service is enabled in this project. To check the Gmail API status, do the following:
In the Google Cloud console, navigate to Navigation Menu () and select > APIs & Services > Library.
In the Library, search for Gmail API
. Select the API from the list of results.
The Gmail API is already enabled in this project. If it is not, click Enable.
I'm sick
.The bot should respond with a card similar to the image below that prompts you to configure the bot.
Now the bot displays the available options.
Click SET VACATION IN GMAIL. You should see the message "Turned on vacation settings."
Click BLOCK OUT DAY IN CALENDAR. You should see the message "Blocked out your calendar for the day."
You should see the Vacation Setting in Gmail.
You should also see a day blocked out in the Calendar.
Click Check my progress to verify the objective.
You created a bot that responds to user messages, sets their vacation responder in Gmail, and puts an all-day event on their Calendar.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated August 4, 2024
Lab Last Tested August 4, 2024
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Konten ini tidak tersedia untuk saat ini
Kami akan memberi tahu Anda melalui email saat konten tersedia
Bagus!
Kami akan menghubungi Anda melalui email saat konten tersedia
One lab at a time
Confirm to end all existing labs and start this one