node.js - 仅通过使用 telegraf.js 的命令启动电报场景
问题描述
我试图仅通过确切的命令开始一些场景/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()
解决方案
尝试在您的代码中替换此部分:
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();
推荐阅读
- wordpress - 在 Wordpress 中缩小后 CSS 代码更改
- typescript - TypeScript:将普通对象混入类中
- node.js - 为什么自适应卡总是显示错误出了点问题请在提交时重试?
- typescript - 在自动生成 swagger api 上定义错误模型
- singleton - 具有干净 arhirecure 的单例
- python - 具有两个不同 proto 文件的重定向 grpc 服务器
- c++ - 我们如何访问像 WM_CTLCOLOR 这样的属性表的窗口消息?
- powerbi - 在 PowerBI 中创建摊销表 - 循环参考
- javascript - 在 Typescript 应用程序中找不到 JSON
- laravel - Laravel-Controller 不包含所有上传的图片