dialogflow-es - google user.storage 上的操作会随着用户的每个帖子调用而清除 - 在相同的覆盖范围内。我如何保存对话的数据?
问题描述
我正在尝试通过与 的对话来保存数据user.storage
,我正在访问user.storage
这样的内容:
app.post('/', express.json(), (req, res) => {
const agent = new WebhookClient({ request: req, response: res })
let personalD=new personalDetails(agent)
function personal_details(){
personalD.foo()
}
let intentMap = new Map()
intentMap.set('inform.PersonalDetails',personal_details)
agent.handleRequest(intentMap)
}
//that's the personalDetails class:
class PersonalDetails{
constructor(agent){
this.agent=agent;
this.conv=this.agent.conv();
}
foo() {
this.conv.user.storage.name=this.agent.parameters.name;
this.conv.user.storage.age=this.agent.parameters.age;
this.conv.user.storage.gender=this.agent.parameters.gender;
const gotname = this.conv.user.storage.name==''?0:1
const gotage = this.conv.user.storage.age==''?0:1
const gotgender =this.conv.user.storage.gender==''?0:1
const name=this.conv.user.storage.name;
const gender=this.conv.user.storage.gender;
if (gotname && !gotage&&!gotgender)
this.agent.add(`Ok, ${name}, How old are you? and what is you'r gender?`)
else if (gotname && gotage&&!gotgender)
this.agent.add(`Ok, ${name}, What gender you belong to`)
else if(gotname && !gotage&&gotgender)
this.agent.add(`Ok, ${name}, How old are you?`)
else if (!gotname && gotage&&gotgender)
this.agent.add(`What's your name please?`)
else if (!gotname && !gotage&&gotgender)
this.agent.add(`Well dear ${gender}, What is your name and how old are you`)
else if(!gotname && gotage&&!gotgender)
this.agent.add('Let me know what is your name and what is your gender')
else if (!gotname && !gotage&&!gotgender)
this.agent.add(`I want to get to know you before we begin. what is you'r name?`)
}
}
module.exports=PersonalDetails;
Dialogflow 需要用户提供三个实体:姓名、年龄和性别。当用户没有提供所有这些时,代码会执行一些逻辑来查看缺少的内容。
问题是,起初我输入让我们说姓名和年龄,然后它询问用户性别,当用户输入性别时,它已经忘记了姓名和年龄......请帮助
解决方案
在您的对话流实现代码中,您正在对来自意图的每个请求初始化 user.storage 中的参数,而不是仅当您从用户那里获得值时。这段代码是你的问题:
this.conv.user.storage.name=this.agent.parameters.name;
this.conv.user.storage.age=this.agent.parameters.age;
this.conv.user.storage.gender=this.agent.parameters.gender;
您只需设置 user.storage 一次,然后就可以直接在任何地方使用它。
app.intent('GetUserName', (conv, {name}) => {
conv.user.storage.name= name;
conv.ask(`Hi, ${conv.user.storage.name}!.
Please tell me how can I help you? `);
});
app.intent('AboutSC', (conv) => {
conv.ask(`well ${conv.user.storage.name}. What more would you like to know? `);
});
可以user.storage
直接使用参数。但是使用在每个请求上初始化的变量/常量每次都会更改值并且无济于事。
推荐阅读
- c# - 如何在 C# 中使用 StringBuilder 强制 CSV 列将数字视为文本
- c++ - 我正在用 c++ 做设计器 pdf 查看器问题,但在代码中我无法理解一件事
- r - 如何在闪亮的应用程序中居中和加粗文本
- r - UseMethod(“html_table”)中的错误:没有适用于“html_table”的方法应用于“xml_missing”类的对象
- google-apps-script - 如何使用单元格中的 url 循环从网页中收集文本并在另一列中设置值?
- python-3.x - Python CSV 文件是更新部分文件的好方法吗?
- django - 如何更新 django 表中的日期?
- python - 如何将一个 Python 库集成到另一个库中?
- c - 如何在 Xcode 中为语言 C 输入字符“Ñ”?
- python - 如何通过删除pyspark中的列中的字符来自动批量重命名