# Code

{% hint style="info" %}
Code cells are compatible with and intrinsically related to the [Dynamic Content](#d-dynamic-answer) feature. Code cells perform operations on them and may read, write, and update variables.
{% endhint %}

**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.&#x20;

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](https://docs.conversational-ai.syntphony.com/user-guide/build-dialogs/dialog-cells/services), 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

{% hint style="info" %}
**Tip: Create** [**Variable Answers**](https://docs.conversational-ai.syntphony.com/user-guide/build-dialogs/dialog-cells/rule/variable-answers-using-code-and-rule-cells) **using** [**Code** ](https://docs.conversational-ai.syntphony.com/user-guide/build-dialogs/dialog-cells/code)**and** [**Rule** ](https://docs.conversational-ai.syntphony.com/user-guide/build-dialogs/dialog-cells/rule)**cells** 😉
{% endhint %}

### Using a code cell

THe code cell has a body to input your code snippet in JavaScript:

![Code cell field](https://content.gitbook.com/content/n6zS4HeuuVpRHZEvDiFU/blobs/9igTh8QrgqbksI6EnqG4/2%20code%20cell%20field.png)

You can use JavaScript’s variables (if you wanna know more about this language, [access this page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)) and program any code in it, as long as it’s executable within 100 millisecond&#x73;**.**

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

<table><thead><tr><th width="153.20001220703125">Value</th><th>Function</th></tr></thead><tbody><tr><td>input</td><td>information that users write to the virtual agent and fed into Syntphony CAI</td></tr><tr><td>opencontext</td><td>information that is open to channels to alter its values</td></tr><tr><td>visiblecontext</td><td>information that is open to channels, but its values cannot be changed</td></tr><tr><td>hiddencontext</td><td>information that is closed to channels, being visible only to Syntphony CAI and the services called</td></tr><tr><td>intents</td><td>information registered in Syntphony CAI that means what the user wants to get out of the interaction. intents[0].name returns the intent name</td></tr><tr><td>entities</td><td>information registered in Syntphony CAI that means knowledge repositories used by the virtual agent to provide personalized and accurate responses. entities['entity_name'] returns the desired entity.</td></tr><tr><td>channelType</td><td>channel's type (if it's web, Facebook, Alexa, etc..)</td></tr><tr><td>channelName</td><td>Channel's name registered in Syntphony CAI </td></tr><tr><td>botName</td><td></td></tr></tbody></table>

#### Intent <a href="#intent" id="intent"></a>

| **Name**       | **Type** | **Required** | **Description**                                                                     |
| -------------- | -------- | ------------ | ----------------------------------------------------------------------------------- |
| **name**       | String   | Yes          | Name of the intent, same as the NLP                                                 |
| **confidence** | Double   | Yes          | Confidence score returned by the NLP, this will be a percentage number from 0 to 1. |

{% hint style="warning" %}
Entities and intents are read-only attributes. That means you cannot edit their content.
{% endhint %}

#### Entity <a href="#entity" id="entity"></a>

| **Name** | **Type** | **Required** | **Description**                                      |
| -------- | -------- | ------------ | ---------------------------------------------------- |
| name     | String   | Yes          | Name of the entity, same as the NLP.                 |
| value    | String   | Yes          | The value of the entity returned by the NLP.         |
| position | Position | No           | Position of the string within the user input (text). |
