mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-13 10:26:53 +00:00
Implement bot middleware.
This commit is contained in:
parent
3d5b461409
commit
3e30f29aba
1 changed files with 25 additions and 0 deletions
25
aiogram/contrib/middlewares/bot.py
Normal file
25
aiogram/contrib/middlewares/bot.py
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue