如何将 Dialogflow 与 BigQuery 集成

如何将 Dialogflow 与 BigQuery 集成

关于此 Codelab

subject上次更新时间:10月 8, 2020
account_circleJoanna Smith 编写

1. 简介

在本文中,我们将了解 Dialogflow 如何与 BigQuery 连接,以及如何存储对话式体验期间收集的信息。我们将使用在之前的实验中创建的同一个 Agent。Appointment Scheduler”。在代理的 GCP 项目中,我们将在 BigQuery 中创建一个数据集和表。然后,我们将使用 BigQuery 数据集和表 ID 修改原始 fulfillment。最后,我们将测试 BigQuery 中是否记录了互动情况。

下面是从用户到 fulfillment 和 BigQuery 的事件序列图。

538029740db09f49

学习内容

  • 如何在 BigQuery 中创建数据集和表
  • 如何在 Dialogflow 执行中设置 BigQuery 连接详细信息。
  • 如何测试 fulfillment

前提条件

  • Dialogflow 的基本概念和结构。如需查看介绍基本对话设计的 Dialogflow 入门教程视频,请观看以下视频:
  • 使用 Dialogflow 构建预约安排程序聊天机器人。
  • 了解 Dialogflow 中的实体。
  • Fulfillment:将 Dialogflow 与 Google 日历集成。

2. 在 BigQuery 中创建数据集和表

  1. 前往 Google Cloud 控制台
  2. 在 Cloud Console 中,转到菜单图标 ☰ > 大数据 > BigQuery
  3. 在左侧窗格的“资源”下,点击项目 ID,选择后,您会在右侧看到“创建数据集”
  4. 点击“创建数据集”,然后为其命名。

be9f32a18ebb4a5b.png

  1. 创建数据集后,在左侧面板中点击相应数据集。您将在右侧看到“创建表”。
  2. 点击“创建表”,提供表名称,然后点击屏幕底部的“创建表”。

d5fd99b68b7e62e0.png

  1. 创建好表格后,点击左侧面板中的表格。您会在右侧看到修改架构按钮。
  2. 点击“编辑架构”按钮,并点击“添加字段”按钮。添加“日期”字段,并为“时间”和“类型”重复相同的步骤。
  3. 记下“DatasetID”和“tableID

e9d9abbe843823df.png

3. 向 BigQuery Fulfillment 添加 BigQuery 连接详情

  1. 打开 Dialogflow 代理并启用 Fulfillment 内嵌编辑器。如需这方面的帮助,请参阅上一个 实验。
  1. 确保 Dialogflow fulfillment 内嵌编辑器中的“package.json”包含一个 BigQuery 依赖项。"@google-cloud/bigquery": "0.12.0"。在阅读本文时,请确保您使用的是最新版本的 BigQuery。
  2. 在 index.js 中创建“addToBigQuery”函数,以在 BigQuery 表中添加日期、时间和预约类型。
  3. 在 index.js 文件的 TODO 部分添加 projectIDdatasetIDtableID,以正确将您的 BigQuery 表和数据集连接到您的 fulfillment。
{
 
"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”函数来在 Google 日历上安排预约
  2. 在同一函数内,系统会对“addToBigQuery”函数进行调用,以发送要登录到 BigQuery 的数据。

4. 测试您的聊天机器人和 BigQuery 表!

我们来测试一下聊天机器人,您可以在模拟器中对其进行测试,也可以使用我们在前面的文章中学到的网页或 Google Home 集成。

  • 用户:“Set an appointment for vehicle registration at 2pm tomorrow”
  • 聊天机器人回答:“Ok, let me see if we can fit you in.August 6, 2 PM is fine!”。

96d3784c103daf5e

  • 请在回答之后查看 BigQuery 表。使用查询“SELECT * FROM projectID.datasetID.tableID

dcbc9f1c06277a21.png

5. 清理

如果您计划学习本系列中的其他实验,请不要立即进行清理,可在本系列中的所有实验结束后清理。

删除 Dialogflow 代理

  • 点击现有代理旁边的齿轮图标 30a9fea7cfa77c1a

520c1c6bb9f46ea6

  • 常规 (General) 标签页中,向下滚动到底部,然后点击删除此代理 (Delete this Agent)。
  • 在出现的窗口中输入 DELETE,然后点击删除

6. 恭喜!

您创建了一个聊天机器人,并将其与 BigQuery 集成以获取数据洞见。您现在已经是聊天机器人开发者了!

查看其他资源:

1217326c0c490fa.png