Telegraf.js: leave WizardScene with a button

2.6k views Asked by At

I would put a button after a message in a WizardScene with a "Cancel" Button. But i retrieve some error:

This is my wizard scene:

const superWizard = new WizardScene('super-wizard',
      async ctx => {
        ctx.wizard.state.data = {};
        ctx.telegram.sendMessage(ctx.from.id, "Insert name", {
          parse_mode: 'MarkdownV2',
          reply_markup: cancelOrder()
        })
        return ctx.wizard.next();
      },
      ctx => {
        
        ctx.wizard.state.data.name = ctx.message.text;
        ctx.reply("here is your name: "+ctx.wizard.state.data.name);
        return ctx.scene.leave();
      }
);
const stage = new Stage([superWizard]);

bot.use(session());
bot.use(stage.middleware());

Here is my cancel order function:

function cancelOrder() {
  const annullaBtn = Markup.inlineKeyboard([
    Markup.callbackButton('CANCEL', `cancelOrder_btn`),
  ])
  return annullaBtn;
}

and the button action:

bot.action("cancelOrder_btn", (ctx) => {
    ctx.replyWithMarkdown(`Ordine *ANNULLATO* correttamente`)
    return ctx.scene.leave('super-wizard');
  });

The program writes correctly the text, and puts the button. But if i press "CANCEL" it gives error at:

ctx.wizard.state.data.name = ctx.message.text;

as "text is undefined" because i press cancel and i didn't write anything.

So how can i leave the scene without going forward, but if i write a text it goes forward in the wizardScene?

Thank you

1

There are 1 answers

0
Michael Perelman On

Replace

ctx.message.text;

with

ctx.update.callback_query.data;

as there is no message returned by callback button