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
  • Hand-crafted parser tailored to Telegram's Markdown flavor (no third-party parsing dependencies)

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.4.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'))"

Dependencies

Rust

  • No runtime dependencies (only optional pyo3 for 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

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.4.0.tar.gz (27.2 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.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (239.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

telegram_escape-0.4.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (233.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

telegram_escape-0.4.0-cp313-cp313-macosx_11_0_arm64.whl (206.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

telegram_escape-0.4.0-cp313-cp313-macosx_10_12_x86_64.whl (210.6 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: telegram_escape-0.4.0.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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.4.0.tar.gz
Algorithm Hash digest
SHA256 bd3473b5f5c5e80228c77663d65f8ef99e35badd9b476e9e7bf6bcd2fbe28bf0
MD5 477caf61a4de2e810ca02fc1aae183d6
BLAKE2b-256 acf304483b432ad232eca55e1b3b24786a8ce28d318504c591464b735a586a3a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 239.0 kB
  • Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f9aaf9c3c3d99c0d421c2f4e4549b56fdcf024f9200a18894031ec1e207f7e9
MD5 f9a913fbca089afbb771f8e38086d47c
BLAKE2b-256 863be9a3906db3082d66e167a6c6ebbcc45fc6f7430551c2e57374f5c1eb2fd5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.4.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 233.6 kB
  • Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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.4.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b926bde0a5ec1e77aac8c393cd71bcf2b0ecf12f7bd6c6d47d2fe0d5f27290aa
MD5 1fce8f36b3646513a67dce283ee947d9
BLAKE2b-256 fec0459c15377cd446168ec9690f212d39babc43fe5768f2680d087090f099b3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.4.0-cp313-cp313-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 206.2 kB
  • Tags: CPython 3.13, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 78ee0ac10439500256e4674b13cde1108542ab009dbbe328053dba7529a350be
MD5 b97b5d081b3fc2cca6eec7cb1664ea66
BLAKE2b-256 63b032effbe27f678b16d7185b75b568d2333067ce8499536ccfb0fb72468b82

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telegram_escape-0.4.0-cp313-cp313-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 210.6 kB
  • Tags: CPython 3.13, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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.4.0-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7ca44aec0c48272aed340088201e823983e7fe7193b14a21c1b72c7487e532b6
MD5 b8cec958e1b860f4a887382ce49fd32f
BLAKE2b-256 c79b41c690583f8a103cbc159b3ed2dff073fa6b914cfa91a96bf70f8df31814

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