MySQL-powered session middleware for Telegraf (Telegram Bot Framework) v5.3.0. Persists session data both in MySQL and in-memory for performance, with automatic table creation, configurable lifetime and garbage collection. Requires Node >=12 and MySQL >=5.5.62. Forked from telegraf-session-redis, focused on SQL-based storage. Supports scoped sessions per user, chat, or custom key functions.
npm install telegraf-session-mysqlNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Minimal bot with session counter using MySQL middleware. Shows setup, connection, and usage.
Ensure mysql2 is installed (npm install mysql2); no code changes needed unless you used direct mysql connection methods.
Update getSessionKey to accept ctx and use ctx.from, ctx.chat as needed.
Use saveSession() explicitly if you need guaranteed persistence. Not suitable for critical data without additional persistence layer.
Set { table: 'bot_sessions' } in constructor options.Set interval to a reasonable value or implement your own cleanup.
Run: npm install mysql2
Ensure you use 'new MySQLSession(...)' and call .middleware() on the instance.
Create the database manually: CREATE DATABASE telegraf_sessions;
Check that telegraf.use(session.middleware()) is called before any handlers, and verify getSessionKey returns a valid key.