Skip to main content

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

  1. 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.
  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.
  1. 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.
  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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telethon_router-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

telethon_router-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file telethon_router-0.1.1.tar.gz.

File metadata

  • Download URL: telethon_router-0.1.1.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

Hashes for telethon_router-0.1.1.tar.gz
Algorithm Hash digest
SHA256 68046b0cf9e663c6d904df6ea94da2f15e80d12b236f495c60d6cb9f51d89413
MD5 98b2c6bbf6ed912cabedd5854a96921a
BLAKE2b-256 81ab37bbd684ff80f313887204821939677c1a815017d852949b3968b4a22df9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for telethon_router-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef6707441a6ecec1393a9b97539cab18dbffcd45e89493f92d1eae3bae513e36
MD5 12a98f81331b56c37a7e971518f25688
BLAKE2b-256 758ab58ff683c64e5ff90cd0f4a87c72bd5f6de0222ed8922b8df2aa623c1a45

See more details on using hashes here.

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