botsibm-watsonwatson-assistant

How to write a if condition to set a variable value based on another variable value in Watson assistant


I am working on Watson assistant. I need to set the value of a variable "service_option" based on the value of the variable "service_line". I need to write the code within Watson assistant expression. I heard that Watson assistant Uses Spring Boot Expression Language with some modification but I still couldn't figure out how to write the if condition and then set the variable value. The closest I came was like this: $service_line == "Cloud" ? service_option = {"AWS","Azure","Google"} but the above code is not working.there was no error but the variable "service_option" is not getting assigned and when I assigned it for dynamic option, no options are being displayed.

service_line can has any value of "Cloud", "Machine Learning" and "Full stack" and based on the value of the service_line I need to set the value of service_option which should be an array so that I can pass the service_line option as a dynamic variable for the "option" user response type.


Solution

  • It's Spring Expression Language (SpEL).

    As to how to do this.

    Dialog

    The following should work in dialog.

    $response = "<? $service_line == \"Cloud\" ? \"Yes\" : \"No\" ?>"
    

    The <? ?> means to execute the code block replace that block with the response.

    Dialog uses what is called Autoboxing. So although a string returns, later logic can read it as a different object type.

    Actions

    There are two approaches.

    You can add the following as an expression response to assigning to a variable.

    ${service_line} == "Cloud" ? "Yes" : "No"
    

    In actions the <? ?> doesn't work anymore and should not be used. Instead you just wrap the piece with ( ) so that it executes first. Like so.

    ${response} = (${service_line} == "Cloud" ? "Yes" : "No")
    

    Here is an example image of the UI (You only need one of the lines).

    UI view of what is described in answer.


    To cover the second part of your question. (Which I will explain in Actions but you should be able to convert over)

    To check an array you would do something like this:

    ${response} = (["AWS","Azure","Google"].contains(${service_line}) ? "Yes" : "No")