首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅通过使用telegraf.js的命令启动telegraf场景

仅通过使用telegraf.js的命令启动telegraf场景
EN

Stack Overflow用户
提问于 2019-12-06 15:21:45
回答 1查看 1.3K关注 0票数 1

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

我该怎么解决它呢?谢谢。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2020-04-11 10:44:13

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

代码语言:javascript
复制
const stage = new Stage([userWizard], { default: 'user-wizard' })
bot.use(stage)
bot.command('/start',ctx => {
  stage.start(ctx)
}
)

有了这个:

代码语言:javascript
复制
const stage = new Stage([userWizard]);

bot.use(session()); 
bot.use(stage.middleware()); 
bot.command('/start',(ctx) => ctx.scene.enter('user-wizard')); 
bot.launch();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59208629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档