1
1
1hermn2022-01-08 16:23:50
JavaScript
1hermn, 2022-01-08 16:23:50

How to organize an exit button in a tg bot?

At the moment I have:
When loading scenes, I put:

scene.on("message", async (ctx, next) => await utils.keyboard.exitHandler(ctx, next))
scene.on("message", async (ctx, next) => await utils.keyboard.backHandler(ctx, next))


exitHandler:
spoiler

module.exports = async (ctx, next) => {
    if("text" in ctx.message){
        if(ctx.message.text === ctx.i18n.t("exitButton")){
            if(++ctx.session.count % 2 === 0){
                ctx.session.count = 0;
                return next()
            }
            //get state
            if(ctx.session.scenesHistory.length - 2 >= 0){
                let last = ctx.session.scenesHistory[ctx.session.scenesHistory.length - 2]
                ctx.session.scenesHistory = ctx.session.scenesHistory.splice(ctx.session.scenesHistory.length - 2, 1)
                return ctx.scene.enter(last)
            }else {
                return ctx.scene.enter("start")
            }
        }
    }
    return next()
}


backHandler
spoiler
module.exports = async (ctx, next) => {

    if("text" in ctx.message){
        if(ctx.message.text === ctx.i18n.t("backButton")){
            //get state
            if(ctx.session.__scenes.cursor !== 1){
                ctx.wizard.selectStep(ctx.session.__scenes.cursor - 2)
            }else {
                ctx.reply("Не могу перейти на предыдущий этап. Используйте кнопку " + ctx.i18n.t("exitButton"))
            }
            return next()
        }
    }
    return next()
}


The problem is this: sometimes, the bot starts to "exit" endlessly and goes to bed with the error

Promise timed out after 90000 milliseconds

Tried to stop uncontrolled executions, but not successfully.
Question - what could be the reason for this and how to solve it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question