Callback data builder, inspired by aiogram.

This can be used to simplify management of different callbacks.

Learn more in the docs

Type Parameters

  • T extends string

Constructors

Properties

Methods

Constructors

  • Type Parameters

    • T extends string

    Parameters

    • prefix: string

      Prefix for the data. Use something unique across your bot.

    • ...fields: T[]

      Field names in the order they will be serialized.

    Returns CallbackDataBuilder<T>

Properties

prefix: string

Prefix for the data. Use something unique across your bot.

sep: string = ':'

Methods

  • Build a callback data string

    Parameters

    • obj: Record<T, string>

      Object containing the data

    Returns string

  • Create a filter for this callback data.

    You can either pass an object with field names as keys and values as strings or regexes, which will be compiled to a RegExp, or a function that will be called with the parsed data. Note that the strings will be passed to RegExp directly, so you may want to escape them.

    When using a function, you can either return a boolean, or an object with field names as keys and values as strings or regexes. In the latter case, the resulting object will be matched against the parsed data the same way as if you passed it directly.

    Type Parameters

    Parameters

    • params:
          | Partial<Record<T, MaybeArray<string | RegExp>>>
          | (
              upd: Update,
              parsed: Record<T, string>,
          ) => MaybePromise<
              boolean
              | Partial<Record<T, MaybeArray<string | RegExp>>>,
          > = {}

    Returns filters.UpdateFilter<Update, { match: Record<T, string> }>

  • Parse callback data to object

    Parameters

    • data: string

      Callback data as string

    • Optionalsafe: false

      If true, will return null instead of throwing on invalid data

    Returns Record<T, string>

  • Parse callback data to object

    Parameters

    • data: string

      Callback data as string

    • safe: true

      If true, will return null instead of throwing on invalid data

    Returns null | Record<T, string>