Code

Code Cells are compatible with and intrinsically related to the Dynamic Content feature. Code cells perform operations on them and may read, write and update variables.

Code Cell works with information available in Syntphony CAI’s system that doesn’t depend on APIs. It also allows creating variables, that's why it provides immense advantages in a virtual agent flow creation process.

Code cells are very useful in various scenarios, for example:

  • In ecommerce, the Code cell would be responsible for almost the entire process, such as calculating the number of items or calculating the purchase, etc. Only the chosen products availability search and the finalization of the purchase would be in charge of the Service cell.

Below, to insert in a Code cell, there is an example of code to calculate the value of the purchases in a shopping cart:

var total = 0.0;
if (visibleContext.shoppingCart != null && visibleContext.shoppingCart.items != null) {
    for (i in visibleContext.shoppingCart.items)
        total += i.price;
}
  • You can also create a simple variable:

hiddenContext.myvar = 5;
  • Or validate user login after a service call:

hiddenContext.logged = true
  • In a game, you can simplify a lot the creation of quiz bringing together in a single cell all the questions and answers:

hiddenContext.questions = {
    "1": {
        "question":"What is the name of the first chatbot ever?",
        "answer":"ELIZA"
    },
    "2": {
        "question":"When was ELIZA created?",
        "answer":"1966"
    }
};.

Unlike the Service Cell, which connects data from a company via an API, Code Cell performs many activities (such as calculations and validation) without the need for this connection. This gives you the following advantages:

  • Manipulate objects

  • Anticipate executions and actions

  • Perform services without the need for APIs

  • Save time

  • Reduce services costs

Tip: Create Variable Answers using Code and Rule cells 😉

How can you create a Code Cell?

Just insert a code snippet in JavaScript as shown in the image below:

You can use JavaScript’s variables (if you wanna know more about this language, access this page) and program any code in it, as long as it’s executable within 100 milliseconds.

The variables below can be used in Syntphony CAI on the Insert code field:

Intent

Entities and intents are read-only attributes. That means you cannot edit their content.

Entity

Last updated