function session(options = {}) {
    var _a, _b;
    const getSessionKey = (_a = options.getSessionKey) !== null && _a !== void 0 ? _a : defaultGetSessionKey;
    const storage = (_b = options.storage) !== null && _b !== void 0 ? _b : (debug("Storing session data in memory, all data will be lost when the bot restarts."),
        new MemorySessionStorage());
    return async (ctx, next) => {
        var _a, _b;
        const key = await getSessionKey(ctx);
        let value = key === undefined
            ? undefined
            : (_a = (await storage.read(key))) !== null && _a !== void 0 ? _a : (_b = options.initial) === null || _b === void 0 ? void 0 : _b.call(options);
        Object.defineProperty(ctx, "session", {
            get() {
                if (key === undefined) {
                    throw new Error("Cannot access session data because the session key was undefined!");
                }
                return value;
            },
            set(v) {
                if (key === undefined) {
                    throw new Error("Cannot assign session data because the session key was undefined!");
                }
                value = v;
            },
        });
        await next(); // no catch: do not write back if middleware throws
        if (key !== undefined) {
            if (value == null)
                await storage.delete(key);
            else
                await storage.write(key, value);
        }
    };
}
/**
 * Session middleware provides a persistent data storage for your bot. You can
 * use it to let your bot remember any data you want, for example the messages
 * it sent or received in the past. This is done by attaching _session data_ to
 * every chat. The stored data is then provided on the context object under
 * `ctx.session`.
 *
 * > **What is a session?** Simply put, the session of a chat is a little
 * > persistent storage that is attached to it. As an example, your bot can send
 * > a message to a chat and store the ID of that message in the corresponding
 * > session. The next time your bot receives an update from that chat, the
 * > session will still contain that ID.
 * >
 * > Session data can be stored in a database, in a file, or simply in memory.
 * > grammY only supports memory sessions out of the box, but you can use
 * > third-party session middleware to connect to other storage solutions. Note
 * > that memory sessions will be lost when you stop your bot and the process
 * > exits, so they are usually not useful in production.
 *
 * Whenever your bot receives an update, the first thing the session middleware
 * will do is to load the correct session from your storage solution. This
 * object is then provided on `ctx.session` while your other middleware is
 * running. As soon as your bot is done handling the update, the middleware
 * takes over again and writes back the session object to your storage. This
 * allows you to modify the session object arbitrarily in your middleware, and
 * to stop worrying about the database.
 *
 * ```ts
 * bot.use(session())
 *
 * bot.on('message', ctx => {
 *   // The session object is persisted across updates!
 *   const session = ctx.session
 * })
 * ```
 *
 * It is recommended to make use of the `inital` option in the configuration
 * object, which correctly initializes session objects for new chats.
 *
 * You can delete the session data by setting `ctx.session` to `null` or
 * `undefined`.
 *
 * Check out the [documentation](https://grammy.dev/plugins/session.html) on the
 * website to know more about how sessions work in grammY.
 *
 * @param options Optional configuration to pass to the session middleware
 */
Session middleware provides a persistent data storage for your bot. You can
use it to let your bot remember any data you want, for example the messages
it sent or received in the past. This is done by attaching _session data_ to
every chat. The stored data is then provided on the context object under
`ctx.session`.

> **What is a session?** Simply put, the session of a chat is a little
> persistent storage that is attached to it. As an example, your bot can send
> a message to a chat and store the ID of that message in the corresponding
> session. The next time your bot receives an update from that chat, the
> session will still contain that ID.
>
> Session data can be stored in a database, in a file, or simply in memory.
> grammY only supports memory sessions out of the box, but you can use
> third-party session middleware to connect to other storage solutions. Note
> that memory sessions will be lost when you stop your bot and the process
> exits, so they are usually not useful in production.

Whenever your bot receives an update, the first thing the session middleware
will do is to load the correct session from your storage solution. This
object is then provided on `ctx.session` while your other middleware is
running. As soon as your bot is done handling the update, the middleware
takes over again and writes back the session object to your storage. This
allows you to modify the session object arbitrarily in your middleware, and
to stop worrying about the database.



bot.on('message', ctx => {
The session object is persisted across updates.
const session = ctx.session
})
```

It is recommended to make use of the `inital` option in the configuration
object, which correctly initializes session objects for new chats.



Check out the [documentation] on the
website to know more about how sessions work in grammY.

@param options Optional configuration to pass to the session middleware
Session middleware provides a persistent data storage for your bot . You can use it to let your bot remember any data you want for example the messages it sent or received in the past . This is done by attaching _session data_ to every chat . The stored data is then provided on the context object under ` ctx . session ` . > ** What is a session? ** Simply put the session of a chat is a little > persistent storage that is attached to it . As an example your bot can send > a message to a chat and store the ID of that message in the corresponding > session . The next time your bot receives an update from that chat the > session will still contain that ID . > > Session data can be stored in a database in a file or simply in memory . > grammY only supports memory sessions out of the box but you can use > third - party session middleware to connect to other storage solutions . Note > that memory sessions will be lost when you stop your bot and the process > exits so they are usually not useful in production . Whenever your bot receives an update the first thing the session middleware will do is to load the correct session from your storage solution . This object is then provided on ` ctx . session ` while your other middleware is running . As soon as your bot is done handling the update the middleware takes over again and writes back the session object to your storage . This allows you to modify the session object arbitrarily in your middleware and to stop worrying about the database . bot . on ( ' message ' ctx = > { The session object is persisted across updates . const session = ctx . session } ) ` ` ` It is recommended to make use of the ` inital ` option in the configuration object which correctly initializes session objects for new chats . Check out the [ documentation ] on the website to know more about how sessions work in grammY . @param options Optional configuration to pass to the session middleware
function session(options = {}) {
    var _a, _b;
    const getSessionKey = (_a = options.getSessionKey) !== null && _a !== void 0 ? _a : defaultGetSessionKey;
    const storage = (_b = options.storage) !== null && _b !== void 0 ? _b : (debug("Storing session data in memory, all data will be lost when the bot restarts."),
        new MemorySessionStorage());
    return async (ctx, next) => {
        var _a, _b;
        const key = await getSessionKey(ctx);
        let value = key === undefined
            ? undefined
            : (_a = (await storage.read(key))) !== null && _a !== void 0 ? _a : (_b = options.initial) === null || _b === void 0 ? void 0 : _b.call(options);
        Object.defineProperty(ctx, "session", {
            get() {
                if (key === undefined) {
                    throw new Error("Cannot access session data because the session key was undefined!");
                }
                return value;
            },
            set(v) {
                if (key === undefined) {
                    throw new Error("Cannot assign session data because the session key was undefined!");
                }
                value = v;
            },
        });
        await next(); 
        if (key !== undefined) {
            if (value == null)
                await storage.delete(key);
            else
                await storage.write(key, value);
        }
    };
}
function session ( options = { } ) { var _a , _b ; const getSessionKey = ( _a = options . getSessionKey ) !== null && _a !== void 0 ? _a : defaultGetSessionKey ; const storage = ( _b = options . storage ) !== null && _b !== void 0 ? _b : ( debug ( "Storing session data in memory, all data will be lost when the bot restarts." ) , new MemorySessionStorage ( ) ) ; return async ( ctx , next ) => { var _a , _b ; const key = await getSessionKey ( ctx ) ; let value = key === undefined ? undefined : ( _a = ( await storage . read ( key ) ) ) !== null && _a !== void 0 ? _a : ( _b = options . initial ) === null || _b === void 0 ? void 0 : _b . call ( options ) ; Object . defineProperty ( ctx , "session" , { get ( ) { if ( key === undefined ) { throw new Error ( "Cannot access session data because the session key was undefined!" ) ; } return value ; } , set ( v ) { if ( key === undefined ) { throw new Error ( "Cannot assign session data because the session key was undefined!" ) ; } value = v ; } , } ) ; await next ( ) ; if ( key !== undefined ) { if ( value == null ) await storage . delete ( key ) ; else await storage . write ( key , value ) ; } } ; }
Session middleware provides a persistent data storage for your bot.
Session middleware provides a persistent data storage for your bot .
// no catch: do not write back if middleware throws