skip to Main Content

I am creating a telegram bot using telegraf using wizards. I would like to have several wizards in my bot. How can I enter to another scene? I am using the below code and I am getting error Cannot read property 'enter' of undefined.

const Stage = require("telegraf/stage");
const WizardScene = require("telegraf/scenes/wizard");
const wiz1 = new WizardScene('task1',
    ctx => {...},
    ctx => {...}
);
const wiz2 = new WizardScene('task2',
    ctx => {...},
    ctx => {...}
);
const wiz3 = new WizardScene('task3',
    ctx => {...},
    ctx => {...}
);

const stage =new Stage([wiz1,wiz2,wiz3],{default: 'task1'})
bot.hears('anAction', (ctx) => {Stage.enter('wiz2')}); // this does not work

2

Answers


  1. This is a late response, but it may help someone.
    You need to register the middleware of the stages after const stage = new Stage ... and before bot.hears...

    bot.use(stage.middleware())
    

    And also you can enter the scene with their ids not the name of the variable. So instead of Stage.enter('wiz2') use Stage.enter('task2') to enter wizard2.

    Login or Signup to reply.
  2. And also make sure that bot.use(stage.middleware()) should be registred after bot.use(session());. Like that:

    bot.use(session());
    bot.use(stage.middleware());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search