如何整合 Dialogflow 與 BigQuery

如何整合 Dialogflow 與 BigQuery

程式碼研究室簡介

subject上次更新時間:10月 8, 2020
account_circle作者:Joanna Smith

1. 簡介

本文將說明 Dialogflow 如何與 BigQuery 連線,並儲存對話式服務期間蒐集的資訊。我們會使用您在之前的研究室建立的代理程式預約排程器」。在代理程式的 GCP 專案中,我們會在 BigQuery 中建立資料集和資料表。接著,我們會將原始執行要求改成 BigQuery 資料集和資料表 ID。最後,我們會測試是否要在 BigQuery 中記錄互動行為。

以下是使用者、執行要求和 BigQuery 的事件序列圖表。

538029740db09f49.png

課程內容

  • 如何在 BigQuery 中建立資料集和資料表
  • 如何在 Dialogflow 執行要求中設定 BigQuery 連線詳細資料。
  • 如何測試執行要求

必要條件

  • Dialogflow 的基本概念和結構。請觀看下列影片,觀看有關基本對話設計的 Dialogflow 入門教學影片:
  • 使用 Dialogflow 建構預約排程器聊天機器人。
  • 瞭解 Dialogflow 中的實體。
  • 完成:將 Dialogflow 與 Google 日曆整合。

2. 在 BigQuery 中建立資料集和資料表

  1. 前往 Google Cloud 控制台
  2. 前往 Cloud 控制台中的選單圖示 ICON >大數據 >BigQuery
  3. 在左側窗格的「資源」下方,按一下專案 ID,選取後,畫面右側會顯示「建立資料集」
  4. 按一下「建立資料集」並為其命名。

be9f32a18ebb4a5b.png

  1. 建立資料集後,在左側面板中按一下資料集。畫面右側會顯示「建立資料表」。
  2. 按一下「建立資料表」,提供資料表名稱,然後按一下畫面底部的「建立資料表」。

d5fd99b68b7e62e0.png

  1. 建立表格後,請在左側面板中按一下表格。畫面右側會顯示「編輯結構定義」按鈕。
  2. 按一下「編輯結構定義」按鈕,然後點選「新增欄位」按鈕。新增「<日期>」欄位,並對「time」重複相同步驟和「type」。
  3. 記下「DatasetID」DatasetID&quot;和「tableID」DatasetID&quot;

e9d9abbe843823df.png

3. 將 BigQuery 連線詳細資料新增至 Dialogflow 執行要求

  1. 開啟 Dialogflow 代理程式並啟用 Fulfillment 內嵌編輯器。如需這項功能的相關協助,請參閱上一個 研究室。
  1. 請確認 Dialogflow 執行要求內嵌編輯器中的「package.json&quot;包含 BigQuery 依附元件。"@google-cloud/bigquery": "0.12.0"。閱讀本文時,請務必使用最新版的 BigQuery。
  2. 在 index.js 中建立「addToBigQuery」函式,在 BigQuery 資料表中新增日期、時間和預約類型。
  3. 在 index.js 檔案的 TODO 部分加入 projectIDdatasetIDtableID,以將 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);
       
}
       
);
     
}
   
});
 
});
}

從程式碼中瞭解事件序列

  1. 意圖對應會呼叫「makeAppointment&quot;」函式,在 Google 日曆上排定預約時間
  2. 在呼叫「addToBigQuery」的同一個函式中,函式,以便傳送要登入 BigQuery 的資料。

4. 測試聊天機器人和 BigQuery 資料表!

現在來測試聊天機器人吧!請使用模擬器進行測試,或者使用我們在前文介紹的網路或 Google Home 整合工具。

  • 使用者:「設定明天下午 2 點的車輛登記預約」
  • 聊天機器人回覆:「好,我來試試看。8 月 6 日下午 2 點沒問題!」

96d3784c103daf5e.png

  • 查看回應後的 BigQuery 資料表。使用查詢「SELECT * FROM projectID.datasetID.tableID

dcbc9f1c06277a21.png

5. 清除所用資源

如果您打算執行這個系列的其他研究室,在完成該系列的所有研究室後,請勿立即進行清理。

刪除 Dialogflow 代理程式

  • 按一下現有服務專員旁邊的齒輪圖示 30a9fea7cfa77c1a.png

520c1c6bb9f46ea6.png

  • 在「General」分頁中,向下捲動到底部,然後按一下 [Delete this Agent]
  • 在隨即出現的視窗中輸入 DELETE,然後按一下「Delete」(刪除)

6. 恭喜!

您已建立聊天機器人並將其與 BigQuery 整合,以便取得深入分析結果。你已成為聊天機器人開發人員!

歡迎參考下列其他資源:

1217326c0c490fa.png