MongoDB-based session middleware for the Telegraf Telegram bot framework (v4.0+). Current stable version is 2.0.2. Provides persistent session storage using MongoDB, with support for custom collection names and session key generation. Ships TypeScript definitions, requires both `telegraf` and `mongodb` as peer dependencies. Differentiates from in-memory or file-based session stores by offering scalability and persistence via MongoDB.
npm install telegraf-session-mongodbNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Sets up a Telegram bot with Telegraf and MongoDB session middleware, increments a counter on /start.
Upgrade to Telegraf 4.0+ or use version 1.x of this package.
Provide a custom sessionKeyFn to maintain backward compatibility: session(db, { sessionKeyFn: (ctx) => ctx.from?.username ?? ctx.chat?.id ?? 0 })Use the new default or provide a custom sessionKeyFn.
Ensure bot.use(session(...)) is called before bot.use(yourOtherMiddleware).
Wrap bot.use() inside the .then() of MongoClient.connect() as shown in the quickstart.
Remove those options when using MongoDB driver 4.0+.
Use import { session } from 'telegraf-session-mongodb' instead of import session from 'telegraf-session-mongodb'.Make sure bot.use(session(...)) is called before any handler that uses ctx.session.
Keep the MongoClient connected for the lifetime of the bot, and close it only on process termination.
Ensure db is obtained from a resolved MongoClient.connect() promise.