首页 > 解决方案 > 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 需要用户提供三个实体:姓名、年龄和性别。当用户没有提供所有这些时,代码会执行一些逻辑来查看缺少的内容。

问题是,起初我输入让我们说姓名和年龄,然后它询问用户性别,当用户输入性别时,它已经忘记了姓名和年龄......请帮助

标签: dialogflow-esactions-on-google

解决方案


在您的对话流实现代码中,您正在对来自意图的每个请求初始化 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直接使用参数。但是使用在每个请求上初始化的变量/常量每次都会更改值并且无济于事。


推荐阅读