Use CognigyScript and work with Code Nodes to enhance your Virtual Agent
CognigyScript is a superset of JavaScript which gives you access to the Input and Context objects within text and JSON. CognigyScript can also be used in Code Nodes to execute powerful scripts that make your Virtual Agent smart.
#1 Check if the API call was successful and display results
After the HTTP Request from the previous step, the Flow should check if the API returned one or more results. Add an If Node after the HTTP Request. You can traverse through the Context object to identify the right attribute to check for. For this example, configure the following condition:
context.result_character.result.count > 0
Now add a Say Node in the Else branch as a fallback for the number of results is zero. Use this text:
Never heard that before, sorry.
Add another Say Node in the Then branch and configure it to render the result of the API call. Use the following expression:
Ah, I know {{context.result_character.result.results[0].name}}!
As the search can deliver more than one hit, [0] ensures that only the first element from the array is displayed.
#2 Make an HTTP request based on existing data
Let’s make another HTTP request to fetch the homeworld of a character from the Star Wars API. The URL for this request is already included in the Context object from the previous API call, so you can directly access it. Add another HTTP Request Node under the Say Node in the Then branch and use this expression in the URL field:
{{context.result_character.result.results[0].homeworld}}
As Context Key, use the following term to store the data in the Context object:
result_homeworld
#3 Use a Code Node to process data
As a next step, let’s enable the Virtual Agent to use the right pronoun "he" or "she" when referring to a character. An easy way to achieve this is processing a character’s gender from the Context object. Add a Code Node after the last HTTP Request and enter the following TypeScript code:
const gender = context.result_character.result.results[0].gender;
switch (gender) {
case "male":
context.pronoun = "He";
break;
case "female":
context.pronoun = "She";
break;
default:
context.pronoun = "He/She";
}
This code sets a variable "pronoun" in the Context object that holds the grammatically correct pronoun as a string.
#4 Render the results
You can now add a Say Node after the Code Node. Here you should render the correct pronoun and then display the homeworld of a character:
{{context.pronoun}} is from {{context.result_homeworld.result.name}}.
In this tutorial, let's instead use the API to generate the output. Any kind of information or action in the UI can be queried or triggered through the API - also from external systems. Add a second Code Node and enter the following code:
actions.output(`${context.pronoun} is from ${context.result_homeworld.result.name}.`);
The actions class serves as an entry point into the API.
#5 Try it out!
Try out the experience in the Interaction Panel. Use inputs like Luke, Laia or Gandalf and compare the results.
#DONE! Now let's take you to the finish line and capture your Virtual Agent in a Snapshot.
Comments
3 comments