IlyaSemenov / grammy-scenes

Nested named scenes for grammY
MIT License
26 stars 0 forks source link

Exit from scene #6

Closed Gercov closed 2 years ago

Gercov commented 2 years ago

I create a scene where the user should enter the text, but I want the opportunity to cancel the input by the Bot command. How can I continue to execute the command if the handlers outside the scene are already registered on them?

next() does not work

export const banuserScene = new Scene<MyContext>('banuser');

banuserScene.do(async (ctx) => {
  await ctx.editMessageText(phrases.banuserInfo);
});

banuserScene.wait().on('message:text', async (ctx, next) => {
  ctx.scene.exit()
  return next();
});
IlyaSemenov commented 2 years ago

Sorry, I am not following. Could you come up with a user story? Like what the user sends and how do you expect the bot to react?

Just a shot in the dark, make sure you register command handlers before the scenes processor. This way, /commands will have the priority.

Gercov commented 2 years ago

Sorry, I am not following. Could you come up with a user story? Like what the user sends and how do you expect the bot to react?

Just a shot in the dark, make sure you register command handlers before the scenes processor. This way, /commands will have the priority.

Thank you very much, the error was that I was registering command handlers after registering scenes, because of which it did not work

Gercov commented 2 years ago

But now another problem has appeared, how do I end the scene if nothing happens inside it when I enter another command of the command bot?

IlyaSemenov commented 2 years ago

One may exit the current scene with ctx.scene.exit() (from a scene handler) if that's what you mean. Otherwise, please come up with a particular example.

Gercov commented 2 years ago

One may exit the current scene with ctx.scene.exit() (from a scene handler) if that's what you mean. Otherwise, please come up with a particular example.

Example: Being in a scene where you need to enter some text for further actions, a person decides to cancel it and calls another bot command. It is processed by a handler outside the scene, but the scene is not completed. How to fix it?

IlyaSemenov commented 2 years ago

You may add this to your command handler:

delete ctx.session.scenes

or you may do that universally for all commands with something like:

bot.on("message:text", (ctx, next) => {
  if (ctx.message.text.startsWith("/")) {
    delete ctx.session.scenes
  }
  return next()
})

I think we should add something like ctx.scenes.abort() so that user doesn't need to manually edit the session data.

Gercov commented 2 years ago

Вы можете добавить это в свой обработчик команд:

удалить ctx.session.scenes

или вы можете сделать это универсально для всех команд с помощью чего-то вроде:

bot.on("message:text", (ctx, next) => {
  if (ctx.message.text.StartsWith("/")) {
    delete ctx.session.scenes
  }
  return next()
})

Я думаю, мы должны добавить что-то вроде ctx.scenes.abort()того, чтобы пользователю не нужно было вручную редактировать данные сеанса.

Thanks