Nadia. SessionStore behaviour
(nadia v1.5.0)
View Source
Behaviour and helpers for optional bot session storage.
Session storage is deliberately explicit. Nadia does not start or name a global session store for you; applications supervise a store backend and pass that store to their handlers when they need state.
children = [
{Nadia.SessionStore.ETS, name: MyApp.BotSessions}
]
store = {Nadia.SessionStore.ETS, MyApp.BotSessions}
{:ok, key} = Nadia.SessionStore.chat_key(context)
{:ok, session} = Nadia.SessionStore.update(store, key, fn session ->
Map.update(session, :seen, 1, &(&1 + 1))
end)The built-in ETS backend is intended for local development and single-node bots. Production applications can implement this behaviour with their own storage when they need persistence, cross-node consistency, or stronger concurrency guarantees.
Summary
Functions
Builds a chat-scoped session key from a context or update.
Builds a combined chat/user session key from a context or update.
Deletes a session from a store.
Reads a session from a store.
Writes a complete session map to a store.
Atomically updates a session in a store when the backend supports it.
Builds a user-scoped session key from a context or update.
Types
@type key() :: {:chat, integer() | binary()} | {:user, integer()} | {:chat_user, integer() | binary(), integer()}
Session keys used by Nadia helper functions.
@type session() :: map()
A session value.
Nadia keeps sessions as maps so backends can store arbitrary application state without Nadia prescribing a schema.
A concrete store reference.
The first element is the backend module implementing this behaviour. The
second element is backend-specific state, such as a pid or registered process
name for Nadia.SessionStore.ETS.
@type update_or_context() :: Nadia.Model.Update.t() | Nadia.Context.t()
Callbacks
Functions
@spec chat_key(update_or_context()) :: {:ok, key()} | {:error, :missing_chat_id}
Builds a chat-scoped session key from a context or update.
@spec chat_user_key(update_or_context()) :: {:ok, key()} | {:error, :missing_chat_id | :missing_user_id}
Builds a combined chat/user session key from a context or update.
Deletes a session from a store.
Reads a session from a store.
Missing sessions return {:ok, %{}}.
Writes a complete session map to a store.
@spec update(store(), key(), (session() -> session() | {:ok, session()} | {:error, term()})) :: {:ok, session()} | {:error, term()}
Atomically updates a session in a store when the backend supports it.
The update function receives the current session map, or %{} when the
session is missing. It may return a new session map, {:ok, session}, or
{:error, reason}. Error returns do not write a new value.
@spec user_key(update_or_context()) :: {:ok, key()} | {:error, :missing_user_id}
Builds a user-scoped session key from a context or update.