Bale API¶
The Bale adapter provides the typed asynchronous client and Bale-specific
request/response models. The generated reference below recursively discovers
the restructured types and methods packages.
Client¶
Bale platform public API.
Types and methods¶
Bale API types, one file per type. |
|
Bale client methods, one module per method. |
Supporting modules¶
Bale application-level API errors.
Every Bale response is a JSON envelope with an ok flag. On ok: false, Bale still returns a plain HTTP 200 – the failure is encoded in the JSON body (description + error_code + optional parameters), not in the HTTP status line. That makes it an application-level failure, distinct from peyk.transport’s transport-level errors (HTTPStatusError, NetworkError, TimeoutError_, RateLimitedError), which are raised from transport-level facts only (connection failures, timeouts, non-2xx status codes).
BaleAPIError subclasses peyk.transport.errors.TransportError so it can still be caught alongside transport errors by callers that want a single “something about this Bale call failed” catch-all – but it is deliberately not included in peyk.transport.retry.DEFAULT_RETRYABLE, and BaleClient never passes it through run_with_retry in the first place (see client.py): a run_with_retry-wrapped call to Session.request() already returns successfully (HTTP 200, valid JSON) before BaleClient ever looks at the ok flag, so the retry layer never even sees a BaleAPIError to (correctly) decline to retry.
- exception peyk.platforms.bale.errors.BaleAPIError(message: str, *, error_code: int, description: str, parameters: object | None = None)[source]¶
Bases:
TransportErrorRaised when a Bale API response has ok: false.
- Variables:
error_code – Bale’s error_code integer from the response body.
description – Bale’s human-readable description string.
parameters – The optional parameters object Bale sometimes includes alongside an error (e.g. retry hints for specific error codes). None when absent.
Mini App helpers and ble.ir links¶
Server-side helpers for Bale Mini Apps and ble.ir deep links.
A Mini App sends Bale.WebApp.initData (a URL-encoded query string) to your
bot’s backend. Never trust it before verifying the signature: use
verify_init_data() (raises) or validate_init_data() (returns bool).
Signature algorithm (Bale docs, identical to the Telegram scheme):
data_check_string = "\n".join(f"{k}={v}" for k, v in sorted(fields) if k != "hash")
secret_key = HMAC_SHA256(key="WebAppData", message=bot_token)
hash = hex(HMAC_SHA256(key=secret_key, message=data_check_string))
Example
from peyk.platforms.bale.miniapp import verify_init_data
data = verify_init_data(request_init_data, BOT_TOKEN, max_age=3600)
user_id = data.user["id"]
- exception peyk.platforms.bale.miniapp.InvalidInitDataError[source]¶
Bases:
ValueErrorinitDatais malformed, unsigned, forged or expired.
- class peyk.platforms.bale.miniapp.MiniAppInitData(fields: ~typing.Dict[str, str] = <factory>, user: ~typing.Dict[str, object] | None = None, receiver: ~typing.Dict[str, object] | None = None, chat: ~typing.Dict[str, object] | None = None, auth_date: int | None = None, query_id: str | None = None, start_param: str | None = None, chat_type: str | None = None, chat_instance: str | None = None, hash: str | None = None)[source]¶
Bases:
objectParsed (and, when produced by
verify_init_data(), verified)initData.- Variables:
fields (Dict[str, str]) – Every decoded query field, including
hash.user (Dict[str, object] | None) – Decoded
userJSON object, if present.receiver (Dict[str, object] | None) – Decoded
receiverJSON object, if present.chat (Dict[str, object] | None) – Decoded
chatJSON object, if present.auth_date (int | None) – Unix time the data was signed at.
query_id (str | None) –
query_idfield, if present.start_param (str | None) – The
startappvalue the Mini App was opened with.chat_instance (chat_type /) – Launch-context fields, if present.
hash (str | None) – The signature that came with the data.
- fields: Dict[str, str]¶
- user: Dict[str, object] | None = None¶
- receiver: Dict[str, object] | None = None¶
- chat: Dict[str, object] | None = None¶
- auth_date: int | None = None¶
- query_id: str | None = None¶
- start_param: str | None = None¶
- chat_type: str | None = None¶
- chat_instance: str | None = None¶
- hash: str | None = None¶
- peyk.platforms.bale.miniapp.parse_init_data(init_data: str) MiniAppInitData[source]¶
Parse
initDatawithout checking the signature.
- peyk.platforms.bale.miniapp.sign_init_data(fields: Mapping[str, str], bot_token: str) str[source]¶
Build a signed
initDatastring (for tests and local tooling).
- peyk.platforms.bale.miniapp.validate_init_data(init_data: str, bot_token: str, *, max_age: int | None = None) bool[source]¶
Return
Truewhenverify_init_data()accepts the data.
- peyk.platforms.bale.miniapp.verify_init_data(init_data: str, bot_token: str, *, max_age: int | None = None, now: float | None = None) MiniAppInitData[source]¶
Verify the signature (and optionally the age) and return the parsed data.
- Parameters:
init_data – The raw
Bale.WebApp.initDatastring sent by the Mini App.bot_token – Token of the bot that owns the Mini App.
max_age – Reject data whose
auth_dateis older than this many seconds.now – Override the current time (tests).
- Raises:
InvalidInitDataError – Malformed, forged, missing
hashor expired.
- peyk.platforms.bale.miniapp.start_link(bot_username: str, payload: str | None = None) str[source]¶
https://ble.ir/<bot>?start=<payload>: the user’s tap sends/start <payload>.
- peyk.platforms.bale.miniapp.mini_app_link(bot_username: str, start_param: str | None = None, *, mode: str | None = None) str[source]¶
Link that opens the bot’s main Mini App directly (
?startapp).The user does not need to have started the bot.
start_paramarrives in the Mini App as thestart_paramfield ofinitData;modeis the display mode (for example"compact").
https://ble.ir/share?url=...&text=...: opens the user’s chat list to share.