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.3.0.tar.gz (27.3 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.3.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (240.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

telegram_escape-0.3.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (234.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

telegram_escape-0.3.0-cp313-cp313-macosx_11_0_arm64.whl (208.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

telegram_escape-0.3.0-cp313-cp313-macosx_10_12_x86_64.whl (213.3 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: telegram_escape-0.3.0.tar.gz
  • Upload date:
  • Size: 27.3 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.3.0.tar.gz
Algorithm Hash digest
SHA256 3010bcfaf8cf21ae7a760201576eb18eb8f4f684dc08996b11112d6b7f871cae
MD5 fe7e076241b698a5f2458ab6497f43be
BLAKE2b-256 b8b27d3496e6e3c50925c1512ee0a92ed957aadc4b287bfdedbfbba65478a997

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.3.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 240.2 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.3.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 91a625bee6df8c277567256b42b035b185098c29d9ffc7a9dc630865080c42bb
MD5 89b9a5eed9638a2068d990e99c4448ea
BLAKE2b-256 bab6e8109522ccc3f12f9193ec1e1f44e065bde2c419f4ddbf9d339433ca88be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.3.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 234.7 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.3.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 dbd1a64a68004de0747070518d332a852fc6d08de2a7f618a4e6924daef13db6
MD5 78a6fa974b8152e2afc9d0d0c03e9787
BLAKE2b-256 4009aaa580393ea08778e46e2b3c9a1b52e582c7470616510adf40c308c9cf4e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.3.0-cp313-cp313-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 208.3 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.3.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2c55c8b3870b84020224e6fae5dd7a8949e559921b2882b51b882a73d53034ec
MD5 72373cfb580e69a4c95c7f140dc58d3a
BLAKE2b-256 23a8877829becd134eadb182aa00db39f79f7290bc20d3ccdc7e7cb7645b782c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.3.0-cp313-cp313-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 213.3 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.3.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e27bc47b0665bd779aa8cc9332da13c94d4826c60d9a400ce5f9ae82d6ff44cd
MD5 7282e957f57da50a2b8243ee0cd00d20
BLAKE2b-256 21e8925467c5fd571a5761a4f6cdb1e84f8c7325247d3d6328368bf69bd44972

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