Skip to main content

Forms for aiogram

Project description

aiogram-forms

PyPI GitHub Project status Project code coverage PyPI - Downloads

Introduction

aiogram-forms is an addition for aiogram which allows you to create different forms and process user input step by step easily.

Documentation

Documentation can be found here.

Installation

pip install aiogram-forms

Usage

Create form you need by subclassing aiogram_forms.forms.Form. Fields can be added from aiogram_forms.forms.fields subpackage.

from aiogram_forms import dispatcher
from aiogram_forms.forms import Form, fields, FormsManager
from aiogram_forms.errors import ValidationError

def validate_username_format(value: str):
    """Validate username starts with leading @."""
    if not value.startswith('@'):
        raise ValidationError('Username should starts with "@".', code='username_prefix')

@dispatcher.register('test-form')
class TestForm(Form):
    username = fields.TextField(
        'Username', min_length=4, validators=[validate_username_format],
        error_messages={'min_length': 'Username must contain at least 4 characters!'}
    )
    email = fields.EmailField('Email', help_text='We will send confirmation code.')
    phone = fields.PhoneNumberField('Phone number', share_contact=True)
    language = fields.ChoiceField('Language', choices=(
        ('English', 'en'),
        ('Russian', 'ru')
    ))

    @classmethod
    async def callback(cls, message: types.Message, forms: FormsManager, **data) -> None:
        data = await forms.get_data('test-form')  # Get form data from state
        await message.answer(
            text=f'Thank you, {data["username"]}!',
            reply_markup=types.ReplyKeyboardRemove()  # Use this for reset if last field contains keyboard
        )

router = Router()

@router.message(Command(commands=['start']))
async def command_start(message: types.Message, forms: FormsManager) -> None:
    await forms.show('test-form')  # Start form processing

async def main():
    dp = Dispatcher()
    dp.include_router(router)

    dispatcher.attach(dp)  # Attach aiogram to forms dispatcher 

    bot = Bot(...)
    await dp.start_polling(bot)

History

All notable changes to this project will be documented in CHANGELOG file.

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_forms-1.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

aiogram_forms-1.1.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_forms-1.1.0.tar.gz.

File metadata

  • Download URL: aiogram_forms-1.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.4 Darwin/22.3.0

File hashes

Hashes for aiogram_forms-1.1.0.tar.gz
Algorithm Hash digest
SHA256 86853662955c532fda42fe796a2fcfecf2439afd427e5899cb4587f6754bf595
MD5 ace4e93d92b16ec028df9d1d0c8b9aa4
BLAKE2b-256 6012f49c95efb1dbadeae5a8700f5603943febbbcac22e76e1fb58ff63f1f940

See more details on using hashes here.

File details

Details for the file aiogram_forms-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: aiogram_forms-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.4 Darwin/22.3.0

File hashes

Hashes for aiogram_forms-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f004b0b89c1f97c4976b0146ffac82879aef19bb155b11215cafabf9f47d6a6
MD5 54fc0ebf909f1ca3f9c5b129e47b9f99
BLAKE2b-256 ade0d69ff684618afe334cc750089aa14ee8919e9de27f51cb8e60d74726061b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page