Reputation: 921
I am developing an app on Actions on Google and I've noticed that when using the Dialogflow Fulfillment library I can't save data between conversations. Here is the code using the WebhookClient:
const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment');
exports.aog_app = functions.https.onRequest((request, response)=>{
let agent = new WebhookClient({request, response});
let intentMap = new Map();
intentMap.set('Default Welcome Intent', (agent)=>{
agent.add("hello there!") ;
});
intentMap.set('presentation', (agent)=>{
let conv = agent.conv();
let counter = conv.data.counter;
console.log("counter", counter)
if(counter){
conv.data.counter = counter+1;
}else{
conv.data.counter = 1;
}
agent.add("counter is "+counter) ;
});
agent.handleRequest(intentMap)
});
counter
remains undefined on each turn.
But when using the Action on Google Nodejs Library I can save data without issues:
const {
dialogflow,
SimpleResponse,
BasicCard,
Permission,
Suggestions,
BrowseCarousel,
BrowseCarouselItem,
Button,
Carousel,
DateTime,
Image,
DialogflowConversation
} = require('actions-on-google');
const app = dialogflow({debug: true});
app.intent('Default Welcome Intent', (conv)=>{
conv.ask("hello there!");
});
app.intent('presentation', (conv)=>{
let counter = conv.data.counter;
console.log("counter", counter)
if(counter){
conv.data.counter = counter+1;
}else{
conv.data.counter = 1;
}
conv.ask("counter is "+counter)
})
exports.aog_app = functions.https.onRequest(app);
counter
is incremented on each turn.
Is there a way to save data between conversations using the Dialogflow fulfillment library?
Upvotes: 1
Views: 993
Reputation: 1435
you need to add the conv
back to agent after you update the conv.data
agent.add(conv);
Upvotes: 3
Reputation: 668
Google's Dialogflow team published a code sample showing how to implement data persistence by integrating Google Cloud's Firebase Cloud Firestore.
You can find the sample here: https://github.com/dialogflow/fulfillment-firestore-nodejs
This is (probably) the code you're looking for:
function writeToDb (agent) {
// Get parameter from Dialogflow with the string to add to the database
const databaseEntry = agent.parameters.databaseEntry;
// Get the database collection 'dialogflow' and document 'agent' and store
// the document {entry: "<value of database entry>"} in the 'agent' document
const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
return db.runTransaction(t => {
t.set(dialogflowAgentRef, {entry: databaseEntry});
return Promise.resolve('Write complete');
}).then(doc => {
agent.add(`Wrote "${databaseEntry}" to the Firestore database.`);
}).catch(err => {
console.log(`Error writing to Firestore: ${err}`);
agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
});
}
Upvotes: 0