balthon 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- balthon-0.1.0/PKG-INFO +274 -0
- balthon-0.1.0/README.md +245 -0
- balthon-0.1.0/balthon.egg-info/PKG-INFO +274 -0
- balthon-0.1.0/balthon.egg-info/SOURCES.txt +7 -0
- balthon-0.1.0/balthon.egg-info/dependency_links.txt +1 -0
- balthon-0.1.0/balthon.egg-info/requires.txt +3 -0
- balthon-0.1.0/balthon.egg-info/top_level.txt +1 -0
- balthon-0.1.0/pyproject.toml +47 -0
- balthon-0.1.0/setup.cfg +4 -0
balthon-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: balthon
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An asynchronous Python library for building bots for the Bale messenger.
|
|
5
|
+
Author-email: SAUMS <balthonSupport@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/saums-1391/balthon
|
|
8
|
+
Project-URL: Repository, https://github.com/saums-1391/balthon
|
|
9
|
+
Project-URL: Issues, https://github.com/saums-1391/balthon/issues
|
|
10
|
+
Keywords: bale,bale-messenger,bot,bot-api,async,asyncio
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Framework :: AsyncIO
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Communications :: Chat
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
Requires-Dist: httpx>=0.24.0
|
|
27
|
+
Requires-Dist: fastapi>=0.100.0
|
|
28
|
+
Requires-Dist: uvicorn[standard]>=0.23.0
|
|
29
|
+
|
|
30
|
+
# Balthon
|
|
31
|
+
|
|
32
|
+
**Balthon** is an asynchronous Python library for building bots for the [Bale](https://bale.ai) messenger.
|
|
33
|
+
|
|
34
|
+
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.
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- Asynchronous API client built on `httpx`
|
|
39
|
+
- Long-polling support
|
|
40
|
+
- Optional built-in webhook server
|
|
41
|
+
- Update handlers and composable filters
|
|
42
|
+
- Message, user, chat, callback-query, and media models
|
|
43
|
+
- Reply and inline keyboard helpers
|
|
44
|
+
- Per-user update queues and rate limiting
|
|
45
|
+
- Lightweight state manager with expiration
|
|
46
|
+
- Bale API operations for messages, chats, files, members, stickers, and payments
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
- Python 3.9 or newer
|
|
51
|
+
- An active Bale bot token
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Install the package from PyPI:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install balthon
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
For webhook mode, install the optional dependencies:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install "balthon[webhook]"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Quick start
|
|
68
|
+
|
|
69
|
+
Create a bot in Bale and obtain its token. Then create a Python file:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from balthon import BotClient, filters
|
|
73
|
+
|
|
74
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@bot.on_update(filters.command("start"))
|
|
78
|
+
async def start_handler(update):
|
|
79
|
+
await bot.reply("Hello! Welcome to my bot.")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
bot.run()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Replace `YOUR_BOT_TOKEN` with your bot token.
|
|
86
|
+
|
|
87
|
+
## Handling updates
|
|
88
|
+
|
|
89
|
+
Register handlers with `on_update`. You can pass one or more filters to decide which updates a handler receives.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from balthon import BotClient, filters
|
|
93
|
+
|
|
94
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@bot.on_update(filters.text())
|
|
98
|
+
async def text_handler(update):
|
|
99
|
+
await bot.reply(f"You sent: {update.text}")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@bot.on_update(filters.photo())
|
|
103
|
+
async def photo_handler(update):
|
|
104
|
+
await bot.reply("I received a photo!")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
bot.run()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
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.
|
|
111
|
+
|
|
112
|
+
Filters can be combined with `&` (AND), `|` (OR), and `~` (NOT).
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
@bot.on_update(filters.private() & filters.text())
|
|
116
|
+
async def private_text_handler(update):
|
|
117
|
+
await bot.reply("This is a private text message.")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Keyboards
|
|
121
|
+
|
|
122
|
+
Balthon provides helpers for reply and inline keyboards.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from balthon import BotClient, Button, KeyBoard, KeyBoardRow
|
|
126
|
+
|
|
127
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@bot.on_update(filters.command("menu"))
|
|
131
|
+
async def menu_handler(update):
|
|
132
|
+
keyboard = KeyBoard(
|
|
133
|
+
KeyBoardRow(Button("Option 1"), Button("Option 2")),
|
|
134
|
+
resize=True,
|
|
135
|
+
)
|
|
136
|
+
await bot.reply("Choose an option:", markup=keyboard)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
bot.run()
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
For inline keyboards:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from balthon import InlineButton, InLineRow, InLineKeyboard
|
|
146
|
+
|
|
147
|
+
keyboard = InLineKeyboard(
|
|
148
|
+
InLineRow(InlineButton("Open Bale", url="https://bale.ai"))
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## State management
|
|
153
|
+
|
|
154
|
+
Use `StateManager` to store temporary state for a user. States expire after the configured number of seconds.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from balthon import BotClient, StateManager, filters
|
|
158
|
+
|
|
159
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
160
|
+
states = StateManager()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@bot.on_update(filters.command("begin"))
|
|
164
|
+
async def begin(update):
|
|
165
|
+
await states.set_state_for(update, "waiting_for_name", expire=60)
|
|
166
|
+
await bot.reply("What is your name?")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@bot.on_update(filters.at_state(states, "waiting_for_name"))
|
|
170
|
+
async def receive_name(update):
|
|
171
|
+
name = update.text
|
|
172
|
+
await bot.reply(f"Nice to meet you, {name}!")
|
|
173
|
+
await states.clear_state_for(update)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
bot.run()
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Webhook mode
|
|
180
|
+
|
|
181
|
+
Webhook mode uses FastAPI and Uvicorn. Install the optional dependencies first:
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
pip install "balthon[webhook]"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Then start the client with `use_webhook=True`:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from balthon import BotClient, filters
|
|
191
|
+
|
|
192
|
+
bot = BotClient("YOUR_BOT_TOKEN", use_webhook=True)
|
|
193
|
+
|
|
194
|
+
@bot.on_update(filters.text())
|
|
195
|
+
async def text_handler(update):
|
|
196
|
+
await bot.reply(update.text)
|
|
197
|
+
|
|
198
|
+
bot.run(host="0.0.0.0", port=8000, webhook_path="/webhook")
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
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.
|
|
202
|
+
|
|
203
|
+
## Async applications
|
|
204
|
+
|
|
205
|
+
If your application already runs an asyncio event loop, use `await bot.start()` instead of `bot.run()`:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
import asyncio
|
|
209
|
+
from balthon import BotClient
|
|
210
|
+
|
|
211
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
async def main():
|
|
215
|
+
await bot.start()
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
asyncio.run(main())
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## API overview
|
|
222
|
+
|
|
223
|
+
The package exports the main client as `BotClient` (also available as `Client` and `BotConnect`), along with:
|
|
224
|
+
|
|
225
|
+
- `filters` and `Filter`
|
|
226
|
+
- `StateManager`
|
|
227
|
+
- Keyboard and button helpers
|
|
228
|
+
- Update, message, user, chat, media, and payment models
|
|
229
|
+
- Bale API response models
|
|
230
|
+
- `ActionsEnum`
|
|
231
|
+
- Balthon-specific exception classes
|
|
232
|
+
|
|
233
|
+
See the source code and exported names in [`balthon/__init__.py`](balthon/__init__.py) for the complete public API.
|
|
234
|
+
|
|
235
|
+
## Error handling
|
|
236
|
+
|
|
237
|
+
Balthon defines exception classes for common client and API errors, including:
|
|
238
|
+
|
|
239
|
+
- `BalthonError`
|
|
240
|
+
- `BaleError`
|
|
241
|
+
- `APIError`
|
|
242
|
+
- `NetworkError`
|
|
243
|
+
- `InvalidTokenError`
|
|
244
|
+
- `AuthenticationError`
|
|
245
|
+
- `RateLimitError`
|
|
246
|
+
- `InvalidRequestError`
|
|
247
|
+
- `UpdateError`
|
|
248
|
+
- `HandlerError`
|
|
249
|
+
- `FilterError`
|
|
250
|
+
- `StateError`
|
|
251
|
+
|
|
252
|
+
You can import these directly from `balthon`:
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
from balthon import APIError, NetworkError
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
# Call a Balthon API method here.
|
|
259
|
+
pass
|
|
260
|
+
except NetworkError as error:
|
|
261
|
+
print(f"Network error: {error}")
|
|
262
|
+
except APIError as error:
|
|
263
|
+
print(f"Bale API error: {error}")
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Links
|
|
267
|
+
|
|
268
|
+
- **GitHub:** https://github.com/saums-1391/balthon
|
|
269
|
+
- **Issues:** https://github.com/saums-1391/balthon/issues
|
|
270
|
+
- **Email:** balthonSupport@gmail.com
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
balthon-0.1.0/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# Balthon
|
|
2
|
+
|
|
3
|
+
**Balthon** is an asynchronous Python library for building bots for the [Bale](https://bale.ai) messenger.
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Asynchronous API client built on `httpx`
|
|
10
|
+
- Long-polling support
|
|
11
|
+
- Optional built-in webhook server
|
|
12
|
+
- Update handlers and composable filters
|
|
13
|
+
- Message, user, chat, callback-query, and media models
|
|
14
|
+
- Reply and inline keyboard helpers
|
|
15
|
+
- Per-user update queues and rate limiting
|
|
16
|
+
- Lightweight state manager with expiration
|
|
17
|
+
- Bale API operations for messages, chats, files, members, stickers, and payments
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
- Python 3.9 or newer
|
|
22
|
+
- An active Bale bot token
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
Install the package from PyPI:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install balthon
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
For webhook mode, install the optional dependencies:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install "balthon[webhook]"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick start
|
|
39
|
+
|
|
40
|
+
Create a bot in Bale and obtain its token. Then create a Python file:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from balthon import BotClient, filters
|
|
44
|
+
|
|
45
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@bot.on_update(filters.command("start"))
|
|
49
|
+
async def start_handler(update):
|
|
50
|
+
await bot.reply("Hello! Welcome to my bot.")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
bot.run()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Replace `YOUR_BOT_TOKEN` with your bot token.
|
|
57
|
+
|
|
58
|
+
## Handling updates
|
|
59
|
+
|
|
60
|
+
Register handlers with `on_update`. You can pass one or more filters to decide which updates a handler receives.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from balthon import BotClient, filters
|
|
64
|
+
|
|
65
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@bot.on_update(filters.text())
|
|
69
|
+
async def text_handler(update):
|
|
70
|
+
await bot.reply(f"You sent: {update.text}")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@bot.on_update(filters.photo())
|
|
74
|
+
async def photo_handler(update):
|
|
75
|
+
await bot.reply("I received a photo!")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
bot.run()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
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.
|
|
82
|
+
|
|
83
|
+
Filters can be combined with `&` (AND), `|` (OR), and `~` (NOT).
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
@bot.on_update(filters.private() & filters.text())
|
|
87
|
+
async def private_text_handler(update):
|
|
88
|
+
await bot.reply("This is a private text message.")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Keyboards
|
|
92
|
+
|
|
93
|
+
Balthon provides helpers for reply and inline keyboards.
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from balthon import BotClient, Button, KeyBoard, KeyBoardRow
|
|
97
|
+
|
|
98
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@bot.on_update(filters.command("menu"))
|
|
102
|
+
async def menu_handler(update):
|
|
103
|
+
keyboard = KeyBoard(
|
|
104
|
+
KeyBoardRow(Button("Option 1"), Button("Option 2")),
|
|
105
|
+
resize=True,
|
|
106
|
+
)
|
|
107
|
+
await bot.reply("Choose an option:", markup=keyboard)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
bot.run()
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For inline keyboards:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from balthon import InlineButton, InLineRow, InLineKeyboard
|
|
117
|
+
|
|
118
|
+
keyboard = InLineKeyboard(
|
|
119
|
+
InLineRow(InlineButton("Open Bale", url="https://bale.ai"))
|
|
120
|
+
)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## State management
|
|
124
|
+
|
|
125
|
+
Use `StateManager` to store temporary state for a user. States expire after the configured number of seconds.
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from balthon import BotClient, StateManager, filters
|
|
129
|
+
|
|
130
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
131
|
+
states = StateManager()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@bot.on_update(filters.command("begin"))
|
|
135
|
+
async def begin(update):
|
|
136
|
+
await states.set_state_for(update, "waiting_for_name", expire=60)
|
|
137
|
+
await bot.reply("What is your name?")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@bot.on_update(filters.at_state(states, "waiting_for_name"))
|
|
141
|
+
async def receive_name(update):
|
|
142
|
+
name = update.text
|
|
143
|
+
await bot.reply(f"Nice to meet you, {name}!")
|
|
144
|
+
await states.clear_state_for(update)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
bot.run()
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Webhook mode
|
|
151
|
+
|
|
152
|
+
Webhook mode uses FastAPI and Uvicorn. Install the optional dependencies first:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pip install "balthon[webhook]"
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Then start the client with `use_webhook=True`:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
from balthon import BotClient, filters
|
|
162
|
+
|
|
163
|
+
bot = BotClient("YOUR_BOT_TOKEN", use_webhook=True)
|
|
164
|
+
|
|
165
|
+
@bot.on_update(filters.text())
|
|
166
|
+
async def text_handler(update):
|
|
167
|
+
await bot.reply(update.text)
|
|
168
|
+
|
|
169
|
+
bot.run(host="0.0.0.0", port=8000, webhook_path="/webhook")
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
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.
|
|
173
|
+
|
|
174
|
+
## Async applications
|
|
175
|
+
|
|
176
|
+
If your application already runs an asyncio event loop, use `await bot.start()` instead of `bot.run()`:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
import asyncio
|
|
180
|
+
from balthon import BotClient
|
|
181
|
+
|
|
182
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
async def main():
|
|
186
|
+
await bot.start()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
asyncio.run(main())
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## API overview
|
|
193
|
+
|
|
194
|
+
The package exports the main client as `BotClient` (also available as `Client` and `BotConnect`), along with:
|
|
195
|
+
|
|
196
|
+
- `filters` and `Filter`
|
|
197
|
+
- `StateManager`
|
|
198
|
+
- Keyboard and button helpers
|
|
199
|
+
- Update, message, user, chat, media, and payment models
|
|
200
|
+
- Bale API response models
|
|
201
|
+
- `ActionsEnum`
|
|
202
|
+
- Balthon-specific exception classes
|
|
203
|
+
|
|
204
|
+
See the source code and exported names in [`balthon/__init__.py`](balthon/__init__.py) for the complete public API.
|
|
205
|
+
|
|
206
|
+
## Error handling
|
|
207
|
+
|
|
208
|
+
Balthon defines exception classes for common client and API errors, including:
|
|
209
|
+
|
|
210
|
+
- `BalthonError`
|
|
211
|
+
- `BaleError`
|
|
212
|
+
- `APIError`
|
|
213
|
+
- `NetworkError`
|
|
214
|
+
- `InvalidTokenError`
|
|
215
|
+
- `AuthenticationError`
|
|
216
|
+
- `RateLimitError`
|
|
217
|
+
- `InvalidRequestError`
|
|
218
|
+
- `UpdateError`
|
|
219
|
+
- `HandlerError`
|
|
220
|
+
- `FilterError`
|
|
221
|
+
- `StateError`
|
|
222
|
+
|
|
223
|
+
You can import these directly from `balthon`:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
from balthon import APIError, NetworkError
|
|
227
|
+
|
|
228
|
+
try:
|
|
229
|
+
# Call a Balthon API method here.
|
|
230
|
+
pass
|
|
231
|
+
except NetworkError as error:
|
|
232
|
+
print(f"Network error: {error}")
|
|
233
|
+
except APIError as error:
|
|
234
|
+
print(f"Bale API error: {error}")
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Links
|
|
238
|
+
|
|
239
|
+
- **GitHub:** https://github.com/saums-1391/balthon
|
|
240
|
+
- **Issues:** https://github.com/saums-1391/balthon/issues
|
|
241
|
+
- **Email:** balthonSupport@gmail.com
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: balthon
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An asynchronous Python library for building bots for the Bale messenger.
|
|
5
|
+
Author-email: SAUMS <balthonSupport@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/saums-1391/balthon
|
|
8
|
+
Project-URL: Repository, https://github.com/saums-1391/balthon
|
|
9
|
+
Project-URL: Issues, https://github.com/saums-1391/balthon/issues
|
|
10
|
+
Keywords: bale,bale-messenger,bot,bot-api,async,asyncio
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Framework :: AsyncIO
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Communications :: Chat
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
Requires-Dist: httpx>=0.24.0
|
|
27
|
+
Requires-Dist: fastapi>=0.100.0
|
|
28
|
+
Requires-Dist: uvicorn[standard]>=0.23.0
|
|
29
|
+
|
|
30
|
+
# Balthon
|
|
31
|
+
|
|
32
|
+
**Balthon** is an asynchronous Python library for building bots for the [Bale](https://bale.ai) messenger.
|
|
33
|
+
|
|
34
|
+
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.
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- Asynchronous API client built on `httpx`
|
|
39
|
+
- Long-polling support
|
|
40
|
+
- Optional built-in webhook server
|
|
41
|
+
- Update handlers and composable filters
|
|
42
|
+
- Message, user, chat, callback-query, and media models
|
|
43
|
+
- Reply and inline keyboard helpers
|
|
44
|
+
- Per-user update queues and rate limiting
|
|
45
|
+
- Lightweight state manager with expiration
|
|
46
|
+
- Bale API operations for messages, chats, files, members, stickers, and payments
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
- Python 3.9 or newer
|
|
51
|
+
- An active Bale bot token
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Install the package from PyPI:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install balthon
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
For webhook mode, install the optional dependencies:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install "balthon[webhook]"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Quick start
|
|
68
|
+
|
|
69
|
+
Create a bot in Bale and obtain its token. Then create a Python file:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from balthon import BotClient, filters
|
|
73
|
+
|
|
74
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@bot.on_update(filters.command("start"))
|
|
78
|
+
async def start_handler(update):
|
|
79
|
+
await bot.reply("Hello! Welcome to my bot.")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
bot.run()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Replace `YOUR_BOT_TOKEN` with your bot token.
|
|
86
|
+
|
|
87
|
+
## Handling updates
|
|
88
|
+
|
|
89
|
+
Register handlers with `on_update`. You can pass one or more filters to decide which updates a handler receives.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from balthon import BotClient, filters
|
|
93
|
+
|
|
94
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@bot.on_update(filters.text())
|
|
98
|
+
async def text_handler(update):
|
|
99
|
+
await bot.reply(f"You sent: {update.text}")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@bot.on_update(filters.photo())
|
|
103
|
+
async def photo_handler(update):
|
|
104
|
+
await bot.reply("I received a photo!")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
bot.run()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
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.
|
|
111
|
+
|
|
112
|
+
Filters can be combined with `&` (AND), `|` (OR), and `~` (NOT).
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
@bot.on_update(filters.private() & filters.text())
|
|
116
|
+
async def private_text_handler(update):
|
|
117
|
+
await bot.reply("This is a private text message.")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Keyboards
|
|
121
|
+
|
|
122
|
+
Balthon provides helpers for reply and inline keyboards.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from balthon import BotClient, Button, KeyBoard, KeyBoardRow
|
|
126
|
+
|
|
127
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@bot.on_update(filters.command("menu"))
|
|
131
|
+
async def menu_handler(update):
|
|
132
|
+
keyboard = KeyBoard(
|
|
133
|
+
KeyBoardRow(Button("Option 1"), Button("Option 2")),
|
|
134
|
+
resize=True,
|
|
135
|
+
)
|
|
136
|
+
await bot.reply("Choose an option:", markup=keyboard)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
bot.run()
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
For inline keyboards:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from balthon import InlineButton, InLineRow, InLineKeyboard
|
|
146
|
+
|
|
147
|
+
keyboard = InLineKeyboard(
|
|
148
|
+
InLineRow(InlineButton("Open Bale", url="https://bale.ai"))
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## State management
|
|
153
|
+
|
|
154
|
+
Use `StateManager` to store temporary state for a user. States expire after the configured number of seconds.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from balthon import BotClient, StateManager, filters
|
|
158
|
+
|
|
159
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
160
|
+
states = StateManager()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
@bot.on_update(filters.command("begin"))
|
|
164
|
+
async def begin(update):
|
|
165
|
+
await states.set_state_for(update, "waiting_for_name", expire=60)
|
|
166
|
+
await bot.reply("What is your name?")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@bot.on_update(filters.at_state(states, "waiting_for_name"))
|
|
170
|
+
async def receive_name(update):
|
|
171
|
+
name = update.text
|
|
172
|
+
await bot.reply(f"Nice to meet you, {name}!")
|
|
173
|
+
await states.clear_state_for(update)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
bot.run()
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Webhook mode
|
|
180
|
+
|
|
181
|
+
Webhook mode uses FastAPI and Uvicorn. Install the optional dependencies first:
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
pip install "balthon[webhook]"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Then start the client with `use_webhook=True`:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from balthon import BotClient, filters
|
|
191
|
+
|
|
192
|
+
bot = BotClient("YOUR_BOT_TOKEN", use_webhook=True)
|
|
193
|
+
|
|
194
|
+
@bot.on_update(filters.text())
|
|
195
|
+
async def text_handler(update):
|
|
196
|
+
await bot.reply(update.text)
|
|
197
|
+
|
|
198
|
+
bot.run(host="0.0.0.0", port=8000, webhook_path="/webhook")
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
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.
|
|
202
|
+
|
|
203
|
+
## Async applications
|
|
204
|
+
|
|
205
|
+
If your application already runs an asyncio event loop, use `await bot.start()` instead of `bot.run()`:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
import asyncio
|
|
209
|
+
from balthon import BotClient
|
|
210
|
+
|
|
211
|
+
bot = BotClient("YOUR_BOT_TOKEN")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
async def main():
|
|
215
|
+
await bot.start()
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
asyncio.run(main())
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## API overview
|
|
222
|
+
|
|
223
|
+
The package exports the main client as `BotClient` (also available as `Client` and `BotConnect`), along with:
|
|
224
|
+
|
|
225
|
+
- `filters` and `Filter`
|
|
226
|
+
- `StateManager`
|
|
227
|
+
- Keyboard and button helpers
|
|
228
|
+
- Update, message, user, chat, media, and payment models
|
|
229
|
+
- Bale API response models
|
|
230
|
+
- `ActionsEnum`
|
|
231
|
+
- Balthon-specific exception classes
|
|
232
|
+
|
|
233
|
+
See the source code and exported names in [`balthon/__init__.py`](balthon/__init__.py) for the complete public API.
|
|
234
|
+
|
|
235
|
+
## Error handling
|
|
236
|
+
|
|
237
|
+
Balthon defines exception classes for common client and API errors, including:
|
|
238
|
+
|
|
239
|
+
- `BalthonError`
|
|
240
|
+
- `BaleError`
|
|
241
|
+
- `APIError`
|
|
242
|
+
- `NetworkError`
|
|
243
|
+
- `InvalidTokenError`
|
|
244
|
+
- `AuthenticationError`
|
|
245
|
+
- `RateLimitError`
|
|
246
|
+
- `InvalidRequestError`
|
|
247
|
+
- `UpdateError`
|
|
248
|
+
- `HandlerError`
|
|
249
|
+
- `FilterError`
|
|
250
|
+
- `StateError`
|
|
251
|
+
|
|
252
|
+
You can import these directly from `balthon`:
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
from balthon import APIError, NetworkError
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
# Call a Balthon API method here.
|
|
259
|
+
pass
|
|
260
|
+
except NetworkError as error:
|
|
261
|
+
print(f"Network error: {error}")
|
|
262
|
+
except APIError as error:
|
|
263
|
+
print(f"Bale API error: {error}")
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Links
|
|
267
|
+
|
|
268
|
+
- **GitHub:** https://github.com/saums-1391/balthon
|
|
269
|
+
- **Issues:** https://github.com/saums-1391/balthon/issues
|
|
270
|
+
- **Email:** balthonSupport@gmail.com
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "balthon"
|
|
7
|
+
version = '0.1.0'
|
|
8
|
+
description = "An asynchronous Python library for building bots for the Bale messenger."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "SAUMS", email = "balthonSupport@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["bale", "bale-messenger", "bot", "bot-api", "async", "asyncio"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Framework :: AsyncIO",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.9",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Topic :: Communications :: Chat",
|
|
29
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
30
|
+
]
|
|
31
|
+
dependencies = [
|
|
32
|
+
"httpx>=0.24.0",
|
|
33
|
+
"fastapi>=0.100.0",
|
|
34
|
+
"uvicorn[standard]>=0.23.0",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/saums-1391/balthon"
|
|
39
|
+
Repository = "https://github.com/saums-1391/balthon"
|
|
40
|
+
Issues = "https://github.com/saums-1391/balthon/issues"
|
|
41
|
+
|
|
42
|
+
[tool.setuptools]
|
|
43
|
+
include-package-data = true
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.packages.find]
|
|
46
|
+
where = ["."]
|
|
47
|
+
include = ["balthon*"]
|
balthon-0.1.0/setup.cfg
ADDED