MongoDB powered simple session middleware for Telegraf 4.0 with TypeScript support.
$ npm install telegraf-session-mongodb
$ yarn add telegraf-session-mongodb
Full JavaScript example can be found here.
const { Telegraf } = require('telegraf');
const { MongoClient } = require('mongodb');
const { session } = require('telegraf-session-mongodb');
const bot = new Telegraf(process.env.BOT_TOKEN);
MongoClient.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(client => {
const db = client.db();
bot.use(session(db, { collectionName: 'sessions' }));
});
Full TypeScript example can be found here.
import { Context, Telegraf } from 'telegraf';
import { MongoClient } from 'mongodb';
import { session } from 'telegraf-session-mongodb';
export interface SessionContext extends Context {
session: any;
};
const bot = new Telegraf<SessionContext>(process.env.BOT_TOKEN);
MongoClient.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(client => {
const db = client.db();
bot.use(session(db, { sessionName: 'session', collectionName: 'sessions' }));
});
collectionName
: name for MongoDB collection (default:sessions
)sessionName
: context property name (default:session
)sessionKeyFn
: function that generates the session key from the context (default implementation, legacy deprecated function)