article by Frank Nimphius, April 2019
In a recent article on Oracle TechExchange, Grant Ronald explained a solution for displaying randomized bot responses. In this quick tip, I am taking Grant's solution to a next level, which is to use resource bundles to store the random strings to display.
The solution so far
In his article, Grant Ronald explained how to create a skill in Oracle Digital Assistant that generates random bot messages when called repeatedly (see screenshot below). The solution outlined was a clever trick in which the strings returned for a given dialog flow state were kept in an array. A random number generator that considered the number of available strings for a state was then used in the prompts to determine the message to display as a bot response.
Using resource bundles
Resource bundles are common in software development and are used to define labels and prompts separate and outside the actual application code. In addition to reuse and the ease with which you can change labels and prompts, Resource Bundles also allow you to create translated versions of the strings that then will be displayed instead of the original string when the locale or other language settings change. The user specific language settings in Oracle Digital Assistant are stored in the profile.locale and profile.languageTag.
So, similar to Grant's solution, the solution in this article uses an array of strings. The strings however represent keys in the resource bundle for a skill. Before, to work with resource bundles, you need to define a content variable of type "resourcebundle". The name of the variable can be freely chosen by you, but most commonly is named "rb". The messages variable then holds an object in which each property holds and array of values for a specific component state. Read this article to learn about what other structured data objects you can create using Apache FreeMarker. The image below shows strings for two different dialog flow state: welcome, confused, goodbye
As shown in the image below, you reference the strings saved in the resource bundle using an expression reference to the resource bundle (rb) variable. To access the array of strings for a specific dialog flow state, you use message.value.<property name>. An expression that uses the current date and time in milliseconds, along with the size of the string array, is used to generate a random number between 0 and the array size -1.
Similar, for the unresolved – "confused" – dialog flow state …
The resource bundle is created in the skill and contains the key names in the array:
How to improve the improved version
If you created a fixed number of strings for all dialog flow states, then you can eliminate the need for a variable object that holds the array of string keys for each dialog flow state.
For example, say that for each resource bundle entry, you create 3 versions: keyname_0, keyname_1, keyname_2. With this, the expression used in a components referenced in the dialog flow state could be changed to
text: " text: ${rb('keyname_${.now?long % 3}')}"
Note: If your key names are not zero-based (e.g. keyname_1, keyname_2, keyname_3) then you can change above expression to
text: " text: ${rb('keyname_${(.now?long % 3)+1}')}"
With this, you eliminate the requirement for maintaining arrays of strings. The image below shows the origin resource bundle example in this article changed so arrays could be eliminated.
Note: The key names used in the example are welcome1, welcome2, welcome3 (as well as goodbye1, goodbye2, goodbye3, confused1, confused2, confused3)
still, the displayed output is:
Using resource bundles with entities
Most common, a prompt in a dialog flow is for users to provide a value for an entity type variable. In Oracle Digital Assistant, prompts for an entity can be saved in the entity. If multiple prompts are provided for an entity, then they are displayed in a random order. So, do you hear a bell ringing? Yes, entities could reference resource bundles in their prompt definitions. This not only gives you random bot responses, but also makes it easy to create a multi-language bot.
Below images show an Airport entity (custom entity) that has 3 prompts defined.
The prompts reference a context variable (rb) of type "resourcebundle" that you need to set in the dialog flow. The resource bundle strings are shown in the image below
In the dialog flow you can now use a System.ResolveEntities component to display the input prompts
Download the sample skill bots:
You can download the sample skills used in this article
Array Based Solution
Non-Array Solution
Entity Solution
Related Content
TechExchange: Building Single Base-Language Chatbots with Oracle Intelligent Bots
TechExchange – Using Apache Freemarker Expression in Oracle Intelligent Bots