使用对象列表设置上下文作为对话流中的参数

setting context with list of objects as prameters in dialogflow

我有一个值列表,每个值都有另一个对应的 KEY 值,当我向用户显示此列表时,用户必须 select 一个值,代理必须调用外部 api使用 selected 值的 KEY。我如何在 dialogflow 中实现这一点?

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

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

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 );
  }


}

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

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

parameters: {
  "cat_parameters": catarray
}

并且当你得到回复的时候处理它,你可以用

取回数组
let catarray = categories.parameters.cat_parameters;

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