# 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](/files/-MjIbh50itxkhFekFSOB)

## 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](/files/-MjE1otJnbYF6EKrfk_O)

{% 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="/files/9fVwjmSexeG0UIMhgQq0" 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="/files/GDoSpLUpZujg1vmOwXFS" 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


---

# 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.botdistrikt.com/how-botdistrikt-works/flow.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.
