https://github.com/13g10n/aiogram-forms
An addition for `aiogram` which allows you to create different forms and process user input step by step easily.
https://github.com/13g10n/aiogram-forms
aiogram asyncio bot forms python
Last synced: 5 months ago
JSON representation
An addition for `aiogram` which allows you to create different forms and process user input step by step easily.
- Host: GitHub
- URL: https://github.com/13g10n/aiogram-forms
- Owner: 13g10n
- License: mit
- Created: 2021-05-24T21:49:19.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-31T17:41:54.000Z (almost 3 years ago)
- Last Synced: 2025-12-22T01:28:00.324Z (6 months ago)
- Topics: aiogram, asyncio, bot, forms, python
- Language: Python
- Homepage: https://aiogram-forms.13g10n.com/en
- Size: 399 KB
- Stars: 28
- Watchers: 2
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# aiogram-forms





## 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](https://13g10n.com/en/docs/aiogram-forms).
## Installation
```bash
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.
```python
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](CHANGELOG.md) file.