Closed koddr closed 2 weeks ago
initial
only needs to provide data that should be used for new chats, i.e. those without existing session data.
The logic inside the session plugin can be simplified to this:
const key = getSessionKey(ctx)
const sessionData = await storage.read(key) ?? initial()
await next() // call downstream handlers
await storage.write(key, sessionData)
So I think it already works exactly like you expect? Or perhaps I'm misunderstanding what you're trying to do, then I need you to elaborate.
@KnorpelSenf thanks for reply.
Oh, wait… It looks like there was some mistake on my side.
When connecting to Redis, an error occurred that I didn't catch, so I always returned null to the session request by key.
In any case, it would be cool to add this explanation of the work to the description of the sessions with Redis in the grammY documentation so that the process is as transparent as possible 😊
@koddr do you mean that the redis session adapter returns null
on error?
@KnorpelSenf I have conducted several clean tests and have not encountered a repeat of this bug. So it wasn't about him, and the adapter for Redis is working correctly.
Awesome!
It is not very clear how to work with data from external storage to initialize the initial data of the user's session.
For example, when working with Redis adapter and
lazySession
:What do I need to do so that when creating middleware with session settings, I can pick up the current data? Now, every time the bot is rebooted, the session is reset to those specified in
initial
block inlazySession
.Since the session key in Redis is equal to the user ID in Telegram (according to the snippet above), in order to programmatically insert all data from Redis into the
ctx.session
, I need to make a separate function, which should be at the beginning of eachbot.*(...)
, etc.?How would I improve this?
It would be convenient if the session initialization method could retrieve data from the remote storage itself. And if there is no data (or the storage is unavailable), then take the specified fallback values.
Or let the developer configure the function himself, which will take data from Redis and put it in the
ctx.session
of the current bot user:I think, such an improvement would be useful for everyone (especially for beginners) and would make the bot code much easier.