Allow to get current handler

This commit is contained in:
Alex Root Junior 2018-09-21 22:34:59 +03:00
parent 5aff7f7d35
commit 5be3f9d00b
2 changed files with 10 additions and 7 deletions

View file

@ -3,8 +3,9 @@ import asyncio
from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.redis import RedisStorage2
from aiogram.dispatcher import DEFAULT_RATE_LIMIT
from aiogram.dispatcher.handler import CancelHandler
from aiogram.dispatcher.handler import CancelHandler, current_handler
from aiogram.dispatcher.middlewares import BaseMiddleware
from aiogram.utils.exceptions import Throttled
TOKEN = 'BOT TOKEN HERE'
@ -52,11 +53,10 @@ class ThrottlingMiddleware(BaseMiddleware):
:param message:
"""
# Get current handler
# handler = context.get_value('handler')
handler = current_handler.get()
# Get dispatcher from context
dispatcher = Dispatcher.current()
# If handler was configured, get rate limit and key from handler
if handler:
limit = getattr(handler, 'throttling_rate_limit', self.rate_limit)
@ -82,7 +82,7 @@ class ThrottlingMiddleware(BaseMiddleware):
:param message:
:param throttled:
"""
# handler = context.get_value('handler')
handler = current_handler.get()
dispatcher = Dispatcher.current()
if handler:
key = getattr(handler, 'throttling_key', f"{self.prefix}_{handler.__name__}")