This documentation supports the 21.3 version of BMC Helix Virtual Agent, which is available only to BMC Helix customers (SaaS).To view an earlier version, select the version from the Product version menu.

Exercise 2 - Consume the Weather service Web API In the chatbot


Before we begin any implementation for consuming a Rest API Output in the Chatbot we need to first decide on the input from the user, which will be passed to the Innovation Studio process and the expected response. Some of these inputs to the process can be implicit and are passed using the User Context data from Foundation.

  • Input from the user
    • City
      • User can provide the city name, or it can be picked from the user's Foundation Data.
    • Country - to determine Unit of measurement for temperature and windspeed.
      • Weather API does not change the unit of measurement, Imperial/Metric, based on the City. It has an optional query parameter which can be set to either imperial or metric.
      • Country can also be determined by User Context data.
      • This implementation sends Country as an input and the process determines the units based on the Country.
  • Output
    • Temperature
    • Windspeed
    • Cloud % - open weather returns image that is used within the chatbot UI

The macro unmigrated-wiki-markup from Confluence is no longer available.


Invoking the REST API from the Process:

  1. Creating Document using the Response In the Weather API, there is no request Document needed as it's a GET call. In the Chatbot Starter Kit Library, we created a Document, 'Open Weather Current Weather', using the JSON response from previous step. Notice the additional "message" property shown in the screenshot blow, as it is used for error handling. In case of error we get a response as shown below: { "code" = "404", "message" = "city not found" }

worddav7dc38d3c6340fd13c52e16b61cd92da2.png

  1. Creating Web API From the Chatbot Starter Kit workspace in Innovation Studio, go to the Web APIs tab and select Open Weather API. Look at how the query parameters in the URL are defined in the Get Current Weather web request, as shown below. Note that the Response is set to the Document defined from the previous step. Although the Query Parameter 'appId' is defined here, it is not used by the process but instead used by the Web API Connection configuration Open Weather Connection.

worddav582220a3250ed2dda0e906983f07daad.png

  1. Creating Process In the Chatbot Starter Kit we have created a process named Get Weather with the necessary input, output and local variables defined. Based on the country, the unit of measurement, to be sent to web – request, is determined. If the call is successful, the variables from the Response Document is set into the output variables else the status is marked as "Error" . This is done by checking the HTTP code returned by "Web Request JSON Response>Output>cod". This process is also guarded by Error Boundry activity for any unexpected error like conectivities issue. worddav219aef7309e12e2bed3aa940eddbfe68.pngThe following is a list of the variables used in the process:In the Chatbot Starter Kit we have created a process named The following is a list of the variables used in the process:
  • Input Variable – These will be passed from the chatbot
    • Country
    • City – passed as input to the Web Request


  • Local variables – used to format input / output
    • unit – Passed as input to the Web Request
      • For US, it is set to Imperial
    • windSpeedUnit
      • default value is m/s
    • temperatureUnit
      • default value is °C

"Set Imperial Unit" activity sets the windSpeedUnit to m/h and tempratureUnit to °F

  • Output variables
    • status – default value is 'Success'
      • If there is error in the Web API "Set Error Message And Code", the activity sets it to "Error"
    • windspeed
      • Mapped to Web Request JSON Response>Output>wind>speed
    • imageUrl
      • Set by the Compute Image URL and Set Units activity
    • locationName – usually its same as City
      • Mapped to the Web Request JSON Response >Output>wind>name
    • errorMessage
      • Mapped to the Web Request JSON Response >Output>message in "Set Error Message And Code"

The following is the input and output mappings used for the Web Request – JSON Response activity from the process.

worddav86ca902e6c8b4eab3a50359844454c1d.png

worddav98cace71b1d276a0bf633a845fec072c.png

The following is an example of how to map the process output variables for windspeed.
 Notice the Web Request – JSON Response output in the screenshot below is same as the "Open Weather Current Weather'" Document we created.
worddav70b724704bc96890458b8b07d5bf5113.png
The following is the input and output mappings used for the Compute Image URL and Set Units activity from the process.
 The Units are just appended temperature and speed respectively by the Compute Activity.

worddav81695fd7ebd86a62a6684efd66818750.png

worddav38ff47f3d89b66de23ee710b6b49f182.png

Before using the process in the chatbot, it is best to test it from the Innovation Studio Manage Process UI, check if it works and perform some testing around it. For example, verify the Process output variables returned is the expected output as shown below. Once testing is done, we are good to invoke it from chatbot.
worddav87ee4987d760152cfe00a55012eae629.png

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*