How to Dynamically Add Response Options to Dialog Nodes in Watson Assistant

Jan Vystrcil
IBM Data Science in Practice
4 min readSep 30, 2020

Smart looking virtual assistants should provide relevant information and try to minimize the number of dialog turns. In this article, you will learn how your bot can be providing options, that are valid at a certain point in time.

Watson Assistant allows you to add various types of responses like text, images, or options. Options can be rendered as clickable buttons to your customer.

After a user clicks a button, your assistant will receive the string in the “Value” column in the screenshot below. You can then capture that value with an entity in the next dialog turn

But what happens if you need options for a specific person or point in time? For example, available time slots to make an appointment. During any given conversation, available time slots may vary, and you wouldn’t want to double book a specific time. The best way to accomplish this is to show only options for available days and times. You could let the user choose any date/time, then validate it afterward. Unfortunately, your assistant will not look very smart if the user has to try over and over again to get an acceptable time. This can be achieved with a webhook that computes the options to be displayed. The webhook should receive a prepared payload that will be added to the output of your message and render correctly on your client application.

Photo by Nathan Dumlao on Unsplash

First, you need to prepare a webhook that returns the JSON object payload that will be rendered later by Watson Assistant. For experiments, Node Red or IBM Cloud Functions can be used. On the first level of the JSON object there must be a JSON array (in the example named “arr”) that contains a JSON object in the format of the options response type.

See the example of a JSON object below:

{
"arr": [
{
"title": "What time do you want your appointment?",
"options": [
{
"label": "Monday 1:00 pm",
"value": {
"input": {
"text": "Monday 1:00 pm"
}
}
},
{
"label": "Tuesday 4:00 pm",
"value": {
"input": {
"text": "Tuesday 4:00 pm"
}
}
}
],
"description": "",
"response_type": "option"
}
]
}

Code snippet of simple IBM Web action is below:

function main(params) {
return { arr:[{“title”:”What time do you want your appointment?”,”options”:[{“label”:”Monday 1:00 pm”,”value”:{“input”:{“text”:”Monday 1:00 pm”}}},{“label”:”Tuesday 4:00 pm”,”value”:{“input”:{“text”:”Tuesday 4:00 pm”}}}],”description”:””,”response_type”:”option”}]};
}

In this example, we expect the webhook response payload (after a successful webhook call) will be stored in the $webhook_result_1 context variable.

Now you need to add a dynamically generated list of options (the result of webhook call) to the response of the dialog node. To achieve this, we will use the Multiple Conditioned Responses that are generated when we customize our dialog node to use webhooks.

On the response for $webhook_result_1, open the customize window. In the popup, you need to open the menu and click ‘Open context editor.’ You will set an auxiliary context variable “tmp” with the following expression:

"<? output.generic.addAll($webhook_result_1.arr) ?>"

The name of the variable (i.e. “tmp”) is not important as you won’t need to refer to it later. The key thing here is you need to execute the SpEL expression on the right-hand side. This will add the dynamically generated options to the output message response.

Now you can put this all together to generate a dynamic option for your user. When the node you have configured gets triggered, the output of your assistant will look like this:

If you can see the options as in the screenshot above, you have successfully finished the goal of this article. Now you just need to modify the code of your webhook, to send dynamic options needed in your bot. More information about response types and webhooks in Watson Assistant can be found in the documentation.

--

--