Invoking Business Logic from Watson Conversation Dialogs

With the IBM Watson Conversation service you can easily recognize intents and entities in user phrases and you can define flows of dialogs declaratively. Recently a new feature was added (as beta) that allows Watson Conversation developers to run code at certain stages in the dialogs via the serverless platform OpenWhisk.

In conversational experiences a typical dialog flow looks like this:
1. A user wants to invoke a certain intent by providing an input phrase, for example ‘how is the weather?’
2. The bot gathers missing required input from the user, for example ‘in which city?’
3. After all slots have been filled, some business logic is executed, for example a REST API request to a weather service
4. The response of the API request is wrapped into a message that is sent to the user

In order to implement this scenario, so far developers had to write and host their own applications that use the Watson Conversation API. Check out the Slack bot for an example.

With the new Conversation functionality OpenWhisk functions can be directly invoked by the Conversation service when certain nodes in the dialogs are reached. Let’s take a look at a simple sample.

The OpenWhisk function is a simple hello world function which only echos one of the input parameters. The function is deployed in the IBM Cloud organization of the Watson Conversation developer.

function main(params) {
   return { message: 'Hello World ' + params.name };
}

To allow Watson Conversation to invoke the function, the OpenWhisk credentials of the Watson Conversation developer are needed in the dialog. For testing purposes they can be pasted in context variables in the dialogs. The namespace ‘private’ prevents the credentials to be stored in the logs.

conversation-openwhisk1

In order to trigger the OpenWhisk functions, a special property ‘actions’ is used which contains all necessary information for the function invocation. In the following example a static value ‘Niklas’ as well as the user’s input phrase is passed to the function. The response is written to the context variable ‘my_input_returned’ which can be accessed in the message that is sent to the user.

conversation-openwhisk2

In order to find out more, watch the video and check out the documentation.