Choosing a Branch
Scenario Objective
Sometimes you may want to select a branch to execute based on a specific value. This value can be obtained through various methods, such as the response from an Avatar.askMe(), the result of a calculation, etc. In this scenario, we will use a Payload
node to determine which branch to execute.
How It Works
The Payload
node is very straightforward because it receives a single value. If the received value matches the one defined in the Payload
, then the associated branch is executed, and all other branches are ignored.
For example, consider a scenario that turns a light on or off. In this case, there are two distinct branches to trigger an Action
node corresponding to the task to be executed, plus a third branch to stop the scenario.
Steps to Set Up the Branches
-
Configure the
Javascript
Node:
In theJavascript
node, use an Avatar.askMe() function to ask what action should be taken.Note:
You could also use aModule
node to achieve the same effect. -
Set Up Three
Payload
Nodes:
Create threePayload
nodes corresponding to the three possible responses.
The askmeWant Function in the Javascript Node
The askmeWant
function in the Javascript
node is designed to prompt the user for input using Avatar.askme()
. It asks a question (for example, "What do you want?") and maps the user's response to a specific command or payload value, which then determines which branch of the scenario will be executed.
How It Works:
-
Prompting:
The function callsAvatar.askme()
to display a question to the user. -
Mapping Responses:
It defines a set of expected responses (such as "turnOn", "turnOff", "end") and maps these responses to specific actions. For instance, if the user responds with "turnOn", the function resolves with a payload that triggers the branch to turn the light on. -
Handling Unrecognized Input:
If the user's input does not match any of the predefined responses, the function prompts the user again, ensuring that a valid response is eventually received. -
Returning the Payload:
Once a valid response is provided, the function returns a payload (or command) that corresponds to the chosen branch, thus guiding the scenario's flow.
This interactive function is essential for scenarios that require dynamic decision-making based on user input, allowing the scenario to adapt its execution path in real-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Explanation
-
Asynchronous Handling with askmeWant:
The call toaskme
is encapsulated within a Promise to manage asynchronicity and handle the default case. This is done by recursively calling theaskmeWant
function within the callback ofAvatar.speak
until a valid response is received. Without this structure, it would be impossible to chain asynchronous calls, resume the control flow when an unrecognized response is encountered, and propagate the function calls to continue the scenario. -
Returning a Payload:
For each case (e.g., "turnOn", "turnOff", "end"), a payload with a specific value is defined and returned. This payload then determines which branch of the scenario will be executed.
The Payload Nodes
Three Payload
nodes are defined to handle the possible responses:
Payload "turnOn"
This payload receives the value returned by the "turnOn" case of the askme
function.
Payload "turnOff"
This payload receives the value returned by the "turnOff" case of the askme
function.
Payload "end"
This payload receives the value returned by the "end" case of the askme
function.