Skip to main content

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:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make your changes and add tests.
  4. Ensure your code follows PEP 8 style guidelines.
  5. Submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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_text_splitter-0.1.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_text_splitter-0.1.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

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

Hashes for telegram_text_splitter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aae4b9c69633bfc68e6d7b55ecd1ae0b939c0652007895255127dd59b38aa131
MD5 c5898163887d2dc51fc5c9be7efb4318
BLAKE2b-256 8f436e3320c1899f3e003ce65bd7ebb9c079519b52832699f55fca36c6225e7f

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file telegram_text_splitter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_text_splitter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7751181dcf9a23cedb87dcfa5602b2e355e34d485f5f979168f256d4b244089c
MD5 5df8d0aea944828fab38c8e356522fd5
BLAKE2b-256 8ac85d1390d6820c8ef37210b834ae434e08b2bc73a2b6178a2d396f38154fe6

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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