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 में, मेन्यू आइकॉन रजिस्ट्रेंट > पर जाएं बिग डेटा > BigQuery
- बाएं पैनल में मौजूद 'संसाधन' सेक्शन में जाकर, प्रोजेक्ट आईडी पर क्लिक करें. इसके बाद, आपको दाईं ओर डेटासेट बनाएं दिखेगा
- 'डेटासेट बनाएं' पर क्लिक करें और इसे नाम दें.
- डेटासेट बनाने के बाद, बाएं पैनल में जाकर उस पर क्लिक करें. आपको दाईं ओर 'टेबल बनाएं' दिखेगा.
- 'टेबल बनाएं' पर क्लिक करके, टेबल को नाम दें. इसके बाद, स्क्रीन के सबसे नीचे मौजूद 'टेबल बनाएं' पर क्लिक करें.
- टेबल बनाने के बाद, बाएं पैनल में मौजूद टेबल पर क्लिक करें. आपको दाईं ओर "स्कीमा में बदलाव करें" बटन दिखेगा.
- 'स्कीमा में बदलाव करें' बटन पर क्लिक करें और 'फ़ील्ड जोड़ें' बटन पर क्लिक करें. "तारीख" जोड़ें फ़ील्ड शुरू करें और इसे "time" के लिए दोहराएं और "टाइप" होना चाहिए.
- "DatasetID" और "DatasetID" को ध्यान में रखें
3. डायलॉग बॉक्स के फ़ुलफ़िलमेंट में BigQuery कनेक्शन की जानकारी जोड़ें
- Dialogflow Agent खोलें और फ़ुलफ़िलमेंट इनलाइन एडिटर को चालू करें. अगर आपको इसमें मदद चाहिए, तो पिछले लैब पर जाएं .
- पक्का करें कि Dialogflow के फ़ुलफ़िलमेंट इनलाइन एडिटर के "package.json" में BigQuery डिपेंडेंसी शामिल हो. "@google-cloud/bigquery": "0.12.0". पक्का करें कि इस लेख को फ़ॉलो करते समय, आपके पास 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" पर कॉल किया जाता है फ़ंक्शन का इस्तेमाल करना होगा.
4. अपने चैटबॉट और BigQuery टेबल की जांच करें!
चलिए, हमारे चैटबॉट को आज़माते हैं. इसे सिम्युलेटर में टेस्ट किया जा सकता है. इसके अलावा, पिछले लेखों में बताए गए वेब या Google Home इंटिग्रेशन का इस्तेमाल करके भी ऐसा किया जा सकता है.
- उपयोगकर्ता: "वाहन के रजिस्ट्रेशन के लिए कल दोपहर दो बजे का अपॉइंटमेंट सेट करो"
- चैटबॉट का जवाब: "ठीक है, मुझे देखते हैं कि क्या हम आपको इसके लिए तैयार कर सकते हैं. 6 अगस्त, दोपहर 2 बजे ठीक है!."
- जवाब के बाद BigQuery टेबल देखें. "
projectID.datasetID.tableID
से * चुनें" क्वेरी का इस्तेमाल करें
5. साफ़-सफ़ाई सेवा
अगर आपको इस सीरीज़ में अन्य लैब बनाने की योजना है, तो क्लीनअप अभी न करें. ऐसा सिर्फ़ तब करें, जब सीरीज़ की सभी लैब पूरी हो गई हों.
Dialogflow एजेंट को मिटाना
- अपने मौजूदा एजेंट के बगल में मौजूद, गियर आइकॉन
पर क्लिक करें
- General टैब में स्क्रोल करके नीचे जाएं और इस एजेंट को मिटाएं पर क्लिक करें.
- इसके बाद दिखने वाली विंडो में, मिटाएं टाइप करें और मिटाएं पर क्लिक करें.
6. बधाई हो!
अहम जानकारी पाने के लिए, आपने एक चैटबॉट बनाया है और उसे BigQuery के साथ इंटिग्रेट किया है. अब आप चैटबॉट डेवलपर हैं!
ये अन्य संसाधन देखें:
- Dialogflow GitHub पेज पर कोड के नमूने देखें.