telegraf-session-mongoose
TypeScript icon, indicating that this package has built-in type declarations

2.0.6 • Public • Published

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)

Package Sidebar

Install

npm i telegraf-session-mongoose

Weekly Downloads

10

Version

2.0.6

License

MIT

Unpacked Size

9.62 kB

Total Files

9

Last publish

Collaborators

  • oranjevo