# Rule

{% hint style="info" %}
Rule Cells are compatible with and intrinsically related to the [Dynamic Content](#d-dynamic-answer) feature, and may use them to branch paths on your flow.
{% endhint %}

**The Rule cell allows you to manage and customize the flows.** It’s a resource that makes the dialog more assertive and more precise, as the virtual agent will respond to any changeable scenarios.

In a flow, you will often encounter scenarios where the if-then-else conditionals are applicable. For each possibility, you will predict a Rule Cell, always foregrounding the main rule.&#x20;

**Summarizing, Rule cell is fundamental in flows that need any kind of rules.**

Below, there are some examples of use cases, with respective codes to base the creation of your Rule cells.

* If the user informs that is over 18, will be directed by the Rule Cell to a dialog to open his account. Minors will be directed to a dialog informing how to open an account with an adult guardian. In that case, you should write the following code in the Rule Cell:

```
hiddenContext.age < 18
```

* Use different responses according to channels:

```
info.channelName == 'Homepage'
```

* To check if user is logged in:

```
hiddenContext.logged == true
```

* To check if the shopping cart has items:

```
visibleContext.shoppingCart != null && 
visibleContext.shoppingCart.items != null &&
!visibleContext.shoppingCart.items.isEmpty()
```

* To validate confidence level of intent:

```
intents[0].name == 'MY_INTENT' && intents[0].confidence > 0.8
```

* For single-variable validation:

```
hiddenContext.myvar == 5
```

* If the user wants to create an "escape", a way to execute a flow in which no other scenario fits:

```
true
```

* To disambiguate, as an alternative to the use of entities *(although the recommended and simplest way to build flows is, actually, using entities*). For example: if you want to differentiate a user who wants to check his bank account balance from another user who wants to check his credit card balance, you can predict two *(2)* Rules Cells:

```
//First Rule Cell code:
if(!intents.isEmpty())     
    intents[0].name =="BANK_BALANCE" 
else   
    false;

//Second Rule Cell code:
if(intent.isEmpty())     
    intents[0].name =="CREDITCARD_BALANCE" 
else   
    false;
```

### **How can you create a Rule Cell?**

![](/files/yekJk8fy3TcRIClg6rzR)

{% hint style="info" %}
Remember to verify your code by clicking on "Validate" before you "Save" it
{% endhint %}

In the Rule’s Cell value field you can insert the code snippet in 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 milliseconds.

**The Rule Cell will always be accompanied by a Not Expected Cell**, which acts to predict all user interactions outside the guidelines.

The variables below can be used in Syntphony CAI on the Insert code field. Just copy-paste the formula in the table according your scenario:

| Predict responses  according to:    | Function                                                                      | Formula                            |
| ----------------------------------- | ----------------------------------------------------------------------------- | ---------------------------------- |
| Text                                | to use the last user's writing in the response                                | text                               |
| Information about the virtual agent | to use the name of the virtual agent in the answer                            | info.bot                           |
| Information about the channel       | to use the channel's name in the answer                                       | info.channelName                   |
| Information about the channel type  | to use the channel's type in the answer (if it's web, Facebook, Alexa, etc..) | info.channelType                   |
| Session Code                        | to inform the UUID/GUID in the answer                                         | sessionCode                        |
| Code                                | to use in the response the Code of the last user's writing                    | code                               |
| Parameter                           | to use in the response the parameters' value                                  | parameters\['parameter's\_name']   |
| Open Context                        | to use in the response the Open Context's value                               | opencontext.information's\_name    |
| Visible Context                     | to use in the response the Visible Context's value                            | visiblecontext.information's\_name |
| Hidden Context                      | to use in the response the Hidden Context's value                             | hiddencontext.information's\_name  |

{% hint style="info" %}
**Important:** The variables within the contexts are created by the user, not by the Syntphony CAI platform
{% endhint %}

### **How to test the assertiveness of the Rule Cell?**

You can test the assertiveness of the Rule Cell and also predict responses with the last user input by using in the Answer Cell a Dynamic Answer (to get more information about [Dynamic Answers](/user-guide/build-dialogs/dialog-cells/answer.md#d-dynamic-answer))

**If you’re not familiar with codes, you can copy paste the following shortcuts and put it in the Answer Cell, as a Dynamic Answer:**

| Predict responses  according to:    | Function                                                                      | Formula                                                                                               |
| ----------------------------------- | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| Intents                             | to use in the response the main Intent of a conversation                      | <p>$intents\[0].name <em>(OBS: this formula stays the same, no matter the Intent's name)</em><br></p> |
| Entities                            | to use in the response the main Entity of a conversation                      | $entities\['entity's\_name']                                                                          |
| Text                                | to use the last user's writing in the response                                | $text                                                                                                 |
| Information about the virtual agent | to use the name of the virtual agent in the answer                            | $info.bot                                                                                             |
| Information about the channel       | to use the channel's name in the answer                                       | $info.channelName                                                                                     |
| Information about the channel type  | to use the channel's type in the answer (if it's web, Facebook, Alexa, etc..) | $info.channelType                                                                                     |
| Session Code                        | to inform the UUID/GUID in the answer                                         | $sessionCode                                                                                          |
| Code                                | to use in the response the Code of the last user's writing                    | $code                                                                                                 |
| Parameter                           | to use in the response the parameters' value                                  | $parameters\['parameter's\_name']                                                                     |
| Open Context                        | to use in the response the Open Context's value                               | $opencontext.information's\_name *(registered in the code cell)*                                      |
| Visible Context                     | to use in the response the Visible Context's value                            | $visiblecontext.information's\_name *(registered in the code cell)*                                   |
| Hidden Context                      | to use in the response the Hidden Context's value                             | $hiddencontext.information's\_name *(registered in the code cell*                                     |

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

<table data-header-hidden><thead><tr><th width="205">Name</th><th>Type</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td><strong>Name</strong></td><td><strong>Type</strong></td><td><strong>Required</strong></td><td><strong>Description</strong></td></tr><tr><td><strong>name</strong></td><td>String</td><td>Yes</td><td>Name of the intent, same as the NLP</td></tr><tr><td><strong>confidence</strong></td><td>Double</td><td>Yes</td><td>Confidence score returned by the NLP, this will be a percentage number from 0 to 1.</td></tr></tbody></table>

{% 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). |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.conversational-ai.syntphony.com/user-guide/build-dialogs/dialog-cells/rule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
