2.0.6 • Published 2 years ago
telegraf-session-mongoose v2.0.6
MongoDB session middleware for Telegraf
Fork from alexnzarov
MongoDB powered simple session middleware for Telegraf 4.0 with TypeScript support.
Installation
$ npm install telegraf-session-mongoose
$ yarn add telegraf-session-mongoose
Example
import { Telegraf } from 'telegraf';
import mongoose from 'mongoose';
import { session } from 'telegraf-session-mongoose';
const bot = new Telegraf(process.env.BOT_TOKEN);
const init = async () => {
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'telegram'
});
bot.use(session({ collectionName: 'sessions' }));
bot.launch();
}
init();
Example (TypeScript)
import { Context, Telegraf } from 'telegraf';
import mongoose from 'mongoose';
import { session } from 'telegraf-session-mongoose';
export interface SessionContext extends Context {
session: any;
};
const bot = new Telegraf<SessionContext>(process.env.BOT_TOKEN);
const init = async () => {
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'telegram'
});
bot.use(session({ sessionName: 'session', collectionName: 'sessions' }));
bot.launch()
}
API
Options
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)