1.1.1 • Published 6 years ago

telegraf-session-mongo v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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.

1.1.1

6 years ago

1.1.0

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago