Telegram Text Splitter
A Python library for splitting long Markdown texts into smaller, Telegram-friendly chunks. It intelligently breaks down text based on Markdown formatting (paragraphs, lines, words) to ensure that each chunk is a valid and readable piece of text, suitable for sending via Telegram bots.
Features
- Splits Markdown text into chunks, each respecting Telegram's message length limits.
- Prioritizes splitting at natural boundaries like paragraph breaks (
\n\n), line breaks (\n), and spaces. - Ensures that no Markdown formatting is broken, making subsequent conversion to HTML (e.g., using
chatgpt-md-converter) reliable. - Lightweight and dependency-free (except for standard Python libraries).
Installation
You can install the library directly from GitHub:
pip install git+https://github.com/kobaltgit/telegram_text_splitter.git
Alternatively, if you have the library cloned locally, you can install it in editable mode:
cd path/to/your/telegram_text_splitter_lib
pip install -e .
Usage
Here's a simple example of how to use the split_markdown_into_chunks function:
from telegram_text_splitter import split_markdown_into_chunks
long_markdown_text = """
# My Awesome Title
This is the first paragraph. It contains some text that needs to be split.
We are aiming for chunks that are less than 4000 characters.
This is the second paragraph. It's separated by a double newline.
### A Subheading
* Item 1
* Item 2
* Sub-item 2.1
* Sub-item 2.2
And a very long word that might cause issues if not handled correctly: Antidisestablishmentarianism.
"""
chunks = split_markdown_into_chunks(long_markdown_text)
for i, chunk in enumerate(chunks):
print(f"--- Chunk {i+1} (Length: {len(chunk)}) ---")
print(chunk)
print("-" * 20)
# Example usage in a Telegram bot handler:
# from aiogram import Bot, types
# from aiogram.enums import ParseMode
# from telegram_text_splitter import split_markdown_into_chunks
# from chatgpt_md_converter import telegram_format # Assuming this is imported elsewhere
# from utils.message_sender import send_long_message
# async def send_ai_response(bot: Bot, chat_id: int, ai_markdown_response: str, i18n):
# markdown_chunks = split_markdown_into_chunks(ai_markdown_response)
# for md_chunk in markdown_chunks:
# html_chunk = telegram_format(md_chunk)
# await send_long_message(
# bot=bot,
# chat_id=chat_id,
# text_chunks=[html_chunk], # Send each HTML chunk as a single item list
# keyboard=None, # Add your keyboard here if needed
# parse_mode=ParseMode.HTML
# )
Integration with chatgpt-md-converter
This library works especially well in combination with chatgpt-md-converter , which converts Markdown into Telegram-compatible HTML. This allows you to safely split and then send formatted text messages via Telegram bots, without breaking Markdown structure or exceeding message length limits.
Using both libraries together ensures:
- Reliable conversion to HTML understood by the Telegram Bot API
- Support for advanced Telegram-specific syntax like spoilers (||text||) and expandable blockquotes (**> text)
- Safe handling of code blocks, lists, nested styles, and more
✅ Example Usage
from telegram_text_splitter import split_markdown_into_chunks
from chatgpt_md_converter import telegram_format
# A long Markdown-formatted response from ChatGPT or another source
long_markdown_text = """
# Introduction
This is a **Markdown** example that includes *italic*, __underlined__, and ~~strikethrough~~ text.
||This is a spoiler||
Code block inside Markdown print("Hello, world!")
"""
# Step 1: Split the Markdown into chunks suitable for Telegram
chunks = split_markdown_into_chunks(long_markdown_text)
# Step 2: Convert each chunk to Telegram-compatible HTML and send
for chunk in chunks:
html_chunk = telegram_format(chunk)
print(html_chunk) # Replace this with bot.send_message(...) using ParseMode.HTML
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and add tests.
- Ensure your code follows PEP 8 style guidelines.
- Submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Release files for telegram-text-splitter 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| telegram_text_splitter-0.2.0.tar.gz | 20.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| telegram_text_splitter-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.0 kB
Release files / telegram_text_splitter-0.2.0.tar.gz
| Download URL | telegram_text_splitter-0.2.0.tar.gz |
|---|---|
| Size | 20.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
af3a6fd9cb894a696638ccaf31949035fe69d277ef793921f150704bcb59fde5
|
|
BLAKE2b-256 checksum How to use checksums |
972596149fc4f294b0388a3427c0573757a1c4a94bd96d696873351575bd0560
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 20, 2026.
Transparency logRelease files / telegram_text_splitter-0.2.0-py3-none-any.whl
| Download URL | telegram_text_splitter-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae2484890c93a4f76e05b27afe9d817faa8856d585dc5d4a569a9776b1ee9cb1
|
|
BLAKE2b-256 checksum How to use checksums |
bde301a119bf4b48f3afb025d10c6238348369e2db7890f30e4844718492b8ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 20, 2026.
Transparency log