1
votes

I create one entities with a few fruits (apple, banana, orange, avocado)

When my user say any intent that I need to check if have one @Fruits work fine, but if my user say 2 or more fruits I need to save all in one array. how can I does this using slots? because in my test he save only the last (if I print $myFruits)

tks

2

2 Answers

0
votes

When the user types two values or more, and this values was inside one entity, the values will be save inside array, and you can access the entity. For example...

You can see in my example, if I types two flavor's, will appear in my console the two values in one array...

Dialog:

enter image description here

Console:

enter image description here

So, if you want all values from the entity @fruits. you can use this method for saves inside one context variable (E.g: $fruits):

 <? entities['fruits'][0].value + entities['fruits'][1].value ?> //if types two fruits

And for this to be shown in your dialog, you can use this method:

{
  "output": {
    "text": "This is the array: <? $fruits.join(', ') ?>"
  }
}

The return will be:

This is the array: calabresa, marguerita,

If you want to access all values from your entity with code, you need to access the return from the calling message (for access entities, intents, context variables, etc), and use the following code:

var arrayEntitie = response.entities 

    for (var i=0; i < arrayEntitie.length; i++) {
        if (arrayEntitie[i].name === 'calabreza') { //make your condition
            //do something
        }
    }
  • Official documentation for accessing methods here.

  • You can see this Github repository by IBM Developer using context variables here.

0
votes

Simple way to do is by using @EntityName.values . It will store all the values of given entity in context in form of array.