Utilities API

class peyk.utils.I18n(path: str | Path, default_locale: str = 'fa', domain: str = 'messages')[source]

Bases: object

Load GNU gettext catalogs and provide aiogram-style translation helpers.

gettext(message: str, locale: str | None = None) str[source]

Performs the gettext operation for the utility client.

Parameters:
  • message – Value used by this operation.

  • locale – Locale used for translation lookup.

Returns:

Result produced by the utility operation.

lazy_gettext(message: str, locale: str | None = None) LazyText[source]

Performs the lazy gettext operation for the utility client.

Parameters:
  • message – Value used by this operation.

  • locale – Locale used for translation lookup.

Returns:

Result produced by the utility operation.

ngettext(singular: str, plural: str, n: int, locale: str | None = None) str[source]

Performs the ngettext operation for the utility client.

Parameters:
  • singular – Value used by this operation.

  • plural – Value used by this operation.

  • n – Value used by this operation.

  • locale – Locale used for translation lookup.

Returns:

Result produced by the utility operation.

set_locale(locale: str) None[source]

Updates locale through the utility API.

Parameters:

locale – Locale used for translation lookup.

class peyk.utils.I18nMiddleware(i18n: I18n, get_locale: Callable[[object, Mapping[str, object]], str] | None = None)[source]

Bases: object

Inject i18n, locale and translation callables into dispatcher DI.

class peyk.utils.LazyText(resolver: Callable[[], str])[source]

Bases: object

LazyText provides the utility API surface used by peyk.

class peyk.utils.MediaGroupBuilder(items: list[~peyk.utils.media_group.T] = <factory>)[source]

Bases: Generic[T]

Collect media items for a platform-supported media group.

add(media: T) MediaGroupBuilder[T][source]

Performs the add operation for the utility client.

Parameters:

media – Value used by this operation.

Returns:

Result produced by the utility operation.

build() list[T][source]

Performs the build operation for the utility client.

Returns:

Result produced by the utility operation.

items: list[T]
peyk.utils.check_webapp_signature(init_data: str, bot_token: str) bool[source]

Validate Telegram Web App init data according to the documented HMAC scheme.

peyk.utils.safe_parse_webapp_init_data(init_data: str, bot_token: str) dict[str, str][source]

Validate and parse Telegram Web App init data.

peyk.utils.encode_payload(payload: str | bytes) str[source]

Encode UTF-8 payload using unpadded URL-safe base64.

peyk.utils.decode_payload(payload: str) str[source]

Decode an unpadded URL-safe base64 payload as UTF-8.

Create the confirmed Telegram Bot API start deep link.