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.5.tar.gz (26.6 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.5-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.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (234.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

telegram_escape-0.2.5-cp313-cp313-macosx_10_12_x86_64.whl (212.8 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: telegram_escape-0.2.5.tar.gz
  • Upload date:
  • Size: 26.6 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.5.tar.gz
Algorithm Hash digest
SHA256 81bd8a1da9061cc67ec3c8d5bc721481dcba4a4ed7b31d2897b8c216664bccc3
MD5 8517fa6de8b0c81ffc34d71f1a896da3
BLAKE2b-256 9f1ff9ee34e8a2c816ab94c2c31139a39fb047ddafd5923d30b53bf194fd520e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.2.5-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.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 133ead34a9b5cbef505ae68f665f2f82bfefcafa91f9daf4d3f34cba89384542
MD5 c658a2c18fb3abecb71200e1bf5014cb
BLAKE2b-256 2c17a255fc3c53d9042a114e4b1c89e308e66a561d6b20b4e22dc3545d579e56

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.2.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 234.6 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.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 de06b1ace768352e2de709f429a91a78744f9e91400851c12f6b13873ef0956b
MD5 9e203e2c337c4f6a3850ab7322d464dc
BLAKE2b-256 6ef99ec4d67bb9eb56be3be40af0e512a4400bdbf52d06a27ddd5901609cbf5a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.2.5-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.2.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc0ebf65fe4b5f41b3e81b9eaad65ac87761a3f5541d0a212466d7817f244535
MD5 2c7a80832d84eb0bea1a12e4c42758d1
BLAKE2b-256 7a7abb9c0be6854c8f743c86e575d9a59d90e156fd312678cb540cc71ce480df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.2.5-cp313-cp313-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 212.8 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.5-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 82f10acf0e7fe17784cf26d2db819a9fae1f7f97ef5867e340d8ca0b31fd6bd4
MD5 063c9bce91b04f48b0d6aff551ab8c08
BLAKE2b-256 4147a5e2302c6131aa5e6925bf37b2839c6c293484030c5a7b9784988ca61ab8

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