I am having trouble implementing the step system in my WizardScene. I've been trying for 2 days now.
System of operation:
The bot edits a post each time it receives the data it needs from a user.
Required:
Ability to go back one step from any step other than the first.
Attempts:
I tried to implement this via ctx.wizard.stepsctx.wizard.cursor - 2. The code was actually going backwards on the question. BUT! After the action in the previous step, the scene worked one step forward from where I returned from. That is:
- Question: Send text. user: text (actually in 2 question/step).
- Question: Send another text user: back (actually in 1 question/step).
- Question: Send text. user: smth new (actually in 3 question/step).
My Code:
const { Scenes, Markup } = require("telegraf");
module.exports = () => {
const scene = new Scenes.WizardScene(
"newSceneWithStapBacks",
async (ctx) => {
//First step. User joined by inline callback button.
await ctx.editMessageText(`Send text`, {
...Markup.inlineKeyboard([
[Markup.button.callback(`Cancel`, `cancel`)], //Cancel button because its a first step.
]),
parse_mode: "HTML",
});
//Creating object for data storage
ctx.wizard.state.data = {};
//Saving ctx with message to editing (message by bot)
ctx.wizard.state.data.firstCtx = ctx;
return ctx.wizard.next();
},
async (userCtx) => {
//Validating ctx. If user doesnt sent a text message - deleting his message.
if (!userCtx?.message?.text) return userCtx.deleteMessage();
const smthData = userCtx.message.text;
//Deleting user's message. We will add it to the bot message by editing it. (ctx.wizard.state.data.firstCtx)
userCtx.deleteMessage();
//Editing message from bot (adding data from user)
await ctx.wizard.state.data.firstCtx.editMessageText(
`1. Your data: ${smthData}
Send something else.`,
{
...Markup.inlineKeyboard([
[
Markup.button.callback(`Back`, `back`),
Markup.button.callback(`Cancel`, `cancel`),
],
]), //Back to te previous step button.
parse_mode: "HTML",
}
);
//Saved data to session storage.
ctx.wizard.state.data.smthData = smthData;
return ctx.wizard.next();
},
async (ctx) => {
//Validating ctx. If user doesnt sent a text message - deleting his message.
if (!userCtx?.message?.text) return userCtx.deleteMessage();
const smthData2 = userCtx.message.text;
//Deleting user's message. We will add it to the bot message by editing it. (ctx.wizard.state.data.firstCtx)
userCtx.deleteMessage();
//Editing message from bot (adding data from user)
await ctx.wizard.state.data.firstCtx.editMessageText(
`1. Your data: ${ctx.wizard.state.data.smthData}
2. Your data: ${smthData2}
Send something else.`,
{
...Markup.inlineKeyboard([
[
Markup.button.callback(`Back`, `back`),
Markup.button.callback(`Cancel`, `cancel`),
],
]), //Back to te previous step button.
parse_mode: "HTML",
}
);
//Saved data 2 to session storage.
ctx.wizard.state.data.smthData2 = smthData2;
return ctx.wizard.next();
},
async (ctx) => {
//Validating ctx. If user doesnt sent a text message - deleting his message.
if (!userCtx?.message?.text) return userCtx.deleteMessage();
const smthData3 = userCtx.message.text;
//Deleting user's message. We will add it to the bot message by editing it. (ctx.wizard.state.data.firstCtx)
userCtx.deleteMessage();
//Editing message from bot (adding data from user)
await ctx.wizard.state.data.firstCtx.editMessageText(
`1. Your data: ${ctx.wizard.state.data.smthData}
2. Your data: ${ctx.wizard.state.data.smthData2}
3. Your data: ${smthData3}
End.`,
{
parse_mode: "HTML",
}
);
//Saved data 2 to session storage.
ctx.wizard.state.data.smthData2 = smthData2;
return ctx.wizard.leave();
}
);
//Step back
scene.action(/^(?:back)$/i, async (ctx) => {
//Working with previous step system...
});
//Leaving from scene
scene.action(/^(?:cancel)$/i, async (ctx) => {
ctx.reply(`Leaved!`);
return ctx.scene.leave();
});
return scene;
};
Help me, please!