English فارسی

Messages & media

Send, reply, edit, forward, react and upload. Every method works with a peer string, a bare id, or a resolved @username.

Peer addressing

Bale identifies a conversation by (id, type). Three textual forms are accepted everywhere:

FormExampleMeaning
"id|type""987654321|2"Canonical wire form
bare id987654321Assumed to be a user
username"@bale_team"Resolved lazily
"me""me"Your own saved messages
TypeConstantMeaning
1Peer.userPrivate chat
2Peer.groupLegacy group
3Peer.channelBroadcast channel
4Peer.botBot account
5Peer.supergroupModern supergroup
6Peer.threadForum thread
from balexyz import Peer

Peer.parse("987654321|2").to_dict()    # {"id": 987654321, "type": 2}
Peer.parse(987654321).to_dict()        # {"id": 987654321, "type": 1}
str(Peer.parse("@bale_team"))          # "@bale_team"

Sending text

# plain
await client.send_message("987654321|1", "hello")

# reply to a specific message
await client.send_message("987654321|1", "see above", reply_to=12345)

# no notification sound
await client.send_message("987654321|1", "deploy finished", silent=True)

Signature:

async send_message(chat_id, text, *, reply_to=None, silent=False, edit=False)
chat_id
Peer string, bare id or @username
text
Plain text; markup is not parsed by the server
reply_to
Message id to quote
silent
Suppress the push notification
edit
Route to EditMessage instead of SendMessage
returns
The raw response message (bale.messaging.v2)

Edit and delete

await client.edit_message("987654321|1", message_id=12345, text="corrected")

# revoke=True deletes for everyone; False only for your own view
await client.delete_messages("987654321|1", [12345, 12346], revoke=True)

On a Message object the same thing is shorter:

await message.edit_text("new body")
await message.delete()
await message.seen()          # mark the chat as read

Files and media

Bale has no single "send file" RPC. The browser reserves an upload slot, pushes the bytes to a Nasim URL, then sends a message that references the file id. bale xyz exposes all three layers, plus one-call shortcuts.

The three layers

async upload_file(path=None, *, data=None, name=None, mime_type="application/octet-stream", chat_id=None, kind="document", put=True)

Reserve a slot (GetNasimFileUploadUrl), then PUT the bytes to the returned URL. Returns a reference you can reuse.

info = await client.upload_file("report.pdf", mime_type="application/pdf")

info["file_id"]      # 99887766
info["url"]          # https://nasim.ble.ir/put/...
info["duplicate"]    # True → Bale already had these bytes; no PUT was made
info["size"]         # 184320
info["crc"]          # zlib.crc32 of the payload

Pass put=False to stop after the slot is reserved and stream the bytes yourself.

async send_file(chat_id, path=None, *, data=None, name=None, mime_type="…", caption=None, kind="document", width=0, height=0, duration=0, reply_to=None, silent=False)

Upload and send in one call. Use this when you do not want to manage the reference.

await client.send_file("987654321|1", "report.pdf",
                       caption="Q4 numbers", mime_type="application/pdf")
async download_file(file_id, *, ciphertext=False) → bytes

Resolve the id to a CDN URL (GetNasimFileUrl) and download it. Use get_file_url() if you only want the link.

Media shortcuts

These set the right MIME type and the ext variant, so the recipient sees a photo bubble instead of a paperclip:

MethodMIMEext variantExtra metadata
send_photoimage/jpegdocumentExPhotowidth, height
send_videovideo/mp4documentExVideowidth, height, duration
send_voiceaudio/oggdocumentExVoiceduration
send_audioaudio/mpegdocumentExAudioduration
send_documentyoursnone—
await client.send_photo("987654321|1", "cat.jpg", caption="look", width=1280, height=960)
await client.send_voice("987654321|1", "note.ogg", duration=12)
await client.send_document("987654321|1", "invoice.pdf", name="invoice.pdf",
                           mime_type="application/pdf")
Why dimensions are not optional in spirit.

Protobuf 3 does not serialise a sub-message whose fields are all default, so an empty ext variant would vanish on the wire and the file would render as a plain document. Pass real width/height for images and duration for audio and video; if you omit them the library substitutes a harmless 1 so the variant survives.

Quota

limits = await client.get_upload_limits()
print(limits["upload_limit_bytes"], limits["permanent_max_bytes"])

What the payload actually looks like

documentMessage has no wrappers on the id fields — this is the single most common source of broken uploads in other libraries:

{
  "documentMessage": {
    "fileId":     99887766,          # int64, NOT {"value": ...}
    "accessHash": 0,                 # int64, NOT {"value": ...}
    "fileSize":   184320,            # int32
    "name":       "report.pdf",
    "mimeType":   "application/pdf",
    "caption":    {"text": "Q4"},    # singular, not "captions"
    "ext":        {"documentExPhoto": {"w": 1280, "h": 960}}
  }
}

Pinned by tests/test_media.py, which encodes and decodes the real descriptor rather than trusting a dict literal.

The Message object

Incoming messages arrive wrapped in a Message with 57 public members. The useful ones:

MemberReturns
message.textThe plain text, or empty for media-only messages
message.id / message_idThe message id
message.sender_idAuthor peer id
message.chat_idConversation peer id
message.is_outgoingSent by you
message.is_replyQuotes another message
message.photo / document / video / voice / audioMedia accessors, or None
message.buttonsInline keyboard rows
message.command / command_args"/start" and the trailing arguments
message.contentThe decoded structured body

Actions:

await message.reply("got it")
await message.answer("also works")          # alias for reply
await message.forward("555|1")
await message.copy("555|1")
await message.react("❤️")
await message.remove_reaction()
await message.pin()                          # pin in the chat
await message.download("/tmp/photo.jpg")

The Chat object

chat = await client.get_chat("987654321|2")

chat.title            # group name
chat.is_group         # True
await chat.send("hello everyone")
await chat.send_action("typing")
history = await chat.load_history(limit=50)

History and search

page = await client.get_history("987654321|2", limit=50)
for msg in page.get("messages", []):
    print(msg["id"], msg.get("text", "")[:40])

# paginate backwards
older = await client.get_history("987654321|2", limit=50, before=min_id)

await client.search_messages("987654321|2", "invoice", limit=20)
await client.search_users("sara", limit=10)

Typing indicator

await client.send_typing("987654321|1")              # "typing"
await client.send_typing("987654321|1", "uploading")  # other actions
bale xyz 1.0.0 · MIT licensed · unofficial client © 2026 bale xyz contributors