article by Frank Nimphius, January 2019
In Oracle Digital Assistant skill bots you use postback actions of the Common Response component to update one or many context variables.
For example, the card layout shown in the image below (image 1) is rendered with the Common Response component. The information about the people in the card response is read from an array of person objects as it would be returned from a REST service call.
Say that when pressing the "More about Grant" button you to access the person name, the location and the image URL. A way to code this in the Common Response component is shown below
metadata:
responseItems:
– type: "cards"
cardLayout: "horizontal"
cards:
– title: "${personArray.name?upper_case}"
description: "${personArray.location}"
imageUrl: "${personArray.image}"
iteratorVariable: "personArray"
rangeStart:
rangeSize:
actions:
– label: "More about ${personArray.name?keep_before(' ')}"
type: "postback"
keyword: "${personArray.name?replace(' ',',')}"
payload:
variables:
personName: "${personArray.name}"
personLocation: "${personArray.location}"
personImageUrl: "${personArray.image}"
In this example, personName, personLocation and personImageUrl are context variables you define in the skill bot dialog flow. And there is nothing wrong with this solution.
However, what if there was more information about a person like mail address, work phone, mobile phone, twitter handle, linkedIn handle, home address, office address, job title, job id, manager id, and many more.
Hope you agree that creating context variables for each attribute sounds like the second-best solution. For objects that expose many attributes, a better solution to access the selected person detail is to use the array index.
metadata:
responseItems:
– type: "cards"
cardLayout: "horizontal"
cards:
– title: "${personArray.name?upper_case}"
description: "${personArray.location}"
imageUrl: "${personArray.image}"
iteratorVariable: "personArray"
rangeStart:
rangeSize:
actions:
– label: "More about ${personArray.name?keep_before(' ')}"
type: "postback"
keyword: "${personArray.name?replace(' ',',')}"
payload:
variables:
personIndex: "${personArray?index}"
In the code sample above, only a single context variable, personIndex of type int, needs to be created. The "?index" expression is from Apache FreeMarker and assigns a number to each card element (or list in a list of values if you build value-lists with the common response component).
The index associated to the payload of each card matches the index the data object has in the array (context variable of type string that you populate from a custom component or statically in the dialog flow using another Apache FreeMarker expression)
To access the person information later in the dialog flow (image 2)…
You use an expression like this:
${personArray.value[personIndex.value?number].location}
The expression above retrieves the person object from the personArray context variable using the index saved in the personIndex context variable. You need to make sure to explicitly make it a number using the "?number" expression in Apache FreeMarker. With a handle to the person object, it then accesses the location information.
Below is the sample code used to render image 2.
Hope you see the advantage of saving the index of an object compared to saving the selected
person's attributes.
Related Content
TechExchange Quick-Tip: Accessing Attribute Names and Values of a Data Object
TechExchange – Using Apache Freemarker Expression in Oracle Intelligent Bots