English فارسی

Bot API

Pass a bot token and Client switches to https://tapi.bale.ai — Telegram-shaped JSON, no WebSocket. The BotApi class is also usable on its own.

Automatic detection

from balexyz import Client

bot = Client("123456789:AAH4x-veryLongTokenValueHere")
await bot.start()

print(bot.is_bot)          # True
print(bot.is_user)         # False

The token shape is \d+:[A-Za-z0-9_-]{20,}. Anything else is treated as a user credential. You can also be explicit:

client = Client(bot_token="123456789:AAH…")
client = Client("ignored", bot_token="123456789:AAH…")   # bot wins

Standalone BotApi

from balexyz import BotApi

bot = BotApi("123456789:AAH…")
me = await bot.get_me()

await bot.call("sendMessage", chat_id="987654321|1", text="hello")
await bot.close()

Methods

MethodNotes
get_me()Bot identity
send_message(chat_id, text, **params)Extra params pass through verbatim
edit_message_text(chat_id, message_id, text, **params)
delete_message(chat_id, message_id)
send_photo / send_video / send_document / send_voice / send_audioAccepts a file id, URL, or upload
send_location(chat_id, latitude, longitude)
send_chat_action(chat_id, action)typing, uploading_photo…
get_chat(chat_id)
get_updates(offset=None, timeout=None, limit=None)Long polling
iter_updates()Async generator wrapper
set_webhook / delete_webhook / get_webhook_info
answer_callback_query
set_my_commandsCommand menu
call(method, **params)Anything not wrapped

Long polling

import asyncio

async def main():
    bot = BotApi("123456789:AAH…")
    offset = None

    while True:
        updates = await bot.get_updates(offset=offset, timeout=30)
        for update in updates.get("result", []):
            offset = update["update_id"] + 1
            message = update.get("message") or {}
            if message.get("text") == "/start":
                await bot.send_message(message["chat"]["id"], "welcome")

asyncio.run(main())

Webhook

await bot.set_webhook(url="https://example.com/bale", secret_token="s3cret")
info = await bot.get_webhook_info()
print(info["url"], info["pending_update_count"])
await bot.delete_webhook()

Errors

from balexyz import BotApiError

try:
    await bot.send_message("bad", "x")
except BotApiError as exc:
    print(exc.code, exc.description, exc.method)   # 400 … "sendMessage"

What is verified here

Endpoint and error path are live-tested.

Requests reach tapi.bale.ai and come back with the expected 401 for an invalid token, which confirms the URL shape, headers and JSON envelope. Successful bot calls require a real token.

bale xyz 1.0.0 · MIT licensed · unofficial client © 2026 bale xyz contributors