Skip to content

ExceedeMedia/telegraf-session-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

MongoDB session middleware for Telegraf

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published