Search code examples
ibm-cloudibm-watsonchatbotwatson-conversation

Watson Conversation: Check and act on existence of context variable?


I am using IBM Watson Conversation service and slots within a dialog node. In the response I want to return the value of a context variable that is gathered with a slot. However, I cannot be sure that the variable exists because the user could have cancelled the input process and because the variable is optional. How can I check the existence and act depending on that check?

Using this

<? $myVariable ?>

gives an error when the variable is not present.


Solution

  • The Conversation service uses Spring Expression Language (SpEL) to process variables and conditions in its responses. There is a special check that can be applied. I have taken the example from this useful collection of use cases:

    "<? context.myVariable? 'Great. I have the following: '+context.myVariable+'.' : 
    'No information present' ?>"
    

    You can access context variables via context followed by the variable name. The question mark (?) checks for existence. The first response is taken in the case the variable is present, else the second response. The colon (:) separates the two answer options.

    In the above example is the answer is either "Great. I have the following: VALUE" or "No information present".