我试图通过与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希望用户提供三个实体:姓名、年龄和性别。当用户没有提供全部内容时,代码会执行一些逻辑来查看缺少的内容。
问题是,一开始我输入姓名和年龄,然后它询问用户性别,当用户输入性别时,它已经忘记了姓名和年龄……请帮帮忙
发布于 2019-09-08 23:36:22
在您的对话流实现代码中,您将在上初始化来自意图的每个请求的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参数。但是,使用在每次请求时初始化的变量/常量将在每次都更改值,这是没有帮助的。
https://stackoverflow.com/questions/57842620
复制相似问题