A Python library for splitting long Markdown texts into Telegram-friendly chunks.
Project description
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
# )
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_text_splitter-0.1.1.tar.gz.
File metadata
- Download URL: telegram_text_splitter-0.1.1.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aae4b9c69633bfc68e6d7b55ecd1ae0b939c0652007895255127dd59b38aa131
|
|
| MD5 |
c5898163887d2dc51fc5c9be7efb4318
|
|
| BLAKE2b-256 |
8f436e3320c1899f3e003ce65bd7ebb9c079519b52832699f55fca36c6225e7f
|
Provenance
The following attestation bundles were made for telegram_text_splitter-0.1.1.tar.gz:
Publisher:
python-publish.yml on kobaltgit/telegram_text_splitter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telegram_text_splitter-0.1.1.tar.gz -
Subject digest:
aae4b9c69633bfc68e6d7b55ecd1ae0b939c0652007895255127dd59b38aa131 - Sigstore transparency entry: 264354279
- Sigstore integration time:
-
Permalink:
kobaltgit/telegram_text_splitter@c8514ce23453331efbe4172236aac536fb2320be -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/kobaltgit
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c8514ce23453331efbe4172236aac536fb2320be -
Trigger Event:
release
-
Statement type:
File details
Details for the file telegram_text_splitter-0.1.1-py3-none-any.whl.
File metadata
- Download URL: telegram_text_splitter-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7751181dcf9a23cedb87dcfa5602b2e355e34d485f5f979168f256d4b244089c
|
|
| MD5 |
5df8d0aea944828fab38c8e356522fd5
|
|
| BLAKE2b-256 |
8ac85d1390d6820c8ef37210b834ae434e08b2bc73a2b6178a2d396f38154fe6
|
Provenance
The following attestation bundles were made for telegram_text_splitter-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on kobaltgit/telegram_text_splitter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telegram_text_splitter-0.1.1-py3-none-any.whl -
Subject digest:
7751181dcf9a23cedb87dcfa5602b2e355e34d485f5f979168f256d4b244089c - Sigstore transparency entry: 264354284
- Sigstore integration time:
-
Permalink:
kobaltgit/telegram_text_splitter@c8514ce23453331efbe4172236aac536fb2320be -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/kobaltgit
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c8514ce23453331efbe4172236aac536fb2320be -
Trigger Event:
release
-
Statement type: