1. 簡介
本文將說明 Dialogflow 如何連結至 BigQuery,以及如何儲存對話期間收集的資訊。我們會使用在先前的實驗室「預約排程」中建立的相同 Agent。我們會在代理程式的 GCP 專案中,於 BigQuery 建立資料集和資料表。接著,我們會使用 BigQuery 資料集和資料表 ID 編輯原始完成動作。最後,我們會測試互動是否記錄在 BigQuery 中。
下圖顯示從使用者到出貨和 BigQuery 的事件順序。

課程內容
- 如何在 BigQuery 中建立資料集和資料表
- 如何在 Dialogflow 實現中設定 BigQuery 連線詳細資料。
- 如何測試執行要求
必要條件
- Dialogflow 的基本概念和結構。如需涵蓋基本對話設計的 Dialogflow 入門教學影片,請觀看下列影片:
- 使用 Dialogflow 建構預約排程器聊天機器人。
- 瞭解 Dialogflow 中的實體。
- 完成:將 Dialogflow 與 Google 日曆整合。
2. 在 BigQuery 中建立資料集和資料表
- 前往 Google Cloud 控制台
- 在 Cloud 控制台中,依序前往「選單」圖示 ☰ >「大數據」>「BigQuery」
- 在左側窗格的「資源」下方,按一下專案 ID,選取後右側會顯示「建立資料集」
- 按一下「建立資料集」,然後命名。

- 建立資料集後,請在左側面板中點選該資料集。右側會顯示「CREATE TABLE」。
- 按一下「CREATE TABLE」,提供資料表名稱,然後按一下畫面底部的「Create table」。

- 資料表建立完成後,請點選左側面板中的資料表。右側會顯示「編輯結構定義」按鈕。
- 按一下「編輯結構定義」按鈕,然後按一下「新增欄位」按鈕。新增「date」欄位,並對「time」和「type」重複相同步驟。
- 請記下「DatasetID」和「tableID」

3. 將 BigQuery 連線詳細資料新增至 Dialogflow Fulfillment
- 開啟 Dialogflow 代理程式,並啟用執行要求內嵌編輯器。如需這方面的協助,請參閱上一個 實驗室。
- 請確認 Dialogflow 實現內嵌編輯器中的「package.json」包含 BigQuery 依附元件。"@google-cloud/bigquery": "0.12.0"。請務必使用本文發布時的最新版 BigQuery。
- 在 index.js 中建立「addToBigQuery」函式,將日期、時間和預約類型新增至 BigQuery 表格。
- 在 index.js 檔案的 TODO 區段中新增 projectID、datasetID 和 tableID,將 BigQuery 資料表和資料集正確連結至服務。
{
"name": "dialogflowFirebaseFulfillment",
"description": "Dialogflow fulfillment for the bike shop sample",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "6"
},
"scripts": {
"lint": "semistandard --fix \"**/*.js\"",
"start": "firebase deploy --only functions",
"deploy": "firebase deploy --only functions"
},
"dependencies": {
"firebase-functions": "2.0.2",
"firebase-admin": "^5.13.1",
"actions-on-google": "2.2.0",
"googleapis": "^27.0.0",
"dialogflow-fulfillment": "0.5.0",
"@google-cloud/bigquery": "^0.12.0"
}
}
'use strict';
const functions = require('firebase-functions');
const {google} = require('googleapis');
const {WebhookClient} = require('dialogflow-fulfillment');
const BIGQUERY = require('@google-cloud/bigquery');
// Enter your calendar ID below and service account JSON below
const calendarId = "XXXXXXXXXXXXXXXXXX@group.calendar.google.com";
const serviceAccount = {}; // Starts with {"type": "service_account",...
// Set up Google Calendar Service account credentials
const serviceAccountAuth = new google.auth.JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: 'https://www.googleapis.com/auth/calendar'
});
const calendar = google.calendar('v3');
process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
const timeZone = 'America/Los_Angeles';
const timeZoneOffset = '-07:00';
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log("Parameters", agent.parameters);
const appointment_type = agent.parameters.AppointmentType;
// Function to create appointment in calendar
function makeAppointment (agent) {
// Calculate appointment start and end datetimes (end = +1hr from start)
const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
const appointmentTimeString = dateTimeStart.toLocaleString(
'en-US',
{ month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
);
// Check the availability of the time, and make an appointment if there is time on the calendar
return createCalendarEvent(dateTimeStart, dateTimeEnd, appointment_type).then(() => {
agent.add(`Ok, let me see if we can fit you in. ${appointmentTimeString} is fine!.`);
// Insert data into a table
addToBigQuery(agent, appointment_type);
}).catch(() => {
agent.add(`I'm sorry, there are no slots available for ${appointmentTimeString}.`);
});
}
let intentMap = new Map();
intentMap.set('Schedule Appointment', makeAppointment);
agent.handleRequest(intentMap);
});
//Add data to BigQuery
function addToBigQuery(agent, appointment_type) {
const date_bq = agent.parameters.date.split('T')[0];
const time_bq = agent.parameters.time.split('T')[1].split('-')[0];
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
//const projectId = '<INSERT your own project ID here>';
//const datasetId = "<INSERT your own dataset name here>";
//const tableId = "<INSERT your own table name here>";
const bigquery = new BIGQUERY({
projectId: projectId
});
const rows = [{date: date_bq, time: time_bq, type: appointment_type}];
bigquery
.dataset(datasetId)
.table(tableId)
.insert(rows)
.then(() => {
console.log(`Inserted ${rows.length} rows`);
})
.catch(err => {
if (err && err.name === 'PartialFailureError') {
if (err.errors && err.errors.length > 0) {
console.log('Insert errors:');
err.errors.forEach(err => console.error(err));
}
} else {
console.error('ERROR:', err);
}
});
agent.add(`Added ${date_bq} and ${time_bq} into the table`);
}
// Function to create appointment in google calendar
function createCalendarEvent (dateTimeStart, dateTimeEnd, appointment_type) {
return new Promise((resolve, reject) => {
calendar.events.list({
auth: serviceAccountAuth, // List events for time period
calendarId: calendarId,
timeMin: dateTimeStart.toISOString(),
timeMax: dateTimeEnd.toISOString()
}, (err, calendarResponse) => {
// Check if there is a event already on the Calendar
if (err || calendarResponse.data.items.length > 0) {
reject(err || new Error('Requested time conflicts with another appointment'));
} else {
// Create event for the requested time period
calendar.events.insert({ auth: serviceAccountAuth,
calendarId: calendarId,
resource: {summary: appointment_type +' Appointment', description: appointment_type,
start: {dateTime: dateTimeStart},
end: {dateTime: dateTimeEnd}}
}, (err, event) => {
err ? reject(err) : resolve(event);
}
);
}
});
});
}
瞭解程式碼中的事件順序
- 意圖對應會呼叫「makeAppointment」函式,在 Google 日曆中安排預約時間
- 在同一個函式中,系統會呼叫「addToBigQuery」函式,將資料傳送至 BigQuery 進行記錄。
4. 測試聊天機器人和 BigQuery 資料表!
讓我們測試聊天機器人,您可以在模擬器中測試,或使用我們在先前文章中學到的網頁或 Google Home 整合功能。
- 使用者:「Set an appointment for vehicle registration at 2pm tomorrow」(預約明天下午 2 點的車輛登記服務)
- 聊天機器人回覆:「好的,我看看是否能安排時間。8 月 6 日下午 2 點可以!」

- 在回應後檢查 BigQuery 資料表。使用「SELECT * FROM
projectID.datasetID.tableID」查詢

5. 清除
如果您打算完成本系列的其他實驗室,請勿立即清除資源,而是在完成本系列的所有實驗室後再執行這項操作。
刪除 Dialogflow 代理程式
- 按一下現有代理程式旁邊的齒輪圖示


- 在「一般設定」分頁中,向下捲動至底部,然後點選「刪除這個代理程式」。
- 在隨即顯示的視窗中輸入「DELETE」,然後按一下「刪除」。
6. 恭喜!
您建立了聊天機器人,並與 BigQuery 整合,以取得洞察資料。你現在是聊天機器人開發人員!
歡迎查看其他資源:
- 請前往 Dialogflow GitHub 頁面查看程式碼範例。
