Search code examples
watson-conversation

Access the value of the array from the text in Watson


I have list of values defined in the values of array inside the text element. Now, when the value of the aray is "Counting finished" I would like to route the flow to new node and pass some textenter image description here to the user. How to achive the condition?


Solution

  • As that is output text the conversation tree can't see it unless you put it into the input or a context variable, and send it back into the tree.

    The easier option is to create a counter. There is more details and an example conversation file here:

    https://sodoherty.com/2016/09/22/using-counters-in-conversation/

    Summary:

    You create a context variable like countdown for example, and set it with a value to countdown from.

    Then in your input text you can add the following code to initiate a decrement.

    <? context.countdown-- ?>
    

    This will be outputted though, so you can use a continue from to jump to another node which has append set to false. This will erase the counter from being displayed.

    Optionally you can set the context variable directly like as follows.

    {
      "output": {
        "text": "Decrementing countdown counter"
      },
      "context": {
        "countdown": "<? context.countdown-- ?>"
      }
    }
    

    This had issues in earlier versions of conversation though. I believe it should work now without issue.