Việc đọc ghi dữ liệu vào trong các file excel rất quang trọng để chatbot của bạn có thể lưu lại những thông tin trong quá trình trao đổi với con người.
Thông thường cho dù chat bot bạn có sử dụng các context thì sau khi cuộc trò chuyện kết thúc. Dữ liệu sẽ không tồn tại nữa. Và khi chính người đó quay lại, thì bot của bạn chẳng còn nhớ những gì đã trao đổi trước đó.
Để làm cho chatbot bạn thông minh, thì những thông tin của khách hàng bạn có thể lưu lại trên file excel của google spreadsheet. Để đơn giản nhất mình hướng dẫn các bạn sử dụng API của bên thứ 3. Họ cung cấp API miễn phí rất tuyệt vời giúp kết nối đơn giản và bạn chỉ cần phải trả phí khá thấp khi mở rộng thêm dữ liệu
Tạo bảng dữ liệu google Spreadsheet
Trước tiên chúng ta cần tạo 1 bảng dữ liệu và cung cấp 1 số thông tin mẫu để thử nghiệm việc chatbot kết nối và đọc dữ liệu từ file này. Trong bảng mình test mẫu có tên là “Test_Chatbot_dialogflow” và có 3 trường thông tin là tên, email và số điện thoại.
Khi tạo bảng này chúng ta cần đường dẫn cho phép người nhận đường dẫn này có thể edit dữ liệu trong bảng

Liên kết google spreadsheet với Sheet.best để lấy API. Sheet.best cung cấp dịch vụ thực hiện các công đoạn kết nối với google sheet và trả về cho chúng ta API để chatbot đọc ghi dữ liệu
Bạn vào web https://sheet.best và dán URL đã copy ở trên vào và nhấn nút TRY IT NOW. Trang web sẽ tạo cho bạn 1 API

API đó là gì, thực ra nó lấy dữ liệu trong bảng spreadsheet của bạn và tạo thành 1 dữ liệu kiểu JSON. Bạn có thể copy đoạn API trên và đặt vào browser sẽ thấy như vậy
[{"Name":"Quốc Vinh","Phone":"937777111","Email":"ngoquocvinh@live.com"},{"Name":"Tuấn Anh","Phone":"912888123","Email":"tuananh@gmail.com"},{"Name":"Minh Trí","Phone":"936777111","Email":"minhtri@gmail.com"},{"Name":"Nhật Nam","Phone":"988111222","Email":"nhatnam@gmail.com"}]
So sánh lại các cột và hàng trên bảng dữ liệu ta nhập ban đầu thì hoàn toàn tương thích. Như vậy việc chuẩn bị về tạo bảng spreadsheet và API kết nối đã hoàn tất
Lập trình Dialogfow kết nối Spreadsheet
Để kết nối API ngoài chúng ta cần thư viện axios. Khai báo thư viện này trong 2 file index.js và package.json ở phần Dialogfulfillment Inline Editor giống hình dưới đây

Code phần index.js
const axios = require('axios');
Code phần package.json. Thực ra chỉ cần dòng cuối cùng như hình trên thôi nhưng có dấu phẩy sợ bạn nhầm nên mình copy luôn tòa bộ đoạn code trong file này để đảm bảo không bị sai sót
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0",
"axios":"0.19.0"
}
}
Phần khai báo thư viện đã xong tiếp theo chúng ta viết hàm trong index.js để lây dữ liệu trong API
Đọc dữ liệu google spreadsheet thông qua sheet.best API
Trong phần này chúng ta sẽ tạo ra 1 intent. Huấn luyên cho nói hỏi bạn tên gì. Nếu tên bạn có trong spreadsheet thì nó sẽ hiển thị số điện thoại và email bạn ra.
//hàm mở cổng
function GetGoogleSheet(){
return axios.get('https://sheet.best/api/sheets/c40e53a9-b13f-4ab2-baab-1d296f0f021c');
}
//hàm cho intent readGoogleSheet
function readGoogleSheet(agent) {
const name = agent.parameters.Name;
return GetGoogleSheet().then(res =>{
res.data.map(person => {
if(person.Name === name) {
agent.add( `Ten: ${person.Name}`);
agent.add(`Email: ${person.Email}`);
agent.add(`SDT: ${person.Phone}`);
}
else agent.add(`không có trong dữ liệu`);
});
});
}
//map intent cho hàm readGooglesheet
intentMap.set('ReadGoogleSheet', readGoogleSheet);
agent.handleRequest(intentMap);
Dưới đây là intent đọc dữ liệu từ google sheet. Bạn chỉ cần training đơn giản để test việc lấy thông tin từ API thôi. Nhớ bật webhook call cho intent này nha. Các bạn chưa biết webhook hay lập trình Fulfillment thì hãy xem các bài chia sẽ trước đây của mình

Ghi dữ liệu vào Google spreadsheet
Tương tự việc đọc dữ liệu, thì ghi dữ liệu cũng vậy. Chúng ta sử dụng hàm axios.post(url, data) trong thư viện axios đã khai báo ở trên
Bạn tạo ra 1 intent và lấy dữ liệu của người dùng lưu vào cột name, phone, email (phần này đơn giản nên mình không giải thích dài dòng). Tiếp theo đoạn code này handle intent ghi dữ liệu (writeGoogleSheet). Tham số URL chính là API do sheet.best tạo cho bạn ở trên
function writeGoogleSheet(agent){
const {name, email, phone} = agent.parameters;
const data = [{
Name: name,
Email: email,
Phone: phone
}];
axios.post('https://sheet.best/api/sheets/c40e53a9-b13f-4ab2-baab-1d296f0f021c',data);
}
//map vào intent
intentMap.set('WriteGoogleSheet', writeGoogleSheet);
agent.handleRequest(intentMap);
Sau đây là toàn bộ code của File index.js
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Image, Suggestion} = require('dialogflow-fulfillment');
const axios = require('axios');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function GetGoogleSheet(){
return axios.get('https://sheet.best/api/sheets/c40e53a9-b13f-4ab2-baab-1d296f0f021c');
}
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function readGoogleSheet(agent) {
const name = agent.parameters.Name;
return GetGoogleSheet().then(res =>{
res.data.map(person => {
if(person.Name === name) {
agent.add( `Ten: ${person.Name}`);
agent.add(`Email: ${person.Email}`);
agent.add(`SDT: ${person.Phone}`);
}
});
});
}
function writeGoogleSheet(agent){
const {name, age, email, phone} = agent.parameters;
const data = [{
Name: name,
Age: age,
Email: email,
Phone: phone
}];
axios.post('https://sheet.best/api/sheets/c40e53a9-b13f-4ab2-baab-1d296f0f021c',data);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('ReadGoogleSheet', readGoogleSheet);
intentMap.set('WriteGoogleSheet', writeGoogleSheet);
agent.handleRequest(intentMap);
});
Và đây là code của File Package. Json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0",
"axios":"0.19.0"
}
}
Nếu bạn thấy bài viết mình hữu ích, hãy chia sẽ cho cộng đồng cùng xem để giúp nhau phát triển
Thank you
Ngo Quoc Vinh
Thanks anh, bài viết rất hay, lâu lắm rồi mới thấy anh đăng bài mới?
Cám ơn bạn nha. Do mình dạo này hơi bận nên hok có nhiều thời gian để viết bài 🙂
Đây là chuỗi bài về dialogflow tiếng việt hay nhất, cụ thể nhất (hầu như duy nhất), nhiều kỹ thuật hứu ích. Có mọt chỗ a cho e hỏi là lm thế nài để tạo intent lấy tên, tuổi, địa chỉ của người dùng rồi lưu vào parameter ạ ? Tại e thấy tên hay địa chỉ thì rất khó để khai bóa trong entity nên ko biết bot nó có bóc tách ra rồi gán vào paramater đc ko. Anh có kỹ thuật hay chuỗi bài hướng dẫn nào vấn đề này giúp e với đc ko ah.
Thanks a 3000 :))
Chào bạn
Bạn tham khảo bài slotfilling nha. Để đảm bảo bắt dữ liệu đúng.
và bài Nâng cao khả năng bắt tình huống chính xác cho intent – Dialogflow
Dữ liệu bạn có thể lưu lại trên Spreadsheet