I want to extend my Watson Assistant and have it fetch information from an external source (via an HTTP REST API service call) so that I could have richer interactions in my dialogs. For example, bring the weather forecast to my the chatbot conversation.
The typical way of achieving such integration is to create an orchestration service. This orchestration service is the ‘glue logic’ between Watson Assistant and an external service. However, I wanted to experiment with the approach where Watson Assistant would call an external service directly. The reason for that is to eliminate that orchestration layer and call services directly from a Watson Assistant.
I found the documentation article called “Making programmatic calls from a dialog node” (link) . The doc implies that external calls from Watson Assistant node are limited to calls to IBM Cloud Functions. You cannot just provide the URL for your external service of choice, e.g. weather forecast provider. In this article, I assume that limitation and implement a simple proxy in a cloud function. That is I have a cloud function to forward the Watson Assistant call to an external service. This diagram explain what I want to do:

Since that initial article (link) provides a good overall background, I’ll just provide the specific details of my implementation.
STEP 1: Create the proxy IBM Cloud Function
var rp = require('request-promise'); function main(params) { let { message, service } = params var options = { method: 'POST', uri: service, body: { info: 'MyProxy - Cloud Function', input: params }, json: true }; return new Promise(function(resolve, reject) { rp(options) .then(function (parsedBody) { resolve({ message: parsedBody, parameters: params }); }) .catch(function (err) { resolve({ message: 'failed!!', error: err.toString() }); }); }); }
Note that the ‘params’ has an element called ‘service’. This is so that you can specify the service name in Watson Assistant. If you prefer, you can override that and override the options.uri
So that you can test the Cloud Function in the UI, it’s useful to define some default parameter values. Note that context, is just an example to show that you can pass any variable from Watson Assistant into Cloud Function.
Note: WebAction can be turned off, as it appears Assistant does not use that interface.
Code for IBM Watson Assistant dialog
The ‘actions’ property is where the custom code is. As you can see in ‘output.text.values’, I’m using the response returned by the external server in the user response.
{ "output": { "text": { "values": [ "The API server returned: $my_result.message.data" ] } },}, "actions": [ { "name": "/username@example.com_environment/MyProxy", "type": "server", "parameters": { "message": "<?input.text?>", "context": "$timezone", "service": "http://example.com/api" }, "credentials": "$private.my_credentials", "result_variable": "$my_result" } ] }
Ping me if you have questions or found this article useful!
References:
[1] https://console.bluemix.net/docs/services/conversation/dialog-actions.html#dialog-actions
You must be logged in to post a comment.