telegraf-session-redis icon indicating copy to clipboard operation
telegraf-session-redis copied to clipboard

Sessions with async functions does not works

Open nightSamurai opened this issue 4 years ago • 1 comments

Hi everyone!

I uses Telegraf + Redis-session + Stage in my bot. But session doesn't works with Promise

This very similar like this issues ( 1, 2, 3, 4 ), but nothing was helpful for me

Into my scene on "enter" event i try to set something like this: ctx.scene.state.{key} = {value}. And this work fine only if use it without promise or async function. For example:

export const scene = new BaseScene(MY_SCENE)

// My enter scene logic
scene.enter(async (ctx) => {
    // this works
    ctx.scene.state.number = 1 
    
    // this set value into a state variable, but NOT save it in sessions
    ctx.scene.state.promise = await (
        new Promise<number>((resolve, reject) => {
            setTimeout(() => {
                resolve(1)
            }, 1000)
        })
    )

    console.log( ctx.scene.state )
    // output { number: 1, promise: 1 }


    // another code here...
})

// on any of event type will be the same behavior
scene.on('message', (ctx) => {
    console.log(
        ctx.scene.state
    )
    // output { number: 1 }
    // expected to be { number: 1, promise: 1 }
})

So, maybe exists the way, to trigger sync process? Or some utility function for force update the session?

I will be glad of any help, thanks!

nightSamurai avatar Nov 20 '20 13:11 nightSamurai

I know its been a long time, but I had similar problem, and the solution is to await ctx.scene.enter(MY_SCENE) instead of ctx.scene.enter(MY_SCENE) without await

hope this helps

syazsolo avatar Sep 22 '23 06:09 syazsolo