TelegramEx.FSM (TelegramEx v1.3.0)

Copy Markdown View Source

Per-user Finite State Machine backed by Pockets (ETS).

Stores {state, data} per chat and provides defstate/2 for state-specific handlers. See Finite State Machines for workflow examples.

Summary

Functions

Defines handlers that only execute when the user is in a specific FSM state.

Retrieves the current FSM state and data for a user.

Initializes the FSM storage for a bot.

Resets the FSM state for a user, removing their stored state and data.

Sets the FSM state for a user, keeping existing data.

Sets the FSM state and data for a user.

Types

chat_id()

@type chat_id() :: TelegramEx.Types.chat_id()

Functions

defstate(state, list)

(macro)

Defines handlers that only execute when the user is in a specific FSM state.

See Finite State Machines for a complete multi-step example.

get_state(name, id)

@spec get_state(atom(), chat_id()) :: {term(), term()}

Retrieves the current FSM state and data for a user.

Returns {nil, nil} when the chat has no stored state.

init(name)

@spec init(atom()) :: :ok | {:error, term()}

Initializes the FSM storage for a bot.

This is called automatically by TelegramEx.Server when the bot starts.

reset_state(name, id)

@spec reset_state(atom(), chat_id()) :: atom() | {:error, term()}

Resets the FSM state for a user, removing their stored state and data.

set_state(name, id, state)

@spec set_state(atom(), chat_id(), atom()) :: atom() | {:error, term()}

Sets the FSM state for a user, keeping existing data.

set_state(name, id, state, data)

@spec set_state(atom(), chat_id(), atom(), term()) :: atom() | {:error, term()}

Sets the FSM state and data for a user.