Welcome to aiogram_oop_framework’s documentation!

Installation

install with:

pip install aiogram_oop_framework

uninstall with:

pip uninstall aiogram_oop_framework

Quick-Start

  1. Create project
python -m aiogram_oop_framework startproject mybot

aiogram_oop_framework will create directory with your project.

Create projects in root directory, otherwise you may get in some bugs (but if you are ready to fix it, than it is even better for this framework’s future xD)

2) Create your views in mybot.views directory You can create views anywhere inside mybot (but not outside), but preferably in mybot.views or it’s subdirectories Your view must be inherited from any View class, for example: MessageView and have classmethod .execute() which is used to run code. And it also may have additional classmethods with any name, with filters (see additional-filters for more info about filters) Code example:

class Example(MessageView):

@classmethod
async def execute(cls, m: types.Message, state: FSMContext = None, **kwargs):
    await m.answer('Hello, World!')

3) If you had created views not in mybot.views (if you created views in it’s subdirectories, this is also for you): In settings.py change project structure by writing struc.include(<path to directory containing your views> with dots as separators) before pr.structure = struc For example:

pr: Project = Project(PROJECT_NAME, PATH)
struc: ProjectStructure = ProjectStructure(pr)
struc.include('views')
struc.include('views.messages.user_commands')
struc.include('views.callbacks')
pr.structure = struc

PROJECT: Project = pr
  1. Write your settings in settings.py (see detailed explanation of all settings in Settings)
  2. You can now start polling with python -m mybot.manage start-polling or in code:
from aiogram import executor

from mybot.manage import initialize_project


if __name__ == '__main__':
    dp, bot = initialize_project()
    executor.start_polling(dp)

OR

from aiogram import executor, Dispatcher, Bot

from mybot.manage import initialize_project
from mybot.settings import TELEGRAM_BOT_TOKEN


if __name__ == '__main__':
    bot: Bot = Bot(TELEGRAM_BOT_TOKEN)
    dp: Dispatcher = Dispatcher(bot)
    initialize_project(dp, bot)
    executor.start_polling(dp)

Views documenation

Message Views

class aiogram_oop_framework.views.message.MessageView

View for updates of type “message”

custom_filters

Custom filters (for ex.: [lambda m: m.reply_to_message]).

Type:list
commands

List of commands (for ex.: [‘start’, ‘help’]), defaults to None

Type:list
regexp

Regexp string for matching message text/caption (for ex.: r’^hello$’), defaults to None

Type:str
content_types

List of content_type’s of message (for ex.: [‘text’, ‘photo’]), defaults to None

Type:list
state

Function, which returns a State object of aiogram or “*”, defaults to lambda: None

Type:Callable
run_task

Run callback in task (no wait results), defaults to None

Type:bool
register_kwargs

Kwargs, which you would add in @dp.message_handler in fresh aiogram, defaults to None

Type:dict
index

in which order to register the view, defaults to None

Type:int
auto_register

set to False if you don’t want re register the view autcomatically, ignored, if AUTO_REGISTER_VIEWS in settings.py is set to False, defaults to True

Type:bool

Make sure you don’t want to use a more high-level view like aiogram_oop_framework.views.custom_views.command.CommandView or aiogram_oop_framework.views.content_types_views.text.TextView instead.

You may found more info about custom_filters, commands, regexp, content_types, state and run_task attributes attributes in aiogram’s docs on Dispatcher.message_handler or you may not, depends on aiogram’s docs.

classmethod register(dp: aiogram.dispatcher.dispatcher.Dispatcher)

method to register the handler, normally called automatically in manage.initialize_project()

Parameters:dp (Dispatcher) – in which dispatcher to register the handler
class aiogram_oop_framework.views.edited_message_view.EditedMessageView
classmethod register(dp: aiogram.dispatcher.dispatcher.Dispatcher)

method to register the handler, normally called automatically in manage.initialize_project()

Parameters:dp (Dispatcher) – in which dispatcher to register the handler
class aiogram_oop_framework.views.channel_post_view.ChannelPostView
classmethod register(dp: aiogram.dispatcher.dispatcher.Dispatcher)

method to register the handler, normally called automatically in manage.initialize_project()

Parameters:dp (Dispatcher) – in which dispatcher to register the handler
class aiogram_oop_framework.views.edited_channel_post_view.EditedChannelPostView
classmethod register(dp: aiogram.dispatcher.dispatcher.Dispatcher)

method to register the handler, normally called automatically in manage.initialize_project()

Parameters:dp (Dispatcher) – in which dispatcher to register the handler

Views by content_types

class aiogram_oop_framework.views.content_types_views.animation.AnimationView

May be same as aiogram_oop_framework.views.message.MessageView, or aiogram_oop_framework.views.edited_message_view.EditedMessageView, or aiogram_oop_framework.views.channel_post_view.ChannelPostView, or aiogram_oop_framework.views.edited_channel_post_view.EditedChannelPostView, dependently on update_type field, with difference that content_types field defaults to [ContentType.ANIMATION], and it has one more field:

update_type

Update type, must be one of: ‘message’, ‘edited_message’, ‘channel_post’, ‘edited_channel_post’, defaults to ‘message’s

Type:str
class aiogram_oop_framework.views.content_types_views.audio.AudioView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.AUDIO]

class aiogram_oop_framework.views.content_types_views.connected_website.ConnectedWebsiteView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.CONNECTED_WEBSITE]

class aiogram_oop_framework.views.content_types_views.contact.ContactView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.CONTACT]

class aiogram_oop_framework.views.content_types_views.delete_chat_photo.DeleteChatPhotoView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.DELETE_CHAT_PHOTO]

class aiogram_oop_framework.views.content_types_views.dice.DiceView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.DICE]

class aiogram_oop_framework.views.content_types_views.document.DocumentView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.DOCUMENT]

class aiogram_oop_framework.views.content_types_views.game.GameView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.GAME]

class aiogram_oop_framework.views.content_types_views.group_chat_created.GroupChatCreatedView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.GROUP_CHAT_CREATED]

class aiogram_oop_framework.views.content_types_views.invoice.InvoiceView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.INVOICE]

class aiogram_oop_framework.views.content_types_views.left_chat_member.LeftChatMemberView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.LEFT_CHAT_MEMBER]

class aiogram_oop_framework.views.content_types_views.location.LocationView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.LOCATION]

class aiogram_oop_framework.views.content_types_views.migrate_from_chat_id.MigrateFromChatIdView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.MIGRATE_FROM_CHAT_ID]

class aiogram_oop_framework.views.content_types_views.migrate_to_chat_id.MigrateToChatIdView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.MIGRATE_TO_CHAT_ID]

class aiogram_oop_framework.views.content_types_views.new_chat_members.NewChatMembersView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.NEW_CHAT_MEMBERS]

class aiogram_oop_framework.views.content_types_views.new_chat_photo.NewChatPhotoView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.NEW_CHAT_PHOTO]

class aiogram_oop_framework.views.content_types_views.new_chat_title.NewChatTitleView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.NEW_CHAT_TITLE]

class aiogram_oop_framework.views.content_types_views.passport_data.PassportDataView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.PASSPORT_DATA]

class aiogram_oop_framework.views.content_types_views.photo.PhotoView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.PHOTO]

class aiogram_oop_framework.views.content_types_views.pinned_message.PinnedMessageView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.PINNED_MESSAGE]

class aiogram_oop_framework.views.content_types_views.poll.PollMessageView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.POLL]

class aiogram_oop_framework.views.content_types_views.sticker.StickerView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.STICKER]

class aiogram_oop_framework.views.content_types_views.successful_payment.SuccessfulPaymentView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.SUCCESSFUL_PAYMENT]

class aiogram_oop_framework.views.content_types_views.text.TextView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.TEXT]

class aiogram_oop_framework.views.content_types_views.venue.VenueView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.VENUE]

class aiogram_oop_framework.views.content_types_views.video.VideoView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.VIDEO]

class aiogram_oop_framework.views.content_types_views.video_note.VideoNoteView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.VIDEO_NOTE]

class aiogram_oop_framework.views.content_types_views.voice.VoiceView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to [ContentType.VOICE]

class aiogram_oop_framework.views.content_types_views.any_content_type_message.AnyContentTypeMessageView

Same as aiogram_oop_framework.views.content_types_views.animation.AnimationView, but content_types defaults to ContentType.all()

Other update types’ views

class aiogram_oop_framework.views.inline_query.InlineQueryView

View for updates of type “message”

custom_filters

Custom filters (for ex.: [lambda m: m.reply_to_message]).

Type:list
state

Function, which returns a State object of aiogram or “*”

Type:Callable
run_task

Run callback in task (no wait results)

Type:bool
register_kwargs

Kwargs, which you would add in @dp.message_handler in fresh aiogram

Type:dict

You may found more info about attributes in aiogram’s docs on Dispatcher.message_handler or you may not, depends on aiogram’s docs.

class aiogram_oop_framework.views.callback_query.CallbackQueryView
class aiogram_oop_framework.views.poll.PollView
class aiogram_oop_framework.views.poll_answer.PollAnswerView
class aiogram_oop_framework.views.shipping_query.ShippingQueryView

Custom views

class aiogram_oop_framework.views.custom_views.command.CommandView

May be same as aiogram_oop_framework.views.message.MessageView, or aiogram_oop_framework.views.edited_message_view.EditedMessageView, or aiogram_oop_framework.views.channel_post_view.ChannelPostView, or aiogram_oop_framework.views.edited_channel_post_view.EditedChannelPostView, dependently on update_type field, with difference that content_types field defaults to [ContentType.TEXT], and it has some differences, look down:

update_type

Update type, must be one of: ‘message’, ‘edited_message’, ‘channel_post’, ‘edited_channel_post’, defaults to ‘message’

Type:str
append_commands

If you set commands manually, does it need to append to the commands the default command or not

Type:bool

while registering looks at your view’s name and creates a default command with it (Start -> [‘start’])

classmethod register(dp: aiogram.dispatcher.dispatcher.Dispatcher)

Settings

Your project settings should be in projects_name/settings.py

PATH: path to folder in which project folder is PROJECT_NAME: name of the folder of the project

PROJECT: aiogram_oop_framework.core.project.Project instance, you don’t need to change it, only it’s structure field (see in quick-start)

AUTO_REGISTER_VIEWS: does the framework need to register your views automatically or you will do it yourself

TELEGRAM_BOT_TOKEN: token of your bot

MIDDLEWARES: list of your middlewares (from aiogram), must be classes, not instances

BOT_STORAGE: storage of the bot, you may look closer about that in aiogram’s docs, or you may not, depends in aiogram’s docs
MEMORY_STORAGE: DEPRECATED IN 0.2.dev3, will be removed, old name of BOT_STORAGE

PARSE_MODE: default parse mode for your bot

Additional Filters

Different code execution dependently on chat type

DEPRECATED

You can add special methods to your views to execute different code dependently on chat type by writing execute_in_<chat_type>:

from aiogram import types
from aiogram.dispatcher import FSMContext

from aiogram_oop_framework.views import MessageView


class Example(MessageView):

    @classmethod
    async def execute(cls, m: types.Message, state: FSMContext = None, **kwargs):
        """this will be executed if chat type is not private"""
        await m.answer('undef')

    @classmethod
    async def execute_in_private(cls, m: types.Message, state: FSMContext = None, **kwargs):
        """execute in private chat"""
        await m.answer('gg private brr')

Surely, it only works in views for objects, which may give access to chat like MessageView and CallbackQueryView, but NOT InlineQueryView

filter_execute decorator for methods

You can add to your views additional filters for calling different methods dependently on conditions. filter_execute accepts all aiogram filters with content_types set to ContentType.all() by default

Example:

from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.types.chat_member import ChatMemberStatus

from aiogram_oop_framework.views import MessageView
from aiogram_oop_framework.filters import filter_execute


class Example(MessageView):

    @classmethod
    async def execute(cls, m: types.Message, state: FSMContext = None, **kwargs):
        """this will be executed if chat type is not private"""
        await m.answer('undef')

    @classmethod
    @filter_execute(chat_member_status=ChatMemberStatus.ADMINISTRATOR)
    async def execute_for_admins(cls, m: types.Message, state: FSMContext = None, **kwargs):
        """execute if sender is administrator"""
        await m.answer('gg ADMIN brr')

aiogram_oop_framework builtin filters list:

chat_member_status
status of from_user member (it calls bot.get_chat_member to find out the status!)
chat_type
type of chat where update occurred
entities
if message.entities/message.caption_entities/poll.explanation_entities contains entities with specified types
poll_type
type of the Poll
dice_emoji
dice’s emoji
func
|a callable, which returns boolean value |DEPRECATED in 0.2.dev4, because of starting using aiogram filters, so you can simply put the func in args (ex.: filter_execute(foo))

While func may only be a function or lambda, other filter types accept much more values:

  • str
  • aiogram.util.helper.Item (for ex.: ChatType.PRIVATE)
  • list of the above
  • a function, which accepts a TelegramObject as parameter and returns any of the above
Note!
filter_execute works with execute_in_<chat_type> with no problem, if it’s given chat_type in filter_execute, then chat_type from method’s name is omitted

Indices and tables