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

2.0.2 • Public • Published

MongoDB session middleware for Telegraf

MongoDB powered simple session middleware for Telegraf 4.0 with TypeScript support.

Installation

$ npm install telegraf-session-mongodb
$ yarn add telegraf-session-mongodb

Example

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' }));
  });

Example (TypeScript)

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' }));
  });

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-mongodb

Weekly Downloads

176

Version

2.0.2

License

MIT

Unpacked Size

9.29 kB

Total Files

9

Last publish

Collaborators

  • krekeris
  • alexnzarov