diff --git a/aiogram/contrib/middlewares/bot.py b/aiogram/contrib/middlewares/bot.py new file mode 100644 index 00000000..106355cd --- /dev/null +++ b/aiogram/contrib/middlewares/bot.py @@ -0,0 +1,25 @@ +from aiogram.dispatcher.middlewares import BaseMiddleware + + +class BotMiddleware(BaseMiddleware): + def __init__(self, context=None): + super(BotMiddleware, self).__init__() + + if context is None: + context = {} + self.context = context + + def update_data(self, data): + dp = self.manager.dispatcher + data.update( + bot=dp.bot, + dispatcher=dp, + loop=dp.loop + ) + if self.context: + data.update(self.context) + + async def trigger(self, action, args): + if 'error' not in action and action.startswith('pre_process_'): + self.update_data(args[-1]) + return True