olexnzarov / telegraf-session-mongodb

MongoDB session middleware for Telegraf
25 stars 14 forks source link

ctx.session is undefined. #9

Closed haliliceylan closed 3 years ago

haliliceylan commented 4 years ago

This is my code.

const { Telegraf } = require('telegraf')
const { TelegrafMongoSession } = require('telegraf-session-mongodb');

const bot = new Telegraf(process.env.TOKEN);
bot.on('message', (ctx) => ctx.reply(JSON.stringify({message:ctx.session})))

 TelegrafMongoSession.setup(bot, process.env.MONGODB_URI)
  .then((client) => bot.launch())
  .catch((err) => console.log(`Failed to connect to the database: ${err}`));

when i write to bot, bot is sending to me a {}. so ctx.session is undefined. please help me. there is no error on console. i setted environment variables.

versions

    "telegraf": "^3.38.0",
    "telegraf-session-mongodb": "^1.3.1"

mongodb schema url form: mongodb://myhidden.domain:32771/myhiddendb

olexnzarov commented 4 years ago

Hey.

You should call TelegrafMongoSession.setup before you add any event handlers, like this:

const bot = new Telegraf(process.env.TOKEN);

TelegrafMongoSession.setup(...);

bot.on(...);