Files
thon/README.md
trojvn 0761e8ff94 docs: add project documentation
Add comprehensive documentation for the Thon project, including project overview, architecture, usage examples, and development conventions. Update the README.md file to provide clear installation instructions, usage examples, and project structure. Remove the unused CLI entry point from pyproject.toml.
2026-01-25 23:02:19 +03:00

3.1 KiB
Raw Blame History

Thon

Thon is a lightweight, extensible Python library designed for processing Telegram sessions. It provides a clean, asynchronous interface to handle session conversion and automated processing tasks.


📥 Installation

Install the library directly from the repository:

pip install git+https://github.com/your-username/thon.git

🛠 Usage Examples

1. Basic Session Iteration

Iterate through your .session files automatically. Thon converts them on the fly and provides an authorized telethon client for each.

import asyncio
from pathlib import Path
from thon import Thon
from thon.models.options import ThonOptions

async def main():
    # Folder containing your .session files
    sessions_path = Path("my_sessions")
    
    # Basic options
    options = ThonOptions(retries=5, timeout=15)
    
    async for thon_session in Thon(options, sessions_folder=sessions_path):
        # thon_session.thon is an active ProcessThon instance
        # thon_session.session contains session metadata
        
        me = thon_session.thon.me
        print(f"Logged in as: {me.first_name} (@{me.username})")
        
        # Access the underlying Telethon client
        client = thon_session.thon.client
        await client.send_message('me', 'Hello from Thon!')

asyncio.run(main())

2. Searching for Verification Codes

A common task is waiting for a verification code (e.g., from Telegram or a bot). Thon has built-in support for this.

from thon.models.misc import ThonSearchCodeOptions

async def wait_for_code(thon_instance):
    search_options = ThonSearchCodeOptions(
        wait_time=60,    # How long to wait for the message (seconds)
        date_delta=300,  # Only look at messages from the last 5 minutes
        regexp=r'\d{5}'  # Custom regex for the code
    )
    
    code = await thon_instance.search_code(search_options)
    if code:
        print(f"Found code: {code}")
    else:
        print("Code not found.")

3. Handling Banned Sessions and Errors

You can provide a callback to handle errors and automatically move banned sessions to a separate folder.

def on_error():
    print("An error occurred with a session!")

async def process_with_safety():
    options = ThonOptions()
    bot = Thon(
        options,
        sessions_folder=Path("sessions"),
        move_banned_folder=True,  # Automatically moves banned sessions to sessions/banned/
        callback_error=on_error
    )
    
    async for thon_session in bot:
        # Only valid, unbanned sessions reach here
        pass

📁 Project Structure

thon/
├── thon/               # Core package directory
│   ├── __init__.py     # Library entry point (Thon class)
│   ├── converter.py    # Session conversion logic
│   ├── process.py      # Main processing logic (ProcessThon)
│   ├── models/         # Data models and options
│   └── utils.py        # Utility functions
├── pyproject.toml      # Build system and metadata
└── README.md           # Documentation

📜 License

This project is licensed under the MIT License.