1

I'm trying to start some scene only by exact command /start I'm using Telegraf.js library that's not going well when I do middleware it starts automatically when I send some other input and not just /start

how can I solve it? thanks.

bot.use(session())

const userWizard = new WizardScene('user-wizard',
  (ctx) => {
    ctx.reply("What is your name?");

    //Necessary for store the input
    ctx.scene.session.user = {};

    //Store the telegram user id
    ctx.scene.session.user.userId = ctx.from.id;
    return ctx.wizard.next();
  },
  (ctx) => {

    //Validate the name
    if (ctx.message.text.length < 1 || ctx.message.text.length > 12) {
      return ctx.reply("Name entered has an invalid length!");
    }

    //Store the entered name
    ctx.scene.session.user.name = ctx.message.text;
    ctx.reply("What is your last name?");
    return ctx.wizard.next();
  },
  async (ctx) => {

    //Validate last name
    if (ctx.message.text.length > 30) {
      return ctx.reply("Last name has an invalid length");
    }

    ctx.scene.session.user.lastName = ctx.message.text;

    //Store the user in a separate controller
    // userController.StoreUser(ctx.scene.session.user);
    return ctx.scene.leave(); //<- Leaving a scene will clear the session automatically
  }
);

const stage = new Stage([userWizard], { default: 'user-wizard' })
bot.use(stage)
bot.command('/start', ctx => {
  stage.start(ctx)
}
)
bot.launch()
Syntle
  • 5,168
  • 3
  • 13
  • 34
Oshrr Hagag
  • 21
  • 1
  • 3

1 Answers1

1

Try to replace this part in your code:

const stage = new Stage([userWizard], { default: 'user-wizard' })
bot.use(stage)
bot.command('/start',ctx => {
  stage.start(ctx)
}
)

With this:

const stage = new Stage([userWizard]);

bot.use(session()); 
bot.use(stage.middleware()); 
bot.command('/start',(ctx) => ctx.scene.enter('user-wizard')); 
bot.launch();
Stephan
  • 881
  • 9
  • 24