Overview
State keepers provide the foundation for managing conversation state in Telegrator. TheStateKeeperBase class defines a generic state management system that tracks state values associated with Telegram updates.
StateKeeperBase<TKey, TState>
Abstract base class for all state keeper implementations.Type Parameters
The type of key used for state resolution (typically
long for user/chat IDs). Must be non-null.The type of state values (e.g.,
int, string, enum). Must be non-null.Properties
Gets or sets the key resolver used to extract keys from updates. This determines whether states are tracked per-user, per-chat, or using a custom resolution strategy.
Abstract property that defines the default state value when a new state is created. Must be implemented by derived classes.
Public Methods
Sets the state for the specified update to a new value.Parameters:
keySource(Update): The Telegram update to use as a key sourcenewState(TState): The new state value to set
Gets the current state for the specified update.Parameters:
keySource(Update): The Telegram update to use as a key source
KeyNotFoundException if no state exists for the keyAttempts to get the state for the specified update without throwing an exception.Parameters:
keySource(Update): The Telegram update to use as a key sourcestate(out TState?): The state value if found; otherwise, default value
true if state exists; otherwise falseChecks whether a state exists for the specified update.Parameters:
keySource(Update): The Telegram update to use as a key source
true if a state exists; otherwise falseCreates a new state for the specified update using the
DefaultState value.Parameters:keySource(Update): The Telegram update to use as a key source
Removes the state for the specified update.Parameters:
keySource(Update): The Telegram update to use as a key source
Moves the state forward to the next value. If no state exists, creates one with the default value first.Parameters:
keySource(Update): The Telegram update to use as a key source
Moves the state backward to the previous value. If no state exists, creates one with the default value.Parameters:
keySource(Update): The Telegram update to use as a key source
Protected Abstract Methods
These methods must be implemented by derived classes to define state transition logic:Calculates the next state value when moving forward.Parameters:
currentState(TState): The current state valuecurrentKey(TKey): The resolved key
Calculates the previous state value when moving backward.Parameters:
currentState(TState): The current state valuecurrentKey(TKey): The resolved key
IStateKeyResolver<TKey>
Interface for resolving keys from Telegram updates.Methods
Extracts a key from the specified update.Parameters:
keySource(Update): The Telegram update to resolve the key from
Built-in State Keepers
Telegrator provides three built-in state keeper implementations:NumericStateKeeper
Manages integer-based states with automatic increment/decrement.StringStateKeeper
Manages string-based named states. Does not implement automatic navigation.EnumStateKeeper<TEnum>
Manages enum-based states with automatic navigation through enum values.ArrayStateKeeper<TKey, TState>
Abstract base class for state keepers that navigate through a fixed array of states.Constructor
Initializes the array state keeper with a sequence of allowed states.Parameters:
states(params TState[]): The array of states that define the allowed sequence
Protected Members
The array of states that defines the allowed state sequence for navigation.
Behavior
MoveForward()advances to the next element in the arrayMoveBackward()retreats to the previous element in the array- Throws
ArgumentExceptionif current state is not found in the array - Throws
IndexOutOfRangeExceptionwhen attempting to move beyond array boundaries
Creating Custom State Keepers
You can create custom state keeper implementations by inheriting fromStateKeeperBase:
State Storage
State keepers use an in-memory dictionary for state storage:Key Resolution
TheKeyResolver property determines how states are associated with updates. Telegrator provides two built-in resolvers:
- SenderIdResolver: Resolves to the user ID of the message sender (per-user state)
- ChatIdResolver: Resolves to the chat ID (per-chat state)
Thread Safety
Best Practices
-
Choose the Right State Type: Use
NumericStatefor linear flows,StringStatefor flexible flows, andEnumStatefor type-safe sequential flows -
Handle State Absence: Always check if a state exists before operating on it:
-
Clean Up States: Delete states when conversations end to prevent memory leaks:
-
Use SpecialState.AnyState for Common Actions: Handlers like “cancel” or “help” should work regardless of current state:
-
Handle Navigation Exceptions: Catch
IndexOutOfRangeExceptionwhen usingMoveForward/MoveBackwardto gracefully handle boundary conditions
See Also
- NumericState - Integer-based state management
- StringState - String-based state management
- EnumState - Enum-based state management
- Filters - General filtering concepts