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.1.2.tar.gz (25.1 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.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

telegram_escape-0.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

telegram_escape-0.1.2-cp313-cp313-macosx_11_0_arm64.whl (931.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

telegram_escape-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl (976.0 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: telegram_escape-0.1.2.tar.gz
  • Upload date:
  • Size: 25.1 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.1.2.tar.gz
Algorithm Hash digest
SHA256 2fba2a9d5b2f71eab04356a16083e6c6d860e1cdab1ef9ca094ede3ec7228e87
MD5 ac6e2541f485722ff68996b6f20d1dab
BLAKE2b-256 1f611108b450a4e4e77650e72c19f6628240558d11ae1a3e99678c5e86b38242

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.1 MB
  • 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.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64fd5946d1719025541ca946f5a1e7e2f00663ab09627e39be9ca565b5d4a259
MD5 c442716253691d6ffca3df39bbeb46d2
BLAKE2b-256 420e2d0776b8069a67557f8f11abd2d69da47496f86f300e3f19b5b4377f94d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 1.1 MB
  • 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.1.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b9cfcff98bcb900ab53f69eba0328bcaff3c2f088ef94a1247a3423c8eda0c24
MD5 b4ac359c370ed0e8d00cda5664148397
BLAKE2b-256 bb7f0a9934e38eb713887708058ad571c2be86e26af69c9a56c15caf346b7b8a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 931.6 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.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2872f4ddb45bcfb888861b48828e50d5a0ac25b0ba506f89e27d0713ca72d0f3
MD5 486b8ccef7cd885a5b806f7ba2a62c7d
BLAKE2b-256 e2ebe0ee70e584494b417e9bf172f9f4b395583bb605fb430de09688bc8bb74f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 976.0 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.1.2-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9c8e15dfd759af121118a7ec58f0f8b4dc77d3822d85d9ab324c2d484703db99
MD5 aeeffa7f67441c3c4c8b7059ad8daf43
BLAKE2b-256 5523e859ef474a642f024583cbf0082127a5ef7ae9ae20db62a43e676c985cf2

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