ibm-cloudibm-watsonwatson-assistant

IBM Watson Assistant: How to get random responses in the new Action skills


In the Watson Dialog there is an option to generate multiple responses randomly. Allowing it to feel less robotic.

Picture of the UI showing 3 random text messages

In the new Actions skills they do not have this option. Is there a way to reproduce it?


Solution

  • Updates:

    1. SpEL is no longer supported in responses. So the sample code below will only work if assigned to a variable, then that variable is used in the response.

    2. Actions now has the ability to create multiple responses.


    If you have simple one line responses then the following code snippet will work in a response.

    <? {
    0: "Message 1",
    1: "Message 2",
    2: "Message 3"
    }[new java.util.Random().nextInt(3)]
    ?>
    

    The 3 in nextInt(3) must be the total number of random responses.

    If you have more detailed responses to give then you can assign a context variable a value of java.util.Random().nextInt(3) (again changing 3 to total responses). Then use that as a conditional in the steps.

    New Actions steps showing messages