ibm-cloudwatsonwatson-conversation

Watson Assistant best way to handle logic depending on entity values


I'm new to the Watson framework and I'm trying to make a bot that helps with cooking, the way I'm currently doing a dialog is saving every instance of ingredient the user types in an array like so:

If user writes "What can I make with salt and oil"

Then the array willl be: Ingrediente:["salt","oil"]

What I want to do then is add its corresponding dish depending if the Ingrediente array contains a certain ingredient, for example if Ingrediente has both "salt" and "oil", the array ListaPlatillos will be appended the values "Steak" and "Salad" corresponding to the elements in the Ingrediente array respectively, I'm trying to do it inside a slot like so:

enter image description here

The conditions are written like so:

($Ingrediente.contains('Sal') || $Ingrediente.contains('Sal de grano')) && !$ListaPlatillos.contains('Ensalada de ejote asado con menta y queso feta ')

And the LisaPlatillos is appended like so:

    "context": {
    "ListaPlatillos": "<? context.ListaPlatillos.append( 'Ensalada de ejote asado con menta y queso feta ' ) ?>"
  }

The problem is that only the first condition is checked and thus only the first dish is appended, what's the correct/best way to update ListaPlatillos depending on the values of Ingrediente?


Solution

  • The evaluation logic of dialog works in a way that when the response matches - the condition of the response in a slot is evaluated to true, then this response is processed along with its context and no other responses will be processed afterwards. So the updates would need to be made in the context of just one response node - which might not be ideal in this case. You can use the output field for this purpose as the SpEL expressions get evaluated there too and the result of them is not stored in the context which you don't want in this case (you only want to update the value of one field on the context). The code that would do the updates in this case would look like:

    output : {
        "update1" : "<?($Ingrediente.contains('Sal') || $Ingrediente.contains('Sal de grano')) && !$ListaPlatillos.contains('Ensalada de ejote asado con menta y queso feta ') ? context.ListaPlatillos.append( 'Ensalada de ejote asado con menta y queso feta ' ) : '' ?>"
        "update2" : "<?...?>"
    }
    

    In general the syntax is condition ? something : something:

    output : {
        "update" : "<? condition ? what_to_do_when_true : what_to_do_when_false?>"
    }
    

    Now when implementing this kind of a more complex logic in dialog, you might want to check Cloud Functions - there is a way how to call custom functions from Watson Assistant that can process values submitted by user, compute something and return this back to the WA where it can be used to output some text to the user. To find out more about this, visit WA Doc - how to make programatic calls from WA or google how to make programatic calls from watson assistant.