telegraf-session-mongo

1.1.1 • Public • Published

MongoDB Session Middleware for Telegraf

MongoDB powered session storage middlware for Telegraf.

Requirements

  • node >= 7.0
  • --harmony-async-await

If you want to contribute a transpiled distribution - pull requests welcome.

Installation

npm install telegraf-session-mongo --save

Example

const Telegraf = require('telegraf');
const { MongoClient } = require('mongodb');
const MongoSession = require('telegraf-session-mongo');
 
const app = new Telegraf(process.env.BOT_TOKEN);
 
MongoClient.connect(process.env.MONGO_URL).then(client => {
 
        const session = new MongoSession(client, {
            // ttl - in milliseconds
            // property - name of the context property for the session (default: session)
            // collection - name of the mongodb collection for the sessions (default: sessions)
            // getSessionKey - function (ctx) => String (default "chatId:fromId")
        });
 
        // Setup function creates necessary indexes for ttl and key lookup
        session.setup().then(() => {
            app.use(session.middleware);
 
            app.command("session", (ctx) => {
                ctx.respondWithHTML(`<pre>${ctx.session, null, 2}</pre>`);
            });
        });
});

Postscriptum

I hacked this together using telegraf-session-redis as inspiration. So it's lacking tests and a distribution compatible with Node < 7.0. Pull requests welcome.

Package Sidebar

Install

npm i telegraf-session-mongo

Weekly Downloads

2

Version

1.1.1

License

MIT

Unpacked Size

5.87 kB

Total Files

3

Last publish

Collaborators

  • vsviridov