在dialogflow中使用对象列表设置上下文作为参数 [英] setting context with list of objects as prameters in dialogflow

查看:82
本文介绍了在dialogflow中使用对象列表设置上下文作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of values each having another KEY value corresponding to it, when i present this list to user, user has to select a value and agent has to call an external api with selected value's KEY. how can i achieve this in dialogflow?

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

I tried to send the entire key value pair in the context and access it in the next intent but for some reason when i set a list(array) to context parameters dialogflow simply ignoring the fulfillment response.

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

What is happening here and is there any good way to achieve this? I am trying to develop a food ordering chatbot where the category of items in menu is presented and list items in that menu will fetched when user selects a category, this menu is not static thats why i am using api calls to get the dynamic menu.

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


}


推荐答案

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

The primary issue is that the context parameters must be an object, it cannot be an array.

因此,当您保存它时,可以做类似的事情

So when you save it, you can do something like

parameters: {
  "cat_parameters": catarray
}

当您处理它并得到答复时,您可以得到数组返回

and when you deal with it when you get the reply, you can get the array back with

let catarray = categories.parameters.cat_parameters;

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

(There are some other syntax and scoping issues with your code, but this seems like it is the data availability issue you're having.)

这篇关于在dialogflow中使用对象列表设置上下文作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆