Skip to main content

Escape text for Telegram's MarkdownV2 formatting

Project description

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
  • Preserves markdown structure while ensuring proper escaping
  • Uses pulldown-cmark for robust markdown parsing

Python Usage

Requires Python 3.13+.

Installation

# With uv
uv add telegram-escape

# With pip
pip install telegram-escape

To install from source (requires Rust toolchain):

# With uv
uv pip install .

# With pip
pip install .

Example

from telegram_escape import tg_escape

# Basic escaping
escaped = tg_escape("Check /get_stat command :)")
# Result: "Check /get\\_stat command :\\)"

# Code blocks have different escaping rules
escaped = tg_escape("Before `a_*~>#+-=|{}.!\\` after")
# 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 telegram_escape::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(text);

Installation

Add this to your Cargo.toml:

[dependencies]
telegram-escape = "0.1.0"

Testing

# Rust tests
cargo test

# Python (after installing in a venv)
python -c "from telegram_escape import tg_escape; print(tg_escape('hello_world'))"

Note: Some Rust tests are currently marked as #[ignore] due to failing edge cases:

  • test_escaped_characters - handling of already-escaped characters
  • test_math_expressions - mathematical operators escaping

Dependencies

Rust

  • pulldown-cmark - Markdown parsing
  • pulldown-cmark-to-cmark - Markdown serialization (using custom fork)
  • regex - Pattern matching for escape characters

Python build

  • maturin - Build backend (PEP 517 compliant, works with pip/uv/any standard tool)
  • pyo3 - Rust ↔ Python bindings

License

See LICENSE file for details.

Author

Vlad Stepanov utterstep@hey.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_escape-0.2.3.tar.gz (26.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (239.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (233.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

telegram_escape-0.2.3-cp313-cp313-macosx_11_0_arm64.whl (208.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

telegram_escape-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl (211.9 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

Details for the file telegram_escape-0.2.3.tar.gz.

File metadata

  • Download URL: telegram_escape-0.2.3.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telegram_escape-0.2.3.tar.gz
Algorithm Hash digest
SHA256 959ddafd9664c4f859640295facf5182ff0d8cfc0d5cd3cd786aa81b87d8f844
MD5 4f58396cd05680fa8d53a7e307c50469
BLAKE2b-256 0967ca5173514af0c4da57cfd3e61a2fe5d8c28fe776764ce5a14c7926b54c62

See more details on using hashes here.

File details

Details for the file telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 239.8 kB
  • Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 52e54759a36c7d3c8402cbb28e34e065258c1f53741b940c81ef4a097e72385a
MD5 79fadc9fe706f62f0248a47449d6283f
BLAKE2b-256 f8ed5c664dc65b1e0fde34d2a65a0875fc4eeeb6fc3213e1428c2f7ea667bc0a

See more details on using hashes here.

File details

Details for the file telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

  • Download URL: telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 233.5 kB
  • Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telegram_escape-0.2.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e38ed6f8e6b7ab9c1693735463696b0c0d43bbde0f4fed08adaae0ecf107429f
MD5 07b0221863b89f3cbc785249030ef193
BLAKE2b-256 28ac30a734137d297c3bb15c71a48faf87668aee5abdb982ae3ec1c6382d6ff8

See more details on using hashes here.

File details

Details for the file telegram_escape-0.2.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

  • Download URL: telegram_escape-0.2.3-cp313-cp313-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 208.2 kB
  • Tags: CPython 3.13, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telegram_escape-0.2.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 beecaaa01fab7d900922d7a2bf7b4a9e34798f71ac7a52449d45f729d3999e71
MD5 38ecc911e8ba5a1d6049670c98f835e6
BLAKE2b-256 234b5ba06afaf66fcc12c96a0c2801640798da71fc90d158b295b76d8d6bb057

See more details on using hashes here.

File details

Details for the file telegram_escape-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: telegram_escape-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 211.9 kB
  • Tags: CPython 3.13, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for telegram_escape-0.2.3-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a1181a925716638a1c64e0defa697b4b6544cd34858586747867fc6b675aa1fa
MD5 5a5121cfc693fc7c4659907a4eb6e58f
BLAKE2b-256 3604b660a74832aab72f345f5fbb3edb3e53412b615accf79fb239021a8bfecd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page