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:
| Form | Example | Meaning |
|---|---|---|
"id|type" | "987654321|2" | Canonical wire form |
| bare id | 987654321 | Assumed to be a user |
| username | "@bale_team" | Resolved lazily |
"me" | "me" | Your own saved messages |
| Type | Constant | Meaning |
|---|---|---|
| 1 | Peer.user | Private chat |
| 2 | Peer.group | Legacy group |
| 3 | Peer.channel | Broadcast channel |
| 4 | Peer.bot | Bot account |
| 5 | Peer.supergroup | Modern supergroup |
| 6 | Peer.thread | Forum 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:
- 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
EditMessageinstead ofSendMessage - 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
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.
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")
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:
| Method | MIME | ext variant | Extra metadata |
|---|---|---|---|
send_photo | image/jpeg | documentExPhoto | width, height |
send_video | video/mp4 | documentExVideo | width, height, duration |
send_voice | audio/ogg | documentExVoice | duration |
send_audio | audio/mpeg | documentExAudio | duration |
send_document | yours | none | — |
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")
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:
| Member | Returns |
|---|---|
message.text | The plain text, or empty for media-only messages |
message.id / message_id | The message id |
message.sender_id | Author peer id |
message.chat_id | Conversation peer id |
message.is_outgoing | Sent by you |
message.is_reply | Quotes another message |
message.photo / document / video / voice / audio | Media accessors, or None |
message.buttons | Inline keyboard rows |
message.command / command_args | "/start" and the trailing arguments |
message.content | The 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