Sunday, 16 June 2019

Is possible await a WizardScene?

I developed a bot for Telegram using TelegrafJS and I'm also using the library TelegrafInlineMenu for handle the menu layout.

I've registered a WizardScene for validate the user input, here the code:

const WizardScene = require('telegraf/scenes/wizard');
const Stage = require('telegraf/stage');
const projectController = require('../controllers/project.controller');

module.exports = function (bot) {

const projectNameWizard = new WizardScene('projectName-wizard',
(ctx) => {

ctx.reply(ctx.i18n.t('projectName'));
ctx.scene.session.project = {};

//Project updating
ctx.scene.session.project.oldName = ctx.match[1];

//Keep customer reference
ctx.scene.session.project.customerId = ctx.update.callback_query.from.id;
return ctx.wizard.next();
},
async (ctx) => {

//Get the name of the projec
let oldPrName = ctx.scene.session.project.oldName;
let newPrName = ctx.message.text;

//Convalidate project
if (ctx.message.text.length < 1 || ctx.message.text.length > 12) {
    return ctx.reply(ctx.i18n.t('invalidProjectName', { "characters": 12 }));
}
ctx.scene.session.project.name = ctx.message.text;

//Update the project fields
await projectController.SaveProject(ctx.scene.session.project, ctx.update.message.from);
let pr = await projectController.DisplayProjectDetails(ctx, newPrName);

ctx.scene.leave();

//Project was updated
if (oldPrName !== undefined) {

ctx.reply(ctx.i18n.t('projectUpdated'));
}

return pr;
}
);

const stage = new Stage([projectNameWizard]);
stage.command('cancel', (ctx) => {  

   ctx.reply(ctx.i18n.t('operationCanc'));
    return ctx.scene.leave();
});
bot.use(stage.middleware())
}

To start the wizard within the menu, I use the following code:

projectEdit.simpleButton(ctx => 
   ctx.i18n.t('editName'), 'EDIT_PR_NAME', {
       doFunc: async ctx => {
    await 
ctx.scene.enter('projectName-wizard');
    console.log("hello world");
},
});

I need to wait the completion of the WizardScene and then execute the console.log, the problem is that the await in front of ctx.scene.enter does not await anything, and the hello world message is printed instantly. There is something that allow me to await the result of the Wizard? If not, how can I handle this situation?

Kind regards



from Is possible await a WizardScene?

No comments:

Post a Comment