# `ExGram.FSM.Middleware`
[🔗](https://github.com/rockneurotiko/ex_gram_fsm/blob/v0.1.0/lib/ex_gram/fsm/middleware.ex#L1)

ExGram middleware that loads FSM state before every handler call.

This middleware is automatically registered by `use ExGram.FSM`. You don't
need to add it manually.

## What it does

Before every `handle/2` call, this middleware:
1. Extracts a storage key from the update using the configured `ExGram.FSM.Key` module
2. Reads the FSM state from the configured storage backend
3. Writes the state and config into `context.extra`

## Context keys populated

After this middleware runs, `context.extra` contains:

| Key | Type | Description |
|-----|------|-------------|
| `:fsm` | `%ExGram.FSM.State{}` | Current flow + state + data |
| `:fsm_key` | `term()` | The storage key (shape depends on configured key module) |
| `:fsm_storage` | module | Storage backend module |
| `:fsm_flows` | `%{atom => module}` | Map of registered flow name → flow module |
| `:fsm_on_invalid_transition` | atom or tuple | Invalid transition policy |

## FSM key strategy

The key is extracted by the module set via the `key:` option in `use ExGram.FSM`
(default: `ExGram.FSM.Key.ChatUser`). See `ExGram.FSM.Key` for built-in options
and how to implement a custom strategy.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
