1

我试图仅通过确切的命令开始一些场景/start 我正在使用Telegraf.js库,当我做中间件时它运行不顺利,当我发送一些其他输入时它会自动启动,而不仅仅是/start

我该如何解决?谢谢。

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()
4

1 回答 1

1

尝试在您的代码中替换此部分:

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

有了这个:

const stage = new Stage([userWizard]);

bot.use(session()); 
bot.use(stage.middleware()); 
bot.command('/start',(ctx) => ctx.scene.enter('user-wizard')); 
bot.launch();
于 2020-04-11T02:44:13.393 回答