1. परिचय
इस लेख में हम जानेंगे कि Dialogflow, BigQuery से कैसे कनेक्ट होता है और बातचीत के दौरान इकट्ठा की गई जानकारी को कैसे सेव करता है. हम उसी एजेंट का इस्तेमाल करेंगे जिसे हमने पिछले लैब " अपॉइंटमेंट शेड्यूलर" में बनाया था. हम एजेंट के GCP प्रोजेक्ट में, BigQuery में एक डेटासेट और एक टेबल बनाएंगे. इसके बाद, हम BigQuery डेटासेट और टेबल आईडी की मदद से, ओरिजनल फ़ुलफ़िलमेंट में बदलाव करेंगे. आखिर में, हम यह जांच करेंगे कि इंटरैक्शन, BigQuery में रिकॉर्ड हो रहे हैं या नहीं.
यहां उपयोगकर्ता से लेकर फ़ुलफ़िलमेंट और BigQuery तक के इवेंट का क्रमवार डायग्राम दिया गया है.

आपको क्या सीखने को मिलेगा
- BigQuery में डेटासेट और टेबल बनाने का तरीका
- Dialogflow फ़ुलफ़िलमेंट में BigQuery कनेक्शन की जानकारी कैसे सेट अप करें.
- फ़ुलफ़िलमेंट की जांच करने का तरीका
ज़रूरी शर्तें
- Dialogflow के बुनियादी कॉन्सेप्ट और कंस्ट्रक्ट. Dialogflow के शुरुआती ट्यूटोरियल वीडियो में बातचीत के बुनियादी डिज़ाइन के बारे में बताया गया है. इन्हें देखने के लिए, यहां दिए गए वीडियो देखें:
- Dialogflow का इस्तेमाल करके, अपॉइंटमेंट शेड्यूल करने वाला चैटबॉट बनाना.
- Dialogflow में मौजूद इकाइयों के बारे में जानकारी.
- फ़ुलफ़िलमेंट: Dialogflow को Google Calendar के साथ इंटिग्रेट करें.
2. BigQuery में डेटासेट और टेबल बनाना
- Google Cloud Console पर जाएं
- Cloud Console में, मेन्यू आइकॉन ☰ > Big Data > BigQuery पर जाएं
- बाईं ओर मौजूद रिसॉर्स में जाकर, प्रोजेक्ट आईडी पर क्लिक करें. इसके बाद, आपको दाईं ओर डेटासेट बनाएं का विकल्प दिखेगा
- डेटासेट बनाएं पर क्लिक करें और उसका नाम डालें.

- डेटासेट बन जाने के बाद, बाएं पैनल में जाकर उस पर क्लिक करें. आपको दाईं ओर, CREATE TABLE दिखेगा.
- CREATE TABLE पर क्लिक करें. इसके बाद, टेबल का नाम डालें और स्क्रीन पर सबसे नीचे मौजूद, Create table पर क्लिक करें.

- टेबल बन जाने के बाद, बाईं ओर मौजूद पैनल में जाकर टेबल पर क्लिक करें. आपको दाईं ओर "स्कीमा में बदलाव करें" बटन दिखेगा.
- 'स्कीमा में बदलाव करें' बटन पर क्लिक करें. इसके बाद, 'फ़ील्ड जोड़ें' बटन पर क्लिक करें. "date" फ़ील्ड जोड़ें. इसके बाद, "time" और "type" के लिए भी यही प्रोसेस दोहराएं.
- "DatasetID" और "tableID" को नोट करें

3. Dialogflow Fulfillment में BigQuery कनेक्शन की जानकारी जोड़ना
- Dialogflow एजेंट खोलें और फ़ुलफ़िलमेंट इनलाइन एडिटर चालू करें. अगर आपको इस बारे में मदद चाहिए, तो पिछले लैब पर जाएं .
- पक्का करें कि Dialogflow फ़ुलफ़िलमेंट के इनलाइन एडिटर में मौजूद "package.json" में BigQuery डिपेंडेंसी शामिल हो. "@google-cloud/bigquery": "0.12.0". पक्का करें कि इस लेख को पढ़ते समय, BigQuery के नए वर्शन का इस्तेमाल किया जा रहा हो.
- BigQuery टेबल में तारीख, समय, और अपॉइंटमेंट का टाइप जोड़ने के लिए, index.js में "addToBigQuery" फ़ंक्शन बनाएं.
- अपनी BigQuery टेबल और डेटासेट को फ़ुलफ़िलमेंट से सही तरीके से कनेक्ट करने के लिए, index.js फ़ाइल के TODO सेक्शन में projectID, datasetID, और tableID जोड़ें.
{
"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);
}
);
}
});
});
}
कोड से इवेंट के क्रम को समझना
- इंटेंट मैप, Google Calendar पर अपॉइंटमेंट शेड्यूल करने के लिए "makeAppointment" फ़ंक्शन को कॉल करता है
- इसी फ़ंक्शन में, "addToBigQuery" फ़ंक्शन को कॉल किया जाता है, ताकि BigQuery में लॉग किए जाने वाले डेटा को भेजा जा सके.
4. अपने चैटबॉट और BigQuery टेबल की जांच करें!
आइए, अपने चैटबॉट को टेस्ट करें. इसे सिम्युलेटर में टेस्ट किया जा सकता है. इसके अलावा, वेब या Google Home इंटिग्रेशन का इस्तेमाल किया जा सकता है. इसके बारे में हमने पिछले लेखों में सीखा था.
- उपयोगकर्ता: "कल दोपहर 2 बजे वाहन के रजिस्ट्रेशन का अपॉइंटमेंट सेट करो"
- चैटबॉट का जवाब: "ठीक है, हम देखते हैं कि क्या हम आपको शामिल कर सकते हैं. 6 अगस्त, दोपहर 2 बजे ठीक है!."

- जवाब मिलने के बाद, BigQuery टेबल देखें. "SELECT * FROM
projectID.datasetID.tableID" क्वेरी का इस्तेमाल करें

5. साफ़-सफ़ाई सेवा
अगर आपको इस सीरीज़ के अन्य लैब भी पूरे करने हैं, तो अभी क्लीनअप न करें. इस सीरीज़ के सभी लैब पूरे करने के बाद क्लीनअप करें.
Dialogflow एजेंट मिटाना
- अपने मौजूदा एजेंट के बगल में मौजूद गियर आइकॉन
पर क्लिक करें

- सामान्य टैब में, सबसे नीचे तक स्क्रोल करें. इसके बाद, इस एजेंट को मिटाएं पर क्लिक करें.
- दिखने वाली विंडो में DELETE टाइप करें. इसके बाद, मिटाएं पर क्लिक करें.
6. बधाई हो!
आपने एक चैटबॉट बनाया है और उसे अहम जानकारी पाने के लिए BigQuery के साथ इंटिग्रेट किया है. अब आप चैटबॉट डेवलपर बन गए हैं!
इन अन्य संसाधनों को देखें:
- Dialogflow Github पेज पर कोड सैंपल देखें.
