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.
  • Custom Storage: Provides BaseStorage class for extending and managing persistence.

Installation

Run the following in the package directory:

pip install .

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.0.tar.gz (6.2 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.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiogram_input-2.0.0.tar.gz
  • Upload date:
  • Size: 6.2 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.0.tar.gz
Algorithm Hash digest
SHA256 26f90d1d6a73f3e1674514fd02e575bcc3b217ce97ef76059e61577af2e0e6a8
MD5 0ba91f1a8acad3a1622414845cda1619
BLAKE2b-256 5256b8f463d8b5d4397118699ecab49a785ed2551870209e8c567166e31282ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiogram_input-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe892c0bc4247dc76a76fae38a4e71565ba257fce47df65621bba9a1b3ea8440
MD5 96358ff350d4358e4eeeeaa50366f4bd
BLAKE2b-256 a9b3f9687e85add5d7b762a14a09322aa96b09968f8919f9a6a1646b66cfd9dd

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