telegram-escape
A Rust library for escaping text according to Telegram's MarkdownV2 formatting rules, with Python bindings.
Overview
This library provides the tg_escape function that properly escapes special characters in text for use with Telegram Bot API's MarkdownV2 parse mode. It intelligently handles different escaping rules for regular text versus code blocks/inline code.
Available as both a Rust crate and a Python package (via PyO3).
Features
- Escapes special characters according to Telegram MarkdownV2 rules
- Smart context-aware escaping:
- In regular text: escapes
_*[]()~>#+-=|{}.!` characters - In code blocks and inline code: only escapes
`and\characters
- In regular text: escapes
- Preserves markdown structure while ensuring proper escaping
- Hand-crafted parser tailored to Telegram's Markdown flavor (no third-party parsing dependencies)
Python Usage
Requires Python 3.13+.
Installation
# With uv
# With pip
To install from source (requires Rust toolchain):
# With uv
# With pip
Example
# Basic escaping
=
# Result: "Check /get\\_stat command :\\)"
# Code blocks have different escaping rules
=
# Inside backticks, only ` and \ are escaped
Type Checking
The package ships with PEP 561 type stubs, so tg_escape is fully typed out of the box.
Rust Usage
use tg_escape;
let text = "Soon you'll get a stats for today, and the overall status can be viewed by the /get_stat command :)";
let escaped = tg_escape;
Installation
Add this to your Cargo.toml:
[]
= "0.4.0"
Testing
# Rust tests
# Python (after installing in a venv)
Dependencies
Rust
- No runtime dependencies (only optional
pyo3for Python bindings)
Python build
maturin- Build backend (PEP 517 compliant, works with pip/uv/any standard tool)pyo3- Rust ↔ Python bindings
License
MIT — see LICENSE for details.
Author
Vlad Stepanov utterstep@hey.com