【问题标题】:setting context with list of objects as prameters in dialogflow在对话框流中使用对象列表作为参数设置上下文
【发布时间】:2019-01-12 20:33:21
【问题描述】:

我有一个值列表,每个值都有另一个与之对应的 KEY 值,当我将此列表呈现给用户时,用户必须选择一个值,并且代理必须使用所选值的 KEY 调用外部 api。如何在对话流中实现这一点?

我尝试在上下文中发送整个键值对并在下一个意图中访问它,但由于某种原因,当我将列表(数组)设置为上下文参数时,对话框流只是忽略了实现响应。

这里发生了什么,有什么好的方法可以实现吗?我正在尝试开发一个食品订购聊天机器人,其中显示菜单中的项目类别,并且当用户选择一个类别时将获取该菜单中的列表项目,这个菜单不是静态的,这就是我使用 api 调用来获取动态菜单的原因。

function newOrder(agent)
{

  var categories = []
  var cat_parameters = {}
  var catarray = []
  const conv = agent.conv();
  //conv.ask('sure, select a category to order');
  agent.add('select a category to order');
  return  getAllCategories().then((result)=>{

    for(let i=0; i< result.restuarantMenuList.length; i++)
    {
      try{
        var name = result.restuarantMenuList[i].Name;
        var catid = result.restuarantMenuList[i].Id;
        categories.push(name)
        //categories.name = catid
        cat_parameters['id'] = catid;
        cat_parameters['name'] = name
        catarray.push(cat_parameters)
      }catch(ex)
      {
        agent.add('trouble getting the list please try again later')
      }
    }
    agent.context.set({
      name: 'categorynames',
      lifespan: 5,
      parameters: catarray, // if i omit this line, the reponse is the fultillment response with categories names, if i keep this line the reponse is fetching from default static console one.
    })
    return agent.add('\n'+categories.toString())

  })

  function selectedCategory(agent)
  {
    //agent.add('category items should be fetched and displayed here');
    var cat = agent.parameters.category
    const categories = agent.context.get('categorynames')

    const cat_ob = categories.parameters.cat_parameters

    // use the key in the catarray with the parameter cat to call the external API
    agent.add('you have selected '+ cat );
  }


}

【问题讨论】:

    标签: node.js chatbot dialogflow-es


    【解决方案1】:

    主要问题是上下文parameters 必须是对象,不能是数组。

    所以当你保存它时,你可以做类似的事情

    parameters: {
      "cat_parameters": catarray
    }
    

    当你得到回复时处理它,你可以得到数组回来

    let catarray = categories.parameters.cat_parameters;
    

    (您的代码还有一些其他语法和范围问题,但这似乎是您遇到的数据可用性问题。)

    【讨论】:

    • 谢谢你,我完全按照你说的解决了这个问题,这是一个愚蠢的错误,我很尴尬。我不只是 dialogflow 的新手,我也是 javascript 和 node js 的新手(我有 Java 背景)。我慢慢接了,谢谢你没有忽略一个新手提出的这个愚蠢而愚蠢的问题
    • 很高兴您解决了这个问题!如果答案有帮助,接受和/或支持它总是值得赞赏的。不要为这样的错误感到尴尬——这是一个很容易错过的错误,有时与其他人讨论问题会有所帮助。希望通过在这里分享您的问题,您可以帮助将来犯同样错误的其他人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多