# Flow

When a bot receives a message, it performs 2 tasks:

1. Identify **what to do**
2. Decide **how to respond**

The platform uses **Rules** to help your bot identify *what to do*, and **Stories** to help it decide *how to respond*. When creating a flow, configuring a rule first then a story next helps avoid duplicating stories.

![Flow](https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LirAdLo22OkAW9w3tvY%2F-MjI_kT3tSBe5lSFpEyE%2F-MjIbh50itxkhFekFSOB%2FScreenshot%202021-09-11%20at%202.49.55%20PM.png?alt=media\&token=5f2b05b4-cfb1-42af-8377-28f2b36f934d)

## Rule

A rule can also be considered a *question* from a user. When your bot receives a message, it tests the message against all its rules from the top rule (P1) to the bottom rule (PN). The first rule that passes the test is chosen as the **Passing Rule.**

Let's create a rule called "*user is asking bank account balance"* in our bot.

![Sample Rule for Bank Account Balance Query](https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LirAdLo22OkAW9w3tvY%2F-MjDYlFk9Wk6eBEWmuai%2F-MjE1otJnbYF6EKrfk_O%2FScreenshot%202021-09-10%20at%205.30.17%20PM.png?alt=media\&token=a6f9134a-47bb-491a-9225-243e7157838d)

{% hint style="info" %}
Rules are tested in order of appearance. Higher rules have higher priority (P1, P2, etc.), and lower rules have lower priority. Rules can be re-ordered in any way you want them to be tested.
{% endhint %}

A rule must have one or more conditions.

## Condition

A boolean expression that returns *true* or *false*. When **all conditions** in a rule are *true*, the rule becomes the Passing Rule.&#x20;

A condition tests only one context of a message-receiving interaction, either message, memory, user attributes, or NLP.&#x20;

Each condition has 3 parts to it:&#x20;

* Property
* Function
* Value.

For our example above, we include two conditions (C1 and C2) to our rule:

<table data-header-hidden><thead><tr><th width="109"></th><th width="137">Context</th><th width="202">Property</th><th>Function</th><th>Value</th></tr></thead><tbody><tr><td></td><td>Context</td><td>Property</td><td>Function</td><td>Value</td></tr><tr><td>C1</td><td><code>message</code></td><td><code>text</code></td><td><code>has keyword</code></td><td><code>balance</code></td></tr><tr><td>C2</td><td><code>memory</code></td><td><code>ask-bank-account</code></td><td><code>equals</code></td><td><code>true</code></td></tr></tbody></table>

<figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FkNfPVrT4XMPAHr1Iexhy%2FNew%20Project%20(23).png?alt=media&#x26;token=2edd97fb-95b2-4b07-ace1-45f31443a278" alt=""><figcaption></figcaption></figure>

This is a sample conversation with the rule above

> **User:** My account\
> **Bot**: Sure, what would you like to know? \
> `Bot uses an action to store a memory property: ask-bank-account, value: true`\
> \----------\
> **User:** What is my balance?\
> `Bot checks its rules and finds the Passing Rule: user is asking bank account balance. Bot has now IDENTIFIED WHAT TO DO`

When the Passing Rule is found, the bot replies with the rule's Story.

## Story

A collection of responses returned from the bot.&#x20;

In its simplest form, consider a story an *answer* from the bot. It may contain one or many text, image, video, audio, document, card, location, and sticker responses. It can also be used to make your bot display buttons for the user to click, and perform certain actions on a context or trigger webhooks.

Let's create a story for the above example called "*user is asking about bank balance".*

<figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FW0JHzNKsGkpRcI70xUQB%2FGroup%20219.png?alt=media&#x26;token=6ead003e-4041-407d-b5d8-fa0ef8f53fd9" alt=""><figcaption><p>Sample Story</p></figcaption></figure>

In this story, we created 2 responses and 2 quick replies.&#x20;

1. Added a Webhook response to get the user's bank account information from our imaginary custom banking infrastructure, which returns a new memory property `bank-balance`.&#x20;
2. Added a Text Response displaying the bank balance with a *Refresh Now* button.&#x20;
3. Added 2 quick replies:&#x20;
   1. *How to improve*&#x20;
   2. *Contact Customer Support*.

The interaction in our example is complete

> **User:** My account\
> **Bot**: Sure, what would you like to know? \
> `Bot uses an action to store a memory property: ask-bank-account, value: true`\
> \----------\
> **User:** What is my balance?\
> `Bot checks its rules and the Passing Rule: user is asking bank account balance. Bot has now IDENTIFIED WHAT TO DO`\
> \----------\
> `Bot selects the rule's story to DECIDE HOW TO RESPOND`\
> **Bot:** Your current balance $536.12
