Node.js 在与Google上的Actions对话中保存数据
我正在Google上开发一个关于Actions的应用程序,我注意到在使用时,我无法在对话之间保存数据。 以下是使用WebhookClient的代码:Node.js 在与Google上的Actions对话中保存数据,node.js,dialogflow-es,actions-on-google,Node.js,Dialogflow Es,Actions On Google,我正在Google上开发一个关于Actions的应用程序,我注意到在使用时,我无法在对话之间保存数据。 以下是使用WebhookClient的代码: const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment'); exports.aog_app = functions.https.onRequest((request, response)=>{ let agent = new WebhookC
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)
});
计数器在每次转弯时保持未定义状态
但当使用时,我可以毫无问题地保存数据:
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);
计数器
每转一圈递增
有没有一种方法可以使用Dialogflow实现库在对话之间保存数据?谷歌的Dialogflow团队发布了一个代码示例,展示了如何通过集成谷歌云的Firebase云Firestore来实现数据持久化
您可以在此处找到示例:
这(可能)就是您要查找的代码:
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.`);
});
}
函数writeToDb(代理){
//使用要添加到数据库的字符串从Dialogflow获取参数
const databaseEntry=agent.parameters.databaseEntry;
//获取数据库集合“dialogflow”和文档“agent”并存储
//“代理”文档中的文档{条目:}
const dialogflowAgentRef=db.collection('dialogflow').doc('agent');
返回db.runTransaction(t=>{
t、 set(dialogflowAgentRef,{entry:databaseEntry});
返回承诺。解决(“填写完整”);
})。然后(doc=>{
add(`writed“${databaseEntry}”到Firestore数据库。`);
}).catch(错误=>{
log(`写入Firestore时出错:${err}`);
add(`未能将“${databaseEntry}”写入Firestore数据库。`);
});
}
更新conv.data后,需要将conv
添加回代理
agent.add(conv);
是的,我知道您可以在DB上存储数据,但我认为使用agent.conv()对象与使用de AOG库时使用conv对象是一样的,因为agent.conv()返回DialogflowConversation对象。也许我误解了。我们在上下文中保存的这个参数在上下文的生命周期后会丢失,对吗?我们如何在一次对话中保存数据,就像在谷歌对象上执行操作时执行conv.data
一样?您有什么特别的原因不想在谷歌库中执行操作吗?如果您正在使用Dialogflow实现Google助手,那么这似乎非常有用。通过Google Node.js客户端库上的操作,conv.data应该允许您实例化自己的变量。例如,conv.data.hello=“hello world”会将字符串“hello world”存储在conv.data中的hello对象中,允许您稍后在对话中访问该值。这样行吗?我看到你正在起诉conv.data,它只在对话中保存数据。要在对话中保存数据,您应该使用conv.user.storage。这是一样的,conv.user.storage在使用AOG NodeJs库时起作用,但在使用Dialogflow实现库时不起作用。如果我在agent.add(“counter is”+counter)之后添加该代码代码>我得到以下错误:错误:未设置响应。这是否用于未作为对意图处理程序的承诺返回的异步调用中?请尝试将文本直接添加到conv
中conv.ask(“计数器是”+计数器)代码>