You are viewing documentation for our open source project which is maintained by the community. If you want to get started building assistants with Rasa please check out our latest [documentation here](/content/docs/index.html).

**Don't overuse rules**. Rules are great to handle small specific conversation patterns, but unlike [stories](https://legacy-docs-oss.rasa.com/docs/rasa/next/stories), rules don't have the power to generalize to unseen conversation paths. Combine rules and stories to make your assistant robust and able to handle real user behavior.

If you can't decide whether to write a story or a rule to implement a certain behavior, see the best practices for [Writing Conversation Data](https://legacy-docs-oss.rasa.com/docs/rasa/next/writing-stories).

For additional examples on implementing rules in a Rasa assistant, refer to our [Rules example bot](https://github.com/RasaHQ/rasa/tree/main/examples/rules).

## Writing a Rule

Before you start writing rules, you have to make sure that the [Rule Policy](https://legacy-docs-oss.rasa.com/docs/rasa/next/policies#rule-policy) is added to your model configuration:

```yaml
policies:
  - ... # Other policies
  - name: RulePolicy
```

Rules can then be added to the `rules` section of your training data.

To indicate that a rule can apply at any point in a conversation, start with the intent which starts the conversation and then add the actions which your assistant should perform in response to that.

```yaml
rules:
  - rule: Say `hello` whenever the user sends a message with intent `greet`
    steps:
      - intent: greet
      - action: utter_greet
```

This example rule applies at the start of conversation as well as when the user decides to send a message with an intent `greet` in the middle of an ongoing conversation.

Dialogue turns that only appear as rules in the training data and do not appear in stories will be ignored by ML-only policies like `TEDPolicy` at prediction time.

```yaml
rules:
  - rule: Say `hello` whenever the user sends a message with intent `greet`
    steps:
      - intent: greet
      - action: utter_greet
```

### Rules for the Conversation Start

To write a rule which only applies at the beginning of a conversation, add a `conversation_start: true` to your rule:

```yaml
rules:
  - rule: Say `hello` when the user starts a conversation with intent `greet`
    conversation_start: true
    steps:
      - intent: greet
      - action: utter_greet
```

If a user sends a message with the intent `greet` later in the conversation, the rule will not match.

### Rules with Conditions

Conditions describe requirements which have to be fulfilled for a rule to be applicable. To do so, add any information about the prior conversation under the `condition` key:

```yaml
rules:
  - rule: Only say `hello` if the user provided a name
    condition:
      - slot_was_set:
          - user_provided_name: true
    steps:
      - intent: greet
      - action: utter_greet
```

Possible information that you can include under `condition` includes `slot_was_set` events and `active_loop` events.

### Skip Waiting for User Input at the End of a Rule

By default, rules will wait for the next user message when finished with the last step:

```yaml
rules:
  - rule: Rule which will wait for user message when it was applied
    steps:
      - intent: greet
      - action: utter_greet
      # - action: action_listen
      # Every rule implicitly includes a prediction for `action_listen` as last step.
      # This means that Rasa will wait for the next user message.
```

If you want to hand over the next action prediction to another story or rule, add `wait_for_user_input: false` to your rule:

```yaml
rules:
  - rule: Rule which will not wait for user message once it was applied
    steps:
      - intent: greet
      - action: utter_greet
        wait_for_user_input: false
```

This indicates that the assistant should execute another action before waiting for more user input.

### Abort a Rule

Rules are designed to handle multiple output steps of a chatbot. They are terminated as soon as user interaction is required. This happens automatically via [launching a form](https://legacy-docs-oss.rasa.com/docs/rasa/next/rules/#rules-and-forms), since it starts with the user input of the first slot. Therefore all steps after launch are ignored.

Termination, however, can also be achieved manually. This can be useful to implement conditional termination criteria. Here is an example:

```yaml
rules:
  - rule: Rule which will be conditionally terminated
    steps:
      - intent: greet
      - action: action_check_termination
      - action: utter_greet
        wait_for_user_input: true
```

```python
from rasa_sdk import Action
from rasa_sdk.events import FollowupAction

class ActionCheckTermination(Action):
    def name(self):
        return "action_check_termination"

def run(self, dispatcher, tracker, domain):
        # your business logic here
        should_terminate = check_for_termination(<params>)
        if should_terminate:
            return [FollowupAction("action_listen")]
        return []
```

utter_greet is never executed when termination is done, even after user input, because it causes a new intent prediction.

### Rules and Forms

When a [Form](https://legacy-docs-oss.rasa.com/docs/rasa/next/forms) is active, the bot will make predictions based on how the form is defined, ignoring rules. Rules become applicable again if:

- the form fills all required slots
- the form rejects its execution (see [Handling Unhappy Paths](https://legacy-docs-oss.rasa.com/docs/rasa/next/forms#writing-stories--rules-for-unhappy-form-paths) for more details)
