Coding level
: Intermediate
Duration
: 25 minutes
Project type
: Google Chat app
Objectives
- Understand what the solution does.
- Understand what the Apps Script services do within the
solution.
- Set up your environment.
- Set up the script.
- Run the script.
About this solution
Schedule a meeting in Google Calendar from within a direct message (DM) or a
space in Google Chat. You can set specific details for the meeting, such as
subject, start time, or duration, or use the default settings for instant
meeting scheduling.
How it works
The Chat app script uses
slash commands
and
dialogs
to get
meeting details from users and schedule a Calendar event. The
script includes default meeting settings that can be customized to fit your
needs.
Apps Script services
This solution uses the following services:
- Calendar service
?Creates the
calendar event from the provided meeting info.
- Base service
?Uses the
Session
class to get
the script's time zone. Calendar uses this time zone when
scheduling the event.
- Utilities service
?Formats the date for
the calendar event and encodes the event ID to help get the event URL.
Prerequisites
To use this sample, you need the following prerequisites:
- A Google Account (Google Workspace accounts might
require administrator approval).
- A web browser with access to the internet.
- 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 a Cloud project
- 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 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.
All Chat apps require a consent screen configuration. Configuring
your app's OAuth consent screen defines what Google displays to users and
registers your app so you can publish it later.
-
In the Google Cloud console, go to Menu
menu
>
APIs & Services
>
OAuth consent screen
.
Go to 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
Create the Apps Script project
- Click the following button to open the
Schedule meetings from Google Chat
Apps Script project.
Open the project
- Click
Overview
info_outline
.
- On the overview page, click Make a copy
.
Copy the Cloud project number
-
In the Google Cloud console, go to Menu
menu
>
IAM & Admin
>
Settings
.
Go to IAM & Admin Settings
- In the
Project number
field, copy the value.
Set the Apps Script project's Cloud project
- In your copied 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
.
Create a test deployment
- In your copied Apps Script project, click
Deploy
>
Test deployments
.
- Copy the
Head deployment ID
for use in a later step and click
Done
.
- In the Google Cloud console, go to the
Chat API
page.
Go to Chat API
- Click
Configuration
.
- Configure the Chat API with the following information:
- Name
:
Meeting Scheduler
- Avatar URL
: Add a URL that points to an image with a minimum size of
256x256 pixels.
- Description
:
Quickly create meetings.
- Functionality
: Check both boxes to let users message the app directly
and add it to spaces.
- Connection settings
: Click
Apps Script
and enter the
head deployment ID.
- Slash commands
: Add slash commands for
/help
and
/schedule_Meeting
by taking the following steps:
- Click
Add slash command
and configure it with the following
information:
- Name
:
/help
- Command ID
:
1
- Description
:
Learn what this app does.
- Click
Add slash command
again and configure it with the following
information:
- Name
:
/schedule_Meeting
- Command ID
:
2
- Description
:
Schedule a meeting.
- Check the
Opens a dialog
box.
- Permissions
: Select
Specific people and groups in your domain
and
enter your email address.
- Click
Save
and refresh the page.
- On the configuration page, under
App status
, set the status
to
Live - available to users
.
- Click
Save
.
Run the script
- Open
Google Chat
.
- Click Start a chat
add
.
- Search for the app's name,
Meeting Scheduler
.
- Send an initial message, such as
hello
, to prompt authorization.
When the app replies, click
Configure
and authorize the app.
If the OAuth consent screen displays the warning,
This app isn't verified
,
continue by selecting
Advanced
>
Go to {Project Name} (unsafe)
.
Send
/schedule_Meeting
to the app.
In the dialog, add at least one invitee email address. You can update the
other fields or use the default entries.
Click
Submit
.
To view the meeting, click
Open Calendar event
.
Review the code
To review the Apps Script code for this solution, click
View source code
below:
View source code
Dialog.gs
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Form input dialog as JSON.
* @return {object} JSON-formatted cards for the dialog.
*/
function getInputFormAsDialog_(options) {
const form = getForm_(options);
return {
'actionResponse': {
'type': 'DIALOG',
'dialogAction': {
'dialog': {
'body': form
}
}
}
};
}
/**
* Form JSON to collect inputs regarding the meeting.
* @return {object} JSON-formatted cards.
*/
function getForm_(options) {
const sections = [];
// If errors present, display additional section with validation messages.
if (options.errors && options.errors.length) {
let errors = options.errors.reduce((str, err) => `${str}? ${err}<br>`, '');
errors = `<b>Errors:</b><br><font color="#ba0000">${errors}</font>`;
const errorSection = {
'widgets': [
{
textParagraph: {
text: errors
}
}
]
}
sections.push(errorSection);
}
let formSection = {
'header': 'Schedule meeting and send email to invited participants',
'widgets': [
{
'textInput': {
'label': 'Event Title',
'type': 'SINGLE_LINE',
'name': 'subject',
'value': options.subject
}
},
{
'textInput': {
'label': 'Invitee Email Address',
'type': 'SINGLE_LINE',
'name': 'email',
'value': options.invitee,
'hintText': 'Add team group email'
}
},
{
'textInput': {
'label': 'Description',
'type': 'MULTIPLE_LINE',
'name': 'body',
'value': options.body
}
},
{
'textInput': {
'label': 'Meeting start date & time',
'type': 'SINGLE_LINE',
'name': 'date',
'value': options.startTime,
'hintText': 'mm/dd/yyyy H:MM'
}
},
{
'selectionInput': {
'type': 'DROPDOWN',
'label': 'Meeting Duration',
'name': 'duration',
'items': [
{
'text': '15 minutes',
'value': '15',
'selected': options.duration === 15
},
{
'text': '30 minutes',
'value': '30',
'selected': options.duration === 30
},
{
'text': '45 minutes',
'value': '45',
'selected': options.duration === 45
},
{
'text': '1 Hour',
'value': '60',
'selected': options.duration === 60
},
{
'text': '1.5 Hours',
'value': '90',
'selected': options.duration === 90
},
{
'text': '2 Hours',
'value': '120',
'selected': options.duration === 120
}
]
}
}
],
'collapsible': false
};
sections.push(formSection);
const card = {
'sections': sections,
'name': 'Google Chat Scheduled Meeting',
'fixedFooter': {
'primaryButton': {
'text': 'Submit',
'onClick': {
'action': {
'function': 'handleFormSubmit'
}
},
'altText': 'Submit'
}
}
};
return card;
}
/**
* Confirmation dialog after a calendar event is created successfully.
* @param {string} url The Google Calendar Event url for link button
* @return {object} JSON-formatted cards for the dialog
*/
function getConfirmationDialog_(url) {
return {
'actionResponse': {
'type': 'DIALOG',
'dialogAction': {
'dialog': {
'body': {
'sections': [
{
'widgets': [
{
'textParagraph': {
'text': 'Meeting created successfully!'
},
'horizontalAlignment': 'CENTER'
},
{
'buttonList': {
'buttons': [
{
'text': 'Open Calendar Event',
'onClick': {
'openLink': {
'url': url
}
}
}
]
},
'horizontalAlignment': 'CENTER'
}
]
}
],
'fixedFooter': {
'primaryButton': {
'text': 'OK',
'onClick': {
'action': {
'function': 'closeDialog'
}
}
}
}
}
}
}
}
}
}
Contributors
This sample is maintained by Google with the help of Google Developer Experts.
Next steps