telegraf / telegraf-session-redis

Redis session middleware for Telegraf
MIT License
50 stars 25 forks source link

Sessions with async functions does not works #27

Open nightSamurai opened 3 years ago

nightSamurai commented 3 years ago

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!

syazsolo commented 1 year ago

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