article by Frank Nimphius, October 2018
In Oracle Digital Assistant (formerly known as Oracle Intelligent Bots), user input can be validated in two forms
- Based on variables that are based on entities
- manually for variables that are not entity bound
In this article I explain how to use Apache FreeMarker template expressions to validate user input. As an example I use a length validation use-case for "quote of the day" type of user input. The image below shows the runtime view of a bot in the embedded tester for a successful quote, 'Carpe Diem'
The next image shows the same runtime in case of a failed validation. For this I used one of my all time favorite quotes by Wayne Gretzky: "I skate to where the puck is going to be, not where it has been."
Again, length validation is only one type of validation Apache FreeMarker is capable of doing in the context of Oracle Digital Assistant. Below is the BotML code of the dialog flow that is used by the bot shown in the images above
context:
variables:
quoteOfDay: "string"
states:
#here is where the user input is provided and saved into a string variable
getQuoteInput:
component: "System.Text"
properties:
prompt: "Please give me your favorite quote. But make sure it does not exceed 50 characters"
variable: "quoteOfDay"
transitions:
next: "validateInputString"
#the variable is evaluated for the length of the provided string. If it exceeds
#50 characters then the value "lengthExceeded" is returned. "lengthOk" is
#returned otherwise.The returned value is then mapped to a state in the dialog
#flow
validateInputString:
component: "System.Switch"
properties:
source: "${(quoteOfDay.value?length > 50)?then('lengthExceeded','lengthOk')}"
values:
– "lengthExceeded"
– "lengthOk"
transitions:
actions:
lengthExceeded: "correctQuote"
lengthOk: "printQuote"
NONE: "printQuote"
#to ensure the user is given a chance to correct the input you i) let her
#know, and ii) reset the variable before redirecting the conversation to
#the getQuoteInput state
correctQuote:
component: "System.Output"
properties:
text: "With ${quoteOfDay.value?length} characters, your quote exceeds the 50 character length mark."
keepTurn: true
transitions:
next: "resetVariables"
#reset the variable state
resetVariables:
component: "System.ResetVariables"
properties:
variableList: "quoteOfDay"
transitions:
next: "getQuoteInput"
#this is the state the dialog flow is navigated to in the case of a valid length
#of the quote
printQuote:
component: "System.Output"
properties:
text: "The quote you shared is: ${quoteOfDay.value} [${quoteOfDay.value?length} characters]"
keepTurn: false
transitions:
return: "done"
You can easily see, how the code could be changed to e.g. evaluate the user input for string only or numeric only input, or to detect whether a specific keyword is contained. Just have a look at the Apache FreeMarker built-in and experiment with it: https://freemarker.apache.org/docs/ref_builtins.html
Related Articles:
TechExchange – Using Apache Freemarker Expression in Oracle Intelligent Bots
TechExchange Quick-Tip: How to tell whether a specific entity value is contained in a user sentence?
TechExchange Quick-Tip: Accessing Attribute Names and Values of a Data Object