Structure of the session state.
Configuration options for initial state and storage backend.
A MiddlewareFn ready to be used with app.use().
interface MySession {
count: number;
}
app.use(session<MySession>({
initial: () => ({ count: 0 }),
}));
app.addHandler(new CommandHandler("counter", async (update, context) => {
const ctx = context as CallbackContext & { session: MySession };
ctx.session.count++;
await context.bot.sendMessage({
chat_id: update.effective_chat!.id,
text: `You have called this command ${session.count} times!`,
});
}));
Creates a session management middleware.
Automatically loads user or chat session state before handlers execute, exposes
context.session, and persists modified state after execution finishes.