Skip to content

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.

Screenshot

Steps to Set Up the Branches

  1. Configure the Javascript Node:
    In the Javascript node, use an Avatar.askMe() function to ask what action should be taken.

    Note:
    You could also use a Module node to achieve the same effect.

  2. Set Up Three Payload Nodes:
    Create three Payload 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 calls Avatar.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
async function askmeWant(payload, state) {

  return new Promise((resolve, reject) => {
    Avatar.askme("What do you want? | Can I do something for you?", state.client,
      {
        "*": "generic",
        "turn on": "turnOn",
        "turn off": "turnOff",
        "finished": "end"
      },
      15,
      (answer, end) => {

        // Determines the end of askme without reactivating the client's listener
        end(state.client);

        switch (answer) {
          case "turnOn":
            resolve({ payload: "turnOn", state });
            break;
          case "turnOff":
            resolve({ payload: "turnOff", state });
            break;
          case "end":
            Avatar.speak("finished", state.client, () => {
              resolve({ payload: "end", state });
            });
            break;
          default:
            Avatar.speak("try again, I didn't understand", state.client, () => {
               askmeWant(payload, state).then(resolve);
            }, false);
        }
      }
    );
  });
}

Explanation

  1. Asynchronous Handling with askmeWant:
    The call to askme is encapsulated within a Promise to manage asynchronicity and handle the default case. This is done by recursively calling the askmeWant function within the callback of Avatar.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.

  2. 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.

Screenshot

Payload "turnOff"

This payload receives the value returned by the "turnOff" case of the askme function.

Screenshot

Payload "end"

This payload receives the value returned by the "end" case of the askme function.

Screenshot