Skip to main content

A modular, multi-client library for aiogram to handle user response waiting in Telegram bots.

Project description

aiogram_input

A lightweight and flexible library for aiogram to simplify waiting for user responses in Telegram bots. With aiogram_input, you can easily implement interactive flows where your bot waits for a specific user's reply in a specific chat, with full support for multiple bot instances (multi-client) to avoid conflicts.

Key Features

  • Simple API: Use the input method to wait for user messages in a clean and intuitive way.
  • Multi-Client Support: Each bot instance has its own isolated state, ensuring no conflicts when running multiple bots.
  • Timeout Handling: Built-in timeout support to gracefully handle unresponsive users.
  • Logging Support: Integrated logging for easier debugging and monitoring.
  • Thread-Safe & Race Condition Safe: Designed to prevent race conditions in concurrent scenarios.
  • Published on PyPI: Easily installable via pip install aiogram-input.

Installation

You can install the package directly from PyPI:

pip install aiogram-input

Or install from GitHub:

pip install git+https://github.com/mamahoos/aiogram-input.git

Usage Example: Simple Registration with Email Validation

import re
from aiogram import Bot, Dispatcher, Router, F
from aiogram.types import Message
from aiogram_input import InputManager
from aiogram.enums import ChatType

TOKEN = "YOUR_BOT_TOKEN"

bot   = Bot(TOKEN)
dp    = Dispatcher()
asker = InputManager()
dp.include_router(asker.router)

email_validator = re.compile(r"^[\w.-]+@[\w.-]+\.\w+$")

router = Router(name="main")

@router.message(F.chat.type == ChatType.PRIVATE)
async def start_registration(message: Message):
    await message.answer("Welcome! Please enter your email:")
    
    response = await asker.input(message.chat.id, timeout=30)
    if response is None:
        return await message.answer("⏳ Timeout! Try again.")
    
    email = response.text
    if email is None or not email_validator.match(email):
        return await message.answer("❌ Invalid email format.")
    
    await message.answer(f"✅ Email registered: {email}")
    # Code
    ...

dp.include_router(router)

if __name__ == "__main__":
    import asyncio
    asyncio.run(dp.start_polling(bot))

Usage Example: Filtered Message from Admin in a Group

from aiogram import Bot, Dispatcher, Router
from aiogram.types import Message
from aiogram.filters import Filter, Command
from aiogram_input import InputManager

TOKEN = "YOUR_BOT_TOKEN"
ADMIN_ID = 123456789  # Replace with your admin's Telegram ID

bot   = Bot(TOKEN)
dp    = Dispatcher()
asker = InputManager()
dp.include_router(asker.router)

router = Router(name="admin")

class AdminFilter(Filter):
    async def __call__(self, message: Message) -> bool:
        return message.from_user and message.from_user.id == ADMIN_ID

@router.message(AdminFilter(), Command('admin'))
async def admin_handler(message: Message):
    await message.answer("👑 Admin command detected!")
    response = await asker.input(message.chat.id, timeout=20, filter=AdminFilter())
    if response:
        await message.answer(f"Admin replied: {response.text}")
        # Code
        ...
    ...

dp.include_router(router)

if __name__ == "__main__":
    import asyncio
    asyncio.run(dp.start_polling(bot))

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

aiogram_input-2.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

aiogram_input-2.0.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_input-2.0.1.tar.gz.

File metadata

  • Download URL: aiogram_input-2.0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for aiogram_input-2.0.1.tar.gz
Algorithm Hash digest
SHA256 ff7889154d33c3d36a582d0f6ca67140a5683b9cae9889ec121da683f2644fef
MD5 0094e1b2f991a162547a76a11980eb6b
BLAKE2b-256 60b08f8f18b258b8c9b70ed3d289c4fe9784ac8ad426429da7bde7ed25544a9c

See more details on using hashes here.

File details

Details for the file aiogram_input-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiogram_input-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for aiogram_input-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0dc8dbefa8325901f6ae87464f13958370d1696929c3ac3c512bb166a50adde9
MD5 343f8b9a9b44ad0a02a4136b0212643c
BLAKE2b-256 e06fac671e3e3c32f3851ba8ab83986c39a8598a4d556343d330861b625d1fd8

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