编码级别:中级
时长:25 分钟
项目类型:Google Chat 应用
目标
- 了解解决方案的用途。
- 了解 Apps 脚本服务在此解决方案中执行的操作。
- 设置环境。
- 设置脚本。
- 运行脚本。
关于此解决方案
在 Google 日历中,通过私信 (DM) 或 Google Chat 聊天室安排会议。您可以为会议设置特定详细信息,例如主题、开始时间或时长,或者使用默认设置进行即时会议安排。
运作方式
Chat 应用脚本使用斜杠命令和对话框从用户那里获取会议详细信息并安排日历活动。该脚本包含默认会议设置,您可以对这些设置进行自定义,以满足您的需要。
Apps 脚本服务
此解决方案使用以下服务:
- 日历服务 - 根据提供的会议信息创建日历活动。
- 基础服务 - 使用
Session
类获取脚本的时区。日历在安排活动时会使用此时区。
- 实用工具服务 - 设置日历活动日期的格式,并对活动 ID 进行编码以帮助获取活动网址。
前提条件
如需使用此示例,您需要满足以下前提条件:
- Google 帐号(Google Workspace 帐号可能需要管理员批准)。
- 一个能够访问互联网的网络浏览器。
- 具有一个 Google Cloud 项目。
设置您的环境
在 Google Cloud 控制台中打开您的 Cloud 项目
打开您打算用于此示例的 Cloud 项目(如果尚未打开):
- 在 Google Cloud 控制台中,前往选择项目页面。
选择 Cloud 项目
- 选择您要使用的 Google Cloud 项目。或者,点击创建项目,然后按照屏幕上的说明操作。如果您创建了 Google Cloud 项目,则可能需要为该项目启用结算功能。
启用 API
在使用 Google API 之前,您需要在 Google Cloud 项目中启用它们。您可以在单个 Google Cloud 项目中启用一个或多个 API。
所有 Chat 应用都需要配置同意屏幕。通过配置应用的 OAuth 权限请求页面,您可以定义 Google 向用户显示的内容并注册您的应用,以便日后发布应用。
-
在 Google Cloud 控制台中,依次点击“菜单”图标 menu
> API 和服务
> OAuth 同意屏幕。
转到 OAuth 同意屏幕
- 对于用户类型,选择内部,然后点击创建。
- 填写应用注册表单,然后点击保存并继续。
目前,您可以跳过添加范围的步骤,点击 Save and Continue(保存并继续)。
将来,当您创建要在 Google Workspace 组织外部使用的应用时,必须将用户类型更改为外部,然后添加您的应用所需的授权范围。
- 查看您的应用注册摘要。如要进行更改,请点击修改。如果应用注册看起来正常,请点击 Back to Dashboard。
设置脚本
创建 Apps 脚本项目
- 点击以下按钮打开在 Google Chat 中安排会议 Apps 脚本项目。
打开项目
- 点击概览 info_outline。
- 在概览页面上,点击“复制”图标 。
复制 Cloud 项目编号
-
在 Google Cloud 控制台中,依次点击“菜单”图标 menu
> IAM 和管理
> 设置。
转到“IAM 和管理”设置
- 在项目编号字段中,复制相应值。
设置 Apps 脚本项目的 Cloud 项目
- 在复制的 Apps 脚本项目中,点击项目设置 。
- 在 Google Cloud Platform (GCP) 项目下,点击更改项目。
- 在 GCP 项目编号中,粘贴 Google Cloud 项目编号。
- 点击设置项目。
创建测试部署
- 在复制的 Apps 脚本项目中,依次点击部署
> 测试部署。
- 复制 Head 部署 ID 以供后续步骤使用,然后点击 Done(完成)。
- 在 Google Cloud 控制台中,前往 Chat API 页面。
转到 Chat API
- 点击配置。
- 使用以下信息配置 Chat API:
- Name:
Meeting Scheduler
- 头像网址:添加指向一张最小尺寸为 256x256 像素的图片的网址。
- 说明:
Quickly create meetings.
- 功能:同时选中这两个复选框,即可让用户直接向应用发送消息并将其添加到聊天室。
- 连接设置:点击 Apps 脚本,然后输入头部署 ID。
- 斜杠命令:按照以下步骤为
/help
和 /schedule_Meeting
添加斜杠命令:
- 点击添加斜杠命令,并使用以下信息对其进行配置:
- Name:
/help
- 命令 ID:
1
- 说明:
Learn what this app does.
- 再次点击添加斜杠命令,并使用以下信息对其进行配置:
- Name:
/schedule_Meeting
- 命令 ID:
2
- 说明:
Schedule a meeting.
- 选中打开对话框复选框。
- 权限:选择您网域中的特定用户和群组,然后输入您的电子邮件地址。
- 点击保存,然后刷新页面。
- 在配置页面上的应用状态下,将状态设置为已发布 - 可供用户使用。
- 点击保存。
运行脚本
- 打开 Google Chat。
- 点击“发起聊天”图标 add。
- 搜索应用的名称
Meeting Scheduler
。
- 发送初始消息(例如
hello
),以提示授权。
当应用回复时,点击配置并向应用授权。如果 OAuth 同意屏幕显示“此应用未经验证”警告,请依次选择高级 >
转到 {项目名称}(不安全)以继续操作。
向应用发送 /schedule_Meeting
。
在对话框中,添加至少一个邀请对象的电子邮件地址。您可以更新其他字段或使用默认条目。
点击提交。
如要查看会议,请点击打开日历活动。
查看代码
如需查看此解决方案的 Apps 脚本代码,请点击下面的查看源代码:
查看源代码
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'
}
}
}
}
}
}
}
}
}
}
贡献者
此示例由 Google 在 Google 开发者专家的帮助下进行维护。
后续步骤