A library for building Telegram bots using routing similar to web pages.
Project description
Telethon Router
A library for building Telegram bots using routing similar to web pages. It allows you to organize your bot's logic into handlers and routes, making it easy to manage complex interactions.
Features
Routing: Define routes for callback queries, similar to web frameworks.
Query Parameters: Pass parameters to handlers via callback data.
Modular Handlers: Organize your bot's logic into reusable handlers.
Easy Setup: Simple and intuitive API for setting up your bot.
Example Usage
Below is a simple example of how to use TelegramRouter to create a Telegram bot with routing and query parameters. Code Example python Copy
from telethon import TelegramClient, events, Button
from telethon_router.navigator import Navigator
from telethon_router.types.handler import Handler
### Initialize the Telegram client
bot = TelegramClient("bot", "YOUR_API_ID", "YOUR_API_HASH")
navigator = Navigator(bot)
# Define a message handler to display buttons
@bot.on(events.NewMessage(pattern='(?i)hi|hello'))
async def handler(event: events.NewMessage.Event):
buttons = [
[Button.inline("Say Hi!", b"profile/index")],
[Button.inline("Friend 1", b"profile/friends?friend_id=1")],
[Button.inline("Friend 1 Balance", b"profile/friends/balance?friend_id=1")],
]
await event.respond("Choose an option:", buttons=buttons)
# Define a handler for profile-related commands
class Profile(Handler):
path = "profile"
async def index(self, event: events.CallbackQuery.Event) -> None:
await event.answer("Index clicked!", alert=True)
async def friends(self, event: events.CallbackQuery.Event, **kwargs) -> None:
friend_id = kwargs.get("friend_id", "Unknown")
await event.answer(f"Friend clicked! Friend ID: {friend_id}", alert=True)
async def friends_balance(self, event: events.CallbackQuery.Event, **kwargs) -> None:
friend_id = kwargs.get("friend_id", "Unknown")
await event.answer(f"Friend balance clicked! Friend ID: {friend_id}", alert=True)
# Add the Profile handler to the Navigator
navigator.add(Profile())
navigator.setup()
# Start the bot
bot.start(bot_token="YOUR_BOT_TOKEN")
bot.run_until_disconnected()
How It Works
- Routing
The Navigator class handles routing based on the callback_data of inline buttons. Each route corresponds to a method in a handler.
Example Routes:
profile/index: Routes to the index method.
profile/friends?friend_id=1: Routes to the friends method with friend_id=1.
profile/friends/balance?friend_id=1: Routes to the friends_balance method with friend_id=1.
- Query Parameters
Query parameters are passed to the handler methods as keyword arguments (**kwargs). This allows you to dynamically pass data to your handlers.
Example:
profile/friends?friend_id=1 passes friend_id=1 to the friends method.
profile/friends/balance?friend_id=1 passes friend_id=1 to the friends_balance method.
- Buttons
Buttons are created using Button.inline. The callback_data of each button follows the routing pattern.
Example Buttons:
Button.inline("Say Hi!", b"profile/index"): Creates a button that routes to the index method.
Button.inline("Friend 1", b"profile/friends?friend_id=1"): Creates a button that routes to the friends method with friend_id=1.
- Handlers
Handlers are classes that define methods to handle specific routes. Each method corresponds to a route and processes the incoming callback.
Example Methods:
index: Handles the profile/index route.
friends: Handles the profile/friends route and accepts friend_id as a parameter.
friends_balance: Handles the profile/friends/balance route and accepts friend_id as a parameter.
Example Interaction
Send /start or hi to the bot:
The bot responds with a list of buttons:
Say Hi!: Calls the index method.
Friend 1: Calls the friends method with friend_id=1.
Friend 1 Balance: Calls the friends_balance method with friend_id=1.
Click a button:
The bot processes the callback and displays the result.
Requirements
Python 3.13+
Telethon
Install the required dependencies using pip:
pip install telethon
Contribution
Contributions are welcome! Feel free to open issues and PRs. License
This project is licensed under the MIT License.
Detailed Explanation
Navigator
The Navigator class is the core of the library. It manages routing for callback queries and sets up event listeners for each handler.
add(handler): Registers a handler with the navigator.
setup(): Configures event listeners for all registered handlers.
Handler
Handlers are classes that define methods to handle specific routes. Each handler has a path attribute that defines its base route.
Example:
A handler with path = "profile" will handle routes starting with profile/.
Buttons
Buttons are created using Button.inline. The callback_data of each button follows the routing pattern.
Example:
Button.inline("Say Hi!", b"profile/index"): Creates a button that routes to the index method.
Query Parameters
Query parameters are passed to the handler methods as keyword arguments (**kwargs). This allows you to dynamically pass data to your handlers.
Example:
profile/friends?friend_id=1 passes friend_id=1 to the friends method.
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 telethon_router-0.1.0.tar.gz.
File metadata
- Download URL: telethon_router-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f9f7bad459841f39236b026ca06fe9e668b3498061df1d5c536358ccea4edb2
|
|
| MD5 |
d79a866bfcbfdeebb051bf8f662af353
|
|
| BLAKE2b-256 |
a1bb03025a47369620a3b7b1610dd2a718da49215f5dd3052421ff9df03e7fb5
|
File details
Details for the file telethon_router-0.1.0-py3-none-any.whl.
File metadata
- Download URL: telethon_router-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb49fa4c31b74be70548b1e2740bea239a183cd2c69c5c4f9f9e09ed1b9ad637
|
|
| MD5 |
b6e99045991fe50e06c3ee067297ee03
|
|
| BLAKE2b-256 |
33fad45f99c6cb8a99bfe22d350e421c73f717c46f716fc569e9ccc585b88334
|