Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot go backwards in wizard #1078

Open
kzamulin opened this issue Mar 10, 2023 · 0 comments
Open

Cannot go backwards in wizard #1078

kzamulin opened this issue Mar 10, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@kzamulin
Copy link

kzamulin commented Mar 10, 2023

Code sample:

import { UseFilters, UseInterceptors } from '@nestjs/common';
import { Action, Ctx, Wizard, WizardStep } from 'nestjs-telegraf';
import { BotWizards } from 'src/common/enum/bot-wizards.enum';
import { TelegrafExceptionFilter } from 'src/common/filters/telegraf-exception.filter';
import { LoggingInterceptor } from 'src/common/interceptor/logger.interceptor';
import { TelegrafWizardContext } from 'src/common/interfaces/telegraf-wizard-context.interface';
import { Markup } from 'telegraf';
import { Update } from 'telegraf/typings/core/types/typegram';

@Wizard(BotWizards.Test)
@UseFilters(TelegrafExceptionFilter)
@UseInterceptors(LoggingInterceptor)
export class TestWizard {
  constructor() {}

  @WizardStep(1)
  protected async step1(@Ctx() ctx: TelegrafWizardContext) {
    await ctx.reply(
      'Step 1',
      Markup.inlineKeyboard([
        Markup.button.callback('Вперед', 'YES'),
        Markup.button.callback('Выход', 'EXIT')
      ])
    );
    ctx.wizard.next();
  }

  @WizardStep(2)
  @Action(['YES'])
  protected async step2(
    @Ctx() ctx: TelegrafWizardContext & { update: Update.CallbackQueryUpdate }
  ) {
    // await ctx.deleteMessage();
    await ctx.reply(
      'Step 2',
      Markup.inlineKeyboard([
        Markup.button.callback('Вперед', 'YES'),
        Markup.button.callback('Назад', 'BACK'),
        Markup.button.callback('Выход', 'EXIT')
      ])
    );
    ctx.wizard.next();
  }

  @WizardStep(3)
  @Action(['YES', 'BACK'])
  protected async step3(
    @Ctx() ctx: TelegrafWizardContext & { update: Update.CallbackQueryUpdate }
  ) {
    const reply = ctx.update.callback_query['data'];
    if (reply === 'YES') {
      // await ctx.deleteMessage();
      await ctx.reply(
        'Step 3',
        Markup.inlineKeyboard([
          Markup.button.callback('Вперед', 'YES'),
          Markup.button.callback('Назад', 'BACK'),
          Markup.button.callback('Выход', 'EXIT')
        ])
      );
      ctx.wizard.next();
    }
    if (reply === 'BACK') {
      // await ctx.deleteMessage();
      ctx.wizard.back();
      // @ts-ignore
      return ctx.wizard.steps[ctx.wizard.cursor](ctx);
    }
  }

  @WizardStep(4)
  @Action(['YES', 'BACK'])
  protected async step4(@Ctx() ctx: TelegrafWizardContext) {
    // await ctx.deleteMessage();
    await ctx.reply('Step 4, the last');
    ctx.scene.leave();
  }

  // @Action('BACK')
  // protected async back(@Ctx() ctx) {
  //   await ctx.deleteMessage();
  //   ctx.wizard.back();
  //   ctx.wizard.steps[ctx.wizard.cursor](ctx);
  // }

  @Action('EXIT')
  protected async exit(@Ctx() ctx) {
    ctx.deleteMessage();
    await ctx.reply(
      `Existing`,
      Markup.removeKeyboard()
    );
    ctx.scene.leave();
  }
}

Error: following part

ctx.wizard.steps[ctx.wizard.cursor](ctx)

gives error:

[Nest] 13954  - 03/10/2023, 9:01:01 PM   ERROR [TelegrafExceptionFilter] Handler is undefined
Error: Handler is undefined
    at Function.unwrap (/home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:445:19)
    at execute (/home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:473:42)
    at /home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:475:27
    at /home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:341:20
    at /home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:167:111
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async execute (/home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:474:17)
    at async /home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:475:21
    at async execute (/home/ubuntu/repos/telegram/node_modules/telegraf/lib/composer.js:474:17)

This hack ctx.wizard.steps[ctx.wizard.cursor](ctx) is used in order to previous step to execute and show reply and buttons, as ctx.wizard.back() is not enough.

Versions:

"nestjs-telegraf": "^2.6.3",
"@nestjs/core": "^9.0.0",
"telegraf": "^4.11.2"
@evilsprut evilsprut self-assigned this Jul 24, 2023
@evilsprut evilsprut added the bug Something isn't working label Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants