Implement bot middleware.

This commit is contained in:
Alex Root Junior 2018-07-01 01:48:24 +03:00
parent 3d5b461409
commit 3e30f29aba

View file

@ -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