telebot-ts API Documentation - v1.0.0
    Preparing search index...

    Function session

    • Creates a session management middleware.

      Automatically loads user or chat session state before handlers execute, exposes context.session, and persists modified state after execution finishes.

      Type Parameters

      • S = Record<string, unknown>

        Structure of the session state.

      Parameters

      • options: SessionOptions<S> = {}

        Configuration options for initial state and storage backend.

      Returns MiddlewareFn

      A MiddlewareFn ready to be used with app.use().

      interface MySession {
      count: number;
      }

      app.use(session<MySession>({
      initial: () => ({ count: 0 }),
      }));

      app.addHandler(new CommandHandler("counter", async (update, context) => {
      const ctx = context as CallbackContext & { session: MySession };
      ctx.session.count++;
      await context.bot.sendMessage({
      chat_id: update.effective_chat!.id,
      text: `You have called this command ${session.count} times!`,
      });
      }));