2

I need to show an unknown number of buttons in a Watson Assistant dialogue node. The data for the buttons comes from a IBM Cloud Function.

If I manually set up a response type "option" answer in my node the JSON-object looks like this:

{
  "output": {
    "generic": [
      {
        "title": "Välj mötestyp",
        "options": [
          {
            "label": "Rådgivning familjerätt 30 min",
            "value": {
              "input": {
                "text": "447472"
              }
            }
          },
          {
            "label": "Rådgivning familjerätt 60 min",
            "value": {
              "input": {
                "text": "448032"
              }
            }
          }
        ],
        "description": "Välj typ av möte du vill boka",
        "response_type": "option",
        "preference": "dropdown"
      }
    ]
  }
}

My cloud function can create this JSON with x no of options. But how can I use this data in Assistant?

The easiest would be to let the cloud function generate the complete JSON and then just output the returned JSON like this:

{
  $context.output"
}

..but that's not allowed.

Generated output-object from my function:

[{"serviceId":447472,"serviceName":"Rådgivning Familjerätt 30 min"},{"serviceId":448032,"serviceName":"Rådgivning Familjerätt 60 min"}]

Any advice on how to do this?

data_henrik
  • 16,724
  • 2
  • 28
  • 49
GoveeBee
  • 51
  • 3

1 Answers1

0

I don't see a simple way of generating the entire output and options. What you could do is this:

  1. Generate the option labels and values
  2. Pass them into a generic output node that has predefined structures for 1, 2, 3, etc. options. Check based on the array size of your context variable which predefined response structure to fill.

I tested the following:

  {
  "context": {"my": [ {
            "label": "First option",
            "value": "one" 
          },
          {
            "label": "Second",
            "value": "two" }]},


  "output": {
    "generic": [
      {
        "title": "This is a test",
        "options": [{"label": "<? $my[0].label ?>", 
                     "value": {
              "input": {
                "text": "my[0].value"
              }
            }
},{"label": "<? $my[1].label ?>",             "value": {
              "input": {
                "text": "<? $my[1].value ?>"
              }
            }
}],
        "response_type": "option"
      }
    ]
  }
}

It defined a context variable with the options, analogous to the options structure. In the output access the labels and values, later modifying the to proove that they are used and can be modified.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • Hi! That I've done already, and it worked just fine. But what if you don't know the numer of inputs? I can get the size of the array, but how to use it to make x no of buttons? – GoveeBee May 15 '19 at 13:28
  • You would have one response for 2 buttons, one for 3 buttons and so forth (assuming that you keep the options reasonable). For each response you would need to check (if array size == 2 / array size == 3 / ...) – data_henrik May 15 '19 at 13:41
  • Hmm.. So a multiple response plan to try. Interesting. Works for a few different sizes. Definitely a work around, but I shure would like some kind of loop. I better try this. :) – GoveeBee May 15 '19 at 13:44