IlyaSemenov / grammy-scenes

Nested named scenes for grammY
MIT License
27 stars 0 forks source link

Support for lazy sessions #7

Closed zkulbeda closed 1 year ago

zkulbeda commented 2 years ago

Bot loses the session with an entered scene when using lazy sessions. Official documentation says, that developers should offer two ways of using session storage. Example:

import { Bot, Context, lazySession, LazySessionFlavor } from "grammy"
import { SceneSessionFlavor, ScenesFlavor } from "grammy-scenes"
import { scenes } from "./scenes"

type SessionData = SceneSessionFlavor & {}
export type BotContext = Context & LazySessionFlavor<SessionData> & ScenesFlavor

const bot = new Bot<BotContext>(process.env.BOT_TOKEN)
bot.use(
  lazySession({
    initial: () => ({}),
  })
)
bot.use(scenes.manager())
bot.command("start", async (ctx) => {
  await ctx.reply(`Welcome here.`)
  await ctx.scenes.enter("main")
})
bot.use(scenes)
bot.on("message", (ctx)=>ctx.reply("outside the scene"))
bot.start()

After sending /start and then sending some message, bot replies "outside the scene" message.

IlyaSemenov commented 2 years ago

There's not much win in using lazy session in scenes-centric bot because it must read the session on every request anyway. (As it needs to figure out the current scene/step and call the respective middleware).

But I agree that this as a missing feature/reasonable improve.

IlyaSemenov commented 1 year ago

Published in v10.1.0.