Metadata-Version: 2.4
Name: balthon
Version: 0.1.2
Summary: An asynchronous Python library for building bots for the Bale messenger.
Author-email: SAUMS <balthonSupport@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/saums-1391/balthon
Project-URL: Repository, https://github.com/saums-1391/balthon
Project-URL: Issues, https://github.com/saums-1391/balthon/issues
Keywords: bale,bale-messenger,bot,bot-api,async,asyncio
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Communications :: Chat
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: httpx>=0.24.0
Requires-Dist: fastapi>=0.100.0
Requires-Dist: uvicorn>=0.23.0

# **Balthon**

**Balthon** is an asynchronous Python library for building bots for the [Bale](https://bale.ai) messenger.

It provides an async client, update handlers, filters, message and user models, keyboard helpers, state management, and methods for interacting with the Bale Bot API.

## Features

- Asynchronous API client built on `httpx`
- Long-polling support
- Optional built-in webhook server
- Update handlers and composable filters
- Message, user, chat, callback-query, and media models
- Reply and inline keyboard helpers
- Per-user update queues and rate limiting
- Lightweight state manager with expiration
- Bale API operations for messages, chats, files, members, stickers, and payments

## Requirements

- Python 3.9 or newer
- An active Bale bot token

## Installation

Install the package from PyPI:

```bash
pip install balthon
```

For webhook mode, install the optional dependencies:

```bash
pip install "balthon[webhook]"
```

## Quick start

Create a bot in Bale and obtain its token. Then create a Python file:

```python
from balthon import BotClient, filters

bot = BotClient("YOUR_BOT_TOKEN")


@bot.on_update(filters.command("start"))
async def start_handler(update):
    await bot.reply("Hello! Welcome to my bot.")


bot.run()
```

Replace `YOUR_BOT_TOKEN` with your bot token.

## Handling updates

Register handlers with `on_update`. You can pass one or more filters to decide which updates a handler receives.

```python
from balthon import BotClient, filters

bot = BotClient("YOUR_BOT_TOKEN")


@bot.on_update(filters.text())
async def text_handler(update):
    await bot.reply(f"You sent: {update.text}")


@bot.on_update(filters.photo())
async def photo_handler(update):
    await bot.reply("I received a photo!")


bot.run()
```

The library includes filters for common update types and conditions, including text, commands, private chats, groups, channels, photos, documents, audio, video, voice messages, stickers, callbacks, contacts, locations, membership status, user IDs, chat IDs, and state checks.

Filters can be combined with `&` (AND), `|` (OR), and `~` (NOT).

```python
@bot.on_update(filters.private() & filters.text())
async def private_text_handler(update):
    await bot.reply("This is a private text message.")
```

## Keyboards

Balthon provides helpers for reply and inline keyboards.

```python
from balthon import BotClient, Button, KeyBoard, KeyBoardRow

bot = BotClient("YOUR_BOT_TOKEN")


@bot.on_update(filters.command("menu"))
async def menu_handler(update):
    keyboard = KeyBoard(
        KeyBoardRow(Button("Option 1"), Button("Option 2")),
        resize=True,
    )
    await bot.reply("Choose an option:", markup=keyboard)


bot.run()
```

For inline keyboards:

```python
from balthon import InlineButton, InLineRow, InLineKeyboard

keyboard = InLineKeyboard(
    InLineRow(InlineButton("Open Bale", url="https://bale.ai"))
)
```

## State management

Use `StateManager` to store temporary state for a user. States expire after the configured number of seconds.

```python
from balthon import BotClient, StateManager, filters

bot = BotClient("YOUR_BOT_TOKEN")
states = StateManager()


@bot.on_update(filters.command("begin"))
async def begin(update):
    await states.set_state_for(update, "waiting_for_name", expire=60)
    await bot.reply("What is your name?")


@bot.on_update(filters.at_state(states, "waiting_for_name"))
async def receive_name(update):
    name = update.text
    await bot.reply(f"Nice to meet you, {name}!")
    await states.clear_state_for(update)


bot.run()
```

## Webhook mode

Webhook mode uses FastAPI and Uvicorn. Install the optional dependencies first:

```bash
pip install "balthon[webhook]"
```

Then start the client with `use_webhook=True`:

```python
from balthon import BotClient, filters

bot = BotClient("YOUR_BOT_TOKEN", use_webhook=True)

@bot.on_update(filters.text())
async def text_handler(update):
    await bot.reply(update.text)

bot.run(host="0.0.0.0", port=8000, webhook_path="/webhook")
```

Configure your Bale bot webhook to point to the publicly reachable endpoint for your server. Make sure your deployment, network, and webhook URL are configured appropriately.

## Async applications

If your application already runs an asyncio event loop, use `await bot.start()` instead of `bot.run()`:

```python
import asyncio
from balthon import BotClient

bot = BotClient("YOUR_BOT_TOKEN")


async def main():
    await bot.start()


asyncio.run(main())
```

## API overview

The package exports the main client as `BotClient` (also available as `Client` and `BotConnect`), along with:

- `filters` and `Filter`
- `StateManager`
- Keyboard and button helpers
- Update, message, user, chat, media, and payment models
- Bale API response models
- `ActionsEnum`
- Balthon-specific exception classes

See the source code and exported names in [`balthon/__init__.py`](balthon/__init__.py) for the complete public API.

## Error handling

Balthon defines exception classes for common client and API errors, including:

- `BalthonError`
- `BaleError`
- `APIError`
- `NetworkError`
- `InvalidTokenError`
- `AuthenticationError`
- `RateLimitError`
- `InvalidRequestError`
- `UpdateError`
- `HandlerError`
- `FilterError`
- `StateError`

You can import these directly from `balthon`:

```python
from balthon import APIError, NetworkError

try:
    # Call a Balthon API method here.
    pass
except NetworkError as error:
    print(f"Network error: {error}")
except APIError as error:
    print(f"Bale API error: {error}")
```

## Links

- **GitHub:** https://github.com/saums-1391/balthon
- **Issues:** https://github.com/saums-1391/balthon/issues
- **Email:** balthonSupport@gmail.com

## License

This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
