setting context with list of objects as prameters

2019-08-07 01:58发布

问题:

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?

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.

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


}

回答1:

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.)