ibm-cloudwatson-conversation

IBM Watson Assistant: Regular expressions with context variables


I am gathering some context variables with slots, and they work just fine. enter image description here

So I decided to do in another node of the conversation, check if one of these context variables is a specific number:

I was thinking on enabling multi-responses and check if, for example $dni:1 (it is an integer, pattern of 1 integer only), or if it is 2 or 3:

enter image description here

But this is not working. I was trying to solve it for some days with different approaches but I really cannot find a way through it.

My guess is that a context variable has a value, and you can print it to use it like responding with the user's name and stuff like that (which indeed is useful!), but comparing values is not possible.

Any insights on this I can receive?


Solution

  • Watson Assistant uses a short-hand syntax but also supports the more complex expressions. What you could do is to edit the condition in the JSON editor. There, for the condition, use a function like matches() on the value of the context variable.

    Note that it is not recommended to check for context variables in the slot conditions. You can use multi-responses. An alternative way is to put the check into the response itself. There, you can use predicates to generate the answer.

    <? context.dni==1 ? 'Very well' : 'Your number is not 1' ?>
    

    You can nest the evaluation to have three different answers. Another way is to build an array of responses and use dni as key.

    Instead of matching to specific integers, you could consider using the Numbers system entity. Watson Assistant supports several languages. As a benefit, users could answer "the first one", "the 2nd option", etc., and the bot still would understand and your logic could still route to the correct answer.