SessionPlugin<CTX extends Context, TSession> class

A comprehensive session management plugin.

This plugin provides persistent session storage for bots, allowing them to remember data across updates. Sessions are stored per chat and automatically loaded/saved with each update.

Example usage:

// Basic session with Map storage
bot.plugin(SessionPlugin<Context, Map<String, dynamic>>(
  initial: () => {'count': 0, 'name': ''},
));

// Custom session key generation
bot.plugin(SessionPlugin<Context, UserSession>(
  initial: () => UserSession(),
  getSessionKey: (ctx) => 'user_${ctx.from?.id ?? 0}',
));

// Usage in handlers
bot.command('count', (ctx) async {
  final session = ctx.session as Map<String, dynamic>;
  session['count'] = (session['count'] as int) + 1;
  await ctx.reply('Count: ${session['count']}');
});
Implemented types

Constructors

SessionPlugin.new({required TSession initial(), String getSessionKey(CTX ctx)?, SessionStorage<TSession>? storage, String? keyPrefix})
Creates a session plugin.

Properties

dependencies List<String>
List of plugin names that this plugin depends on.
no setteroverride
description String
Optional description of what this plugin does.
no setteroverride
getSessionKey String Function(CTX ctx)
Function to generate session keys from context.
final
hashCode int
The hash code for this object.
no setterinherited
initial → TSession Function()
Factory function to create initial session data.
final
keyPrefix String?
Optional prefix for session keys.
final
name String
The unique name of this plugin.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
storage SessionStorage<TSession>
The storage backend for session data.
final
version String
The version of this plugin.
no setteroverride

Methods

install(Bot<CTX> bot) → void
Installs the plugin on the given bot.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
uninstall(Bot<CTX> bot) → void
Uninstalls the plugin from the given bot.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited