Watson Serverless Applications: Text-Bot with OpenWhisk

Elizabeth Kelly
IBM watsonx Assistant
6 min readAug 11, 2017

Need to know if you should bring an umbrella to work today or any day within the next 7 days? The Text-Bot with OpenWhisk is here to help. Using Natural Language Understanding, Conversation, The Weather Data API, along with Cloudant NoSQL Database support, this application gives you the weather forecast within a 7-day period for a given city in the U.S. Written in Node.js and integrated with OpenWhisk, IBM’s open-source, serverless cloud-computing environment, the Text-Bot is entirely serverless and run on-demand, making it highly scalable and economic.

What It Does:

To check out the application yourself, visit https://text-bot-openwhisk.mybluemix.net/.

To use this application, simply enter a U.S. city name. From there, the Text-Bot may ask for what U.S. state you are referring to, as the GIF above demonstrates. From there, you can then enter a day of the week and the Text-Bot will provide you with the forecast for that day.

How It Works:

The Text-Bot with OpenWhisk is hosted as a Static Assets web application and is built with React, HTML/CSS, and Node.js for the OpenWhisk actions.

With respect to the code, all necessary information required for each service, especially Conversation, is stored and passed within a context. The context in the Text-Bot’s case is written in JSON, and includes fields for service credentials, user input text, city geolocations, and weather forecasts. Conversation uses this context to formulate the dialogue between the user and itself, so in order for it to work as expected, the necessary fields must be present in the context. An example of a context is shown below:

Initial context that is sent to the front-end when the application is started.

Here is an example of the context when a city name has been detected and the weather forecasts have been retrieved:

The updated context.

OpenWhisk Artifacts

Actions

To begin, we create what are known as OpenWhisk actions, which are stateless functions or pieces of code that are executed in the IBM Cloud Platform. Each action contains an API call each to one of the Bluemix or Watson services. In other words, each action is in charge of only one single function, as opposed to having all functionality in one action.

So for example, the getWeather.js action is in charge of just that — getting the weather forecasts.

API call to get the weather forecasts

For nlu.js, this action deals only with calling Natural Language Understanding to process the user input and determine if it is a city name.

Call to Natural Language Understanding

The separation of functionality allows for the code to be compartmentalized, making it easier to organize, to track changes, and to edit or debug.

Sequences

Now, in order to connect the actions together so that they perform their functions one after the other, we create an OpenWhisk sequence. A sequence can be thought of as a single piece of code that contains the functionality of each action. The order of the actions in a sequence are determined by business rules or logic, such as the order that services must be executed in.

In many sequences, including the Text-Bot’s, the outputs from one action becomes the input to the next. Therefore, the flow of inputs and outputs within the OpenWhisk sequence must be structured so that each action returns what is needed for the next action to function.

Below is a flow chart representation of the Text-Bot sequence:

Here is a step-by-step explanation of the graph:

  1. Natural Language Understanding analyzes the input text and performs entity identification to determine if the input is a city name.
  2. If the input is a city name, this text as well as a default context (used by Conversation) is passed into the Get Geolocation action. The geolocation of the input text is retrieved by making a call to The Weather Data API. If there is more than one geolocation for this city name, then a list of geolocations is returned as output.
  3. The output is then passed into Conversation. At this point, the output contains the original input text as well as a Conversation context, now containing a geolocation or list of geolocation. If there is more than one geolocation, then there is not enough information for any of the further actions to function. So in this case, the following actions are skipped and the output is returned back to the beginning of the sequence (to Natural Language Understanding) and the Text-Bot then asks the user to provide a state name that the city exists in. If there is only one geolocation in the context, the sequence continues as normal.
  4. Once there exists only one geolocation in the context, the Get Weather action is then called. This action again makes a call to The Weather Data API to retrieve a 7-day weather forecast for that city, starting from the day that it was called. The list of forecasts is then appended to the context and sent to next action.
  5. The final action is another call to Conversation in which the weather forecast for the day is outputted to the user.

This entire flow is rerun when a user enters a day of the week to get the forecast for (after a the initial forecast for a city is given), or if they enter a new city.

APIs

Once the sequence is created, you can then expose your OpenWhisk sequence as an API. To do this, the sequence is simply mapped to a well-defined API endpoint. The API request can then be called within the React front-end as a fetch request, where the result can be processed there for the user to see.

The fetch request to the OpenWhisk API

Deploy To Bluemix:

If you are interested in running your own instance of the application, you can head to the repository and find the Deploy to Bluemix button shown below:

Clicking this button in the repository will start the deployment toolchain, which will create all the necessary Watson and Bluemix services as well as the OpenWhisk artifacts (actions, sequence, and API). Then it will create a Cloud Foundry application that is deployed to Bluemix.

Future Updates and Improvements:

As it stands, the Text-Bot demo only provides weather forecasts for U.S. cities. However, some of these cities are not detected as such by the Natural Language Understanding service. As such, improved city detection would be a necessary improvement to consider in a future iteration of the application.

On the subject of improved city detection, in order to cast a wider net and allow for the application to be used in a broader sense, it would be beneficial to allow for outside-U.S. city detection, which could also lead to the necessity of multi-language support. With such an addition, this would make the Text-Bot accessible to those who live in or travel to countries outside of the United States.

Special thanks to German Attanasio, James Zhang, Paul Castro, and Mamoon Raja for their mentorship.

--

--