o
    HEDi                     @   sN  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) dd	l*m+Z+ d
dl,m-Z- d
dl.m/Z/ d
dl0m1Z1m2Z2 d
dl3m4Z4m5Z5 e6e7Z8dZ9dddZ:G dd de5e4Z;dS )    N)sentinel)renamed_argument   )CommandContentTypeFilterExceptionsFilterFiltersFactoryHashTagRegexpRegexpCommandsFilterStateFilterTextIDFilterAdminFilterIsReplyFilterForwardedMessageFilterIsSenderContactChatTypeFilterMediaGroupFilterAbstractFilter)Handler)MiddlewareManager)BaseStorageDELTADisabledStorageEXCEEDED_COUNT
FSMContext	LAST_CALL
RATE_LIMITRESULT)BaseResponse   )types)Bot)TelegramAPIError	Throttled)ContextInstanceMixin	DataMixin皙?xasyncio.AbstractEventLoopc                 C   s*   t | tjsJ dtjdt| d S )Nz#Loop must be the implementation of z, not )
isinstanceasyncioAbstractEventLooptype)r)    r/   l/var/www/www-root/data/www/ovozai.pdev.uz/venv/lib/python3.10/site-packages/aiogram/dispatcher/dispatcher.py_ensure_loop   s   
r1   c                   @   s  e Zd ZdZdddeddfdeje defddZ	e
dejej fd	d
Ze
dddZdd Zdd Zdd ZddefddZdejfddZddefddZdd Z		 				!	dded"ed#ejeje  fd$d%Zddefd&d'Zd(d) Zd*d+ Zd,d- Zdddddd.d/d0Z dddddd.d1d2Z!dddddd.d3d4Z"dddddd.d5d6Z#dddddd.d7d8Z$dddddd.d9d:Z%dddddd.d;d<Z&dddddd.d=d>Z'ddd?d@dAZ(ddd?dBdCZ)ddd?dDdEZ*ddd?dFdGZ+ddd?dHdIZ,ddd?dJdKZ-ddd?dLdMZ.ddd?dNdOZ/ddd?dPdQZ0ddd?dRdSZ1ddTdUdVZ2ddTdWdXZ3ddTdYdZZ4ddTd[d\Z5ddTd]ej6d^eje ddfd_d`Z7ddTdadbZ8ddTd]ej6d^eje ddfdcddZ9ddTdedfZ:ddTd]ej6d^eje ddfdgdhZ;ddTdidjZ<dddkdldmZ=dddkdndoZ>dddpdqej?eedf drej?eedf de@fdsdtZAeBdrdudvdwdxeBdqdydvdzdxddddd{defd|d}ZCeBdrdudvdwdxeBdqdydvdzdxdd~dZDeBdrdudvdwdxeBdqdydvdzdxdddZEdd ZFddeGfddZH			ddejej6 fddZI			dd]ej?ej6eJf dejej6 dejejeK  dejejLeK  fddZMd]ej?ej6eJf fddZNdd ZOdS )
Dispatcherz
    Simple Updates dispatcher

    It will process incoming updates: messages, edited messages, channel posts, edited channel posts,
    inline queries, chosen inline results, callback queries, shipping queries, pre-checkout queries.
    NFstoragerun_tasks_by_defaultc                 C   s  t |tstdt|j d|d u rt }|d u rt| }|| _|| _|| _	|| _
|| _|| _|| _t| dd| _t| dd| _t| dd| _t| dd| _t| dd| _t| d	d| _t| d
d| _t| dd| _t| dd| _t| dd| _t| dd| _t| dd| _t| dd| _t| dd| _t| dd| _t| ddd| _t| | _ | j!| j" d| _#d| _$d | _%| &  d S )Nz0Argument 'bot' must be an instance of Bot, not ''update)middleware_keymessageedited_messagechannel_postedited_channel_postinline_querychosen_inline_resultcallback_queryshipping_querypre_checkout_querypollpoll_answermy_chat_memberchat_memberchat_join_requestFerror)oncer7   T)'r+   r#   	TypeErrorr.   __name__r   r   bot
_main_loopr3   r4   throttling_rate_limitno_throttle_errorfilters_factoryr   updates_handlermessage_handlersedited_message_handlerschannel_post_handlersedited_channel_post_handlersinline_query_handlerschosen_inline_result_handlerscallback_query_handlersshipping_query_handlerspre_checkout_query_handlerspoll_handlerspoll_answer_handlersmy_chat_member_handlerschat_member_handlerschat_join_request_handlerserrors_handlersr   
middlewareregisterprocess_update_polling_closed_dispatcher_close_waiter_setup_filters)selfrJ   loopr3   r4   rL   rM   rN   r/   r/   r0   __init__-   sF   

zDispatcher.__init__returnc                 C      | j S N)rK   rf   r/   r/   r0   rg   ^      zDispatcher.loopasyncio.Futurec                 C   s   | j d u rt  | _ | j S rk   )rd   r,   get_event_loopcreate_futurerl   r/   r/   r0   _close_waiterf   s   
zDispatcher._close_waiterc                 C   s  | j }|jt| j| j| jgd |jt| j| j| j	| j
gdf |jt| j| jgd |jt| j| j| j	| j
| j| j| jgd |jt| j| j| j	| j
gd |jt| j| j| j	| j
| j| j| jgd |jt| j| jgd |jt| jgd |jt| j| j| j	| j
| j| j| j| jgd |jt| j| j| j	| j
| j| j| j| j| jg	d |jt| j| j| j	| j
gd |jt| j| j| j	| j
gd |jt| j| j
| j	| j
gd |jt| j| j| j	| j
| j| j| j| jgd |jt| j| j
| j	| j
gd d S )N)exclude_event_handlers)event_handlers)rN   bindr   r^   rY   rZ   r   rP   rQ   rR   rS   r   r   rV   rT   r	   r
   r   r   r   r\   r]   r   r[   r   r   r   r   r   )rf   rN   r/   r/   r0   re   l   s   
		

zDispatcher._setup_filtersc                 C   s   |    d S rk   )stop_pollingrl   r/   r/   r0   __del__   s   zDispatcher.__del__c                    s   | j jddI dH  dS )z
        You can skip old incoming updates from queue.
        This method is not recommended for using in production.

        Note that the webhook will be deleted!
        T)drop_pending_updatesN)rJ   delete_webhookrl   r/   r/   r0   skip_updates   s   zDispatcher.skip_updatesTfastc                    sR   |r fdd|D }t j| I dH S g }|D ]}| j|I dH  q|S )zh
        Process list of updates

        :param updates:
        :param fast:
        :return:
        c                    s   g | ]} j |qS r/   )rO   notify).0r6   rl   r/   r0   
<listcomp>   s    z.Dispatcher.process_updates.<locals>.<listcomp>N)r,   gatherappendrO   r{   )rf   updatesrz   tasksresultsr6   r/   rl   r0   process_updates   s   zDispatcher.process_updatesr6   c              
      s  t j| z|jr.t j|j t j|jj t j|jj | j	
|jI dH W S |jrSt j|j t j|jj t j|jj | j
|jI dH W S |jrpt j|j t j|jj | j
|jI dH W S |jrt j|j t j|jj | j
|jI dH W S |jrt j|j t j|jj | j
|jI dH W S |jrt j|j t j|jj | j
|jI dH W S |jrt j|j |jjrt j|jjj t j|jj | j
|jI dH W S |jrt j|j t j|jj | j
|jI dH W S |jr-t j|j t j|jj | j
|jI dH W S |j rCt j!|j  | j"
|j I dH W S |j#rat j$|j# t j|j#j% | j&
|j#I dH W S |j'rt j(|j' t j|j'j | j)
|j'I dH W S |j*rt j(|j* t j|j*j | j+
|j*I dH W S |j,rt j-|j, t j|j,j t j|j,j | j.
|j,I dH W S W dS  t/y } z| j0
||I dH }|r|W  Y d}~S  d}~ww )zW
        Process single update object

        :param update:
        :return:
        N)1r"   Updateset_currentr8   MessageUser	from_userChatchatrP   r{   r9   rQ   r:   rR   r;   rS   r<   InlineQueryrT   r=   ChosenInlineResultrU   r>   CallbackQueryrV   r?   ShippingQueryrW   r@   PreCheckoutQueryrX   rA   PollrY   rB   
PollAnsweruserrZ   rC   ChatMemberUpdatedr[   rD   r\   rE   ChatJoinRequestr]   	Exceptionr^   )rf   r6   eerrr/   r/   r0   ra      s   zDispatcher.process_updatec                    s0   |r| j  I dH }|jsdS | j  I dH S )z]
        Reset webhook

        :param check: check before deleting
        :return:
        NF)rJ   get_webhook_infourlrx   )rf   checkwhr/   r/   r0   reset_webhook>  s   zDispatcher.reset_webhookc                 C   s
   t |S rk   )r,   create_task)rf   coror/   r/   r0   _loop_create_taskL  s   
zDispatcher._loop_create_task   r(      error_sleepallowed_updatesc              
      s  | j rtdtd t|  t| j |du r%| jddI dH  |r0| jddI dH  d| _ d}z| jj	}	|	t
urN|durNtj|	j| pJdd}
nd}
| j rz%| j|
 | jj||||d	I dH }W d   n1 srw   Y  W n( tjy   Y ne ty } ztd
 t|I dH  W Y d}~qPd}~ww |rtdt| d |d jd }t| || |rt|I dH  | j sSW | jd td dS W | jd td dS W | jd td dS | jd td w )z
        Start long-polling

        :param timeout:
        :param relax:
        :param limit:
        :param reset_webhook:
        :param fast:
        :param error_sleep:
        :param allowed_updates:
        :return:
        zPolling already startedzStart polling.NF)r   Tr   )total)limitoffsettimeoutr   z&Cause exception while getting updates.z	Received z	 updates.zPolling is stopped.)rb   RuntimeErrorloginfor2   r   r#   rJ   r   r   r   aiohttpClientTimeoutr   request_timeoutget_updatesr,   CancelledErrorr   	exceptionsleepdebuglen	update_idr   _process_polling_updatesrq   
set_resultwarning)rf   r   relaxr   r   rz   r   r   r   current_request_timeoutr   r   r   r/   r/   r0   start_pollingO  sj   


zDispatcher.start_pollingc                    s   g }t j| ||I dH D ]}|D ]}t|tsq||| j qq|rAzt	j
|  W dS  ty@   td Y dS w dS )z|
        Process updates received from long-polling.

        :param updates: list of updates.
        :param fast:
        Nz)Cause exception while processing updates.)	itertoolschainfrom_iterabler   r+   r    r   execute_responserJ   r,   r~   r$   r   r   )rf   r   rz   need_to_call	responsesresponser/   r/   r0   r     s   
z#Dispatcher._process_polling_updatesc                 C   s,   t | dr| jrtd d| _dS dS dS )z?
        Break long-polling process.

        :return:
        rb   zStop polling...FN)hasattrrb   r   r   rl   r/   r/   r0   ru     s   

zDispatcher.stop_pollingc                    s   t | jI dH  dS )zF
        Wait for the long-polling to close

        :return:
        N)r,   shieldrq   rl   r/   r/   r0   wait_closed  s   zDispatcher.wait_closedc                 C   rj   )z?
        Check if polling is enabled

        :return:
        )rb   rl   r/   r/   r0   
is_polling  rm   zDispatcher.is_pollingcommandsregexpcontent_typesstaterun_taskc          
      O   @   | j j| jg|R ||||d|}	| j| |||	 dS )a  
        Register handler for message

        .. code-block:: python3

            # This handler works only if state is None (by default).
            dp.register_message_handler(cmd_start, commands=['start', 'about'])
            dp.register_message_handler(entry_point, commands=['setup'])

            # This handler works only if current state is "first_step"
            dp.register_message_handler(step_handler_1, state="first_step")

            # If you want to handle all states by one handler, use `state="*"`.
            dp.register_message_handler(cancel_handler, commands=['cancel'], state="*")
            dp.register_message_handler(cancel_handler, lambda msg: msg.text.lower() == 'cancel', state="*")

        :param callback:
        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param custom_filters: list of custom filters
        :param kwargs:
        :param state:
        :return: decorated function
        r   r   r   r   N)rN   resolverP   r`   _wrap_async_task
rf   callbackr   r   r   r   r   custom_filterskwargsfilters_setr/   r/   r0   register_message_handler  s   z#Dispatcher.register_message_handlerc          	             fdd}|S )ab  
        Decorator for message handler

        Examples:

        Simple commands handler:

        .. code-block:: python3

            @dp.message_handler(commands=['start', 'welcome', 'about'])
            async def cmd_handler(message: types.Message):

        Filter messages by regular expression:

        .. code-block:: python3

            @dp.message_handler(regexp='^[a-z]+-[0-9]+')
            async def msg_handler(message: types.Message):

        Filter messages by command regular expression:

        .. code-block:: python3

            @dp.message_handler(filters.RegexpCommandsFilter(regexp_commands=['item_([0-9]*)']))
            async def send_welcome(message: types.Message):

        Filter by content type:

        .. code-block:: python3

            @dp.message_handler(content_types=ContentType.PHOTO | ContentType.DOCUMENT)
            async def audio_handler(message: types.Message):

        Filter by custom function:

        .. code-block:: python3

            @dp.message_handler(lambda message: message.text and 'hello' in message.text.lower())
            async def text_handler(message: types.Message):

        Use multiple filters:

        .. code-block:: python3

            @dp.message_handler(commands=['command'], content_types=ContentType.TEXT)
            async def text_handler(message: types.Message):

        Register multiple filters set for one handler:

        .. code-block:: python3

            @dp.message_handler(commands=['command'])
            @dp.message_handler(lambda message: demojize(message.text) == ':new_moon_with_face:')
            async def text_handler(message: types.Message):

        This handler will be called if the message starts with '/command' OR is some emoji

        By default content_type is :class:`ContentType.TEXT`

        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param custom_filters: list of custom filters
        :param kwargs:
        :param state:
        :param run_task: run callback in task (no wait results)
        :return: decorated function
        c                    (   j | gR  d | S Nr   )r   r   r   r   r   r   r   r   rf   r   r/   r0   	decorator/  s   z-Dispatcher.message_handler.<locals>.decoratorr/   	rf   r   r   r   r   r   r   r   r   r/   r   r0   message_handler  s   GzDispatcher.message_handlerc          
      O   r   )a  
        Register handler for edited message

        :param callback:
        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param state:
        :param custom_filters: list of custom filters
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        r   N)rN   r   rQ   r`   r   r   r/   r/   r0   register_edited_message_handler7     z*Dispatcher.register_edited_message_handlerc          	         r   )aZ  
        Decorator for edited message handler

        You can use combination of different handlers

        .. code-block:: python3

            @dp.message_handler()
            @dp.edited_message_handler()
            async def msg_handler(message: types.Message):

        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param state:
        :param custom_filters: list of custom filters
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        c                    r   r   )r   r   r   r/   r0   r   f     z4Dispatcher.edited_message_handler.<locals>.decoratorr/   r   r/   r   r0   edited_message_handlerO  s   z!Dispatcher.edited_message_handlerc          
      O   r   )a  
        Register handler for channel post

        :param callback:
        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param state:
        :param custom_filters: list of custom filters
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        r   N)rN   r   rR   r`   r   r   r/   r/   r0   register_channel_post_handlerm  r   z(Dispatcher.register_channel_post_handlerc          	         r   )ay  
        Decorator for channel post handler

        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param state:
        :param custom_filters: list of custom filters
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        c                    r   r   )r   r   r   r/   r0   r     r   z2Dispatcher.channel_post_handler.<locals>.decoratorr/   r   r/   r   r0   channel_post_handler  s   zDispatcher.channel_post_handlerc          
      O   s@   | j j| jg|R ||||d|}	| j| |||	 dS )a  
        Register handler for edited channel post

        :param callback:
        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param state:
        :param custom_filters: list of custom filters
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        r   N)rN   r   rQ   rS   r`   r   r   r/   r/   r0   $register_edited_channel_post_handler  r   z/Dispatcher.register_edited_channel_post_handlerc          	         r   )a  
        Decorator for edited channel post handler

        :param commands: list of commands
        :param regexp: REGEXP
        :param content_types: List of content types.
        :param custom_filters: list of custom filters
        :param state:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        c                    r   r   )r   r   r   r/   r0   r     s   z9Dispatcher.edited_channel_post_handler.<locals>.decoratorr/   r   r/   r   r0   edited_channel_post_handler  s   z&Dispatcher.edited_channel_post_handlerr   r   c                O   F   |du rg }| j j| jg|R d|i|}| j| ||| dS )a  
        Register handler for inline query

        Example:

        .. code-block:: python3

            dp.register_inline_handler(some_inline_handler, lambda inline_query: True)

        :param callback:
        :param custom_filters: list of custom filters
        :param state:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        Nr   )rN   r   rT   r`   r   rf   r   r   r   r   r   r   r/   r/   r0   register_inline_handler     z"Dispatcher.register_inline_handlerc                       fdd}|S )a  
        Decorator for inline query handler

        Example:

        .. code-block:: python3

            @dp.inline_handler(lambda inline_query: True)
            async def some_inline_handler(inline_query: types.InlineQuery)

        :param state:
        :param custom_filters: list of custom filters
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return: decorated function
        c                    "   j | g R d | S Nr   )r   r   r   r   r   rf   r   r/   r0   r        z,Dispatcher.inline_handler.<locals>.decoratorr/   rf   r   r   r   r   r   r/   r   r0   inline_handler     zDispatcher.inline_handlerc                O   r   )a  
        Register handler for chosen inline query

        Example:

        .. code-block:: python3

            dp.register_chosen_inline_handler(some_chosen_inline_handler, lambda chosen_inline_result: True)

        :param callback:
        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return:
        Nr   )rN   r   rU   r`   r   r   r/   r/   r0   register_chosen_inline_handler  r   z)Dispatcher.register_chosen_inline_handlerc                   r   )a  
        Decorator for chosen inline query handler

        Example:

        .. code-block:: python3

            @dp.chosen_inline_handler(lambda chosen_inline_result: True)
            async def some_chosen_inline_handler(chosen_inline_result: types.ChosenInlineResult)

        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        :return:
        c                    r   r   )r   r   r   r/   r0   r   &  r   z3Dispatcher.chosen_inline_handler.<locals>.decoratorr/   r   r/   r   r0   chosen_inline_handler  r   z Dispatcher.chosen_inline_handlerc                O   :   | j j| jg|R d|i|}| j| ||| dS )ar  
        Register handler for callback query

        Example:

        .. code-block:: python3

            dp.register_callback_query_handler(some_callback_handler, lambda callback_query: True)

        :param callback:
        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        r   N)rN   r   rV   r`   r   r   r/   r/   r0   register_callback_query_handler,     z*Dispatcher.register_callback_query_handlerc                   r   )a  
        Decorator for callback query handler

        Example:

        .. code-block:: python3

            @dp.callback_query_handler(lambda callback_query: True)
            async def some_callback_handler(callback_query: types.CallbackQuery)

        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r   r   )r   r   r   r/   r0   r   S  r   z4Dispatcher.callback_query_handler.<locals>.decoratorr/   r   r/   r   r0   callback_query_handlerB     z!Dispatcher.callback_query_handlerc                O   r   )ax  
        Register handler for shipping query

        Example:

        .. code-block:: python3

            dp.register_shipping_query_handler(some_shipping_query_handler, lambda shipping_query: True)

        :param callback:
        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        r   N)rN   r   rW   r`   r   r   r/   r/   r0   register_shipping_query_handlerY  s   z*Dispatcher.register_shipping_query_handlerc                   r   )a  
        Decorator for shipping query handler

        Example:

        .. code-block:: python3

            @dp.shipping_query_handler(lambda shipping_query: True)
            async def some_shipping_query_handler(shipping_query: types.ShippingQuery)

        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r   r   )r   r   r   r/   r0   r     r   z4Dispatcher.shipping_query_handler.<locals>.decoratorr/   r   r/   r   r0   shipping_query_handlerp  r   z!Dispatcher.shipping_query_handlerc                O   r   )a  
        Register handler for pre-checkout query

        Example:

        .. code-block:: python3

            dp.register_pre_checkout_query_handler(some_pre_checkout_query_handler, lambda shipping_query: True)

        :param callback:
        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        r   N)rN   r   rX   r`   r   r   r/   r/   r0   #register_pre_checkout_query_handler  r   z.Dispatcher.register_pre_checkout_query_handlerc                   r   )a  
        Decorator for pre-checkout query handler

        Example:

        .. code-block:: python3

            @dp.pre_checkout_query_handler(lambda shipping_query: True)
            async def some_pre_checkout_query_handler(shipping_query: types.ShippingQuery)

        :param state:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r   r   )r   r   r   r/   r0   r     s   z8Dispatcher.pre_checkout_query_handler.<locals>.decoratorr/   r   r/   r   r0   pre_checkout_query_handler  s   z%Dispatcher.pre_checkout_query_handler)r   c                O   6   | j j| jg|R i |}| j| ||| dS )a'  
        Register handler for poll

        Example:

        .. code-block:: python3

            dp.register_poll_handler(some_poll_handler)

        :param callback:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        N)rN   r   rY   r`   r   rf   r   r   r   r   r   r/   r/   r0   register_poll_handler     z Dispatcher.register_poll_handlerc                       fdd}|S )a0  
        Decorator for poll handler

        Example:

        .. code-block:: python3

            @dp.poll_handler()
            async def some_poll_handler(poll: types.Poll)

        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                        j | g R di | S Nr   )r  r   r   r   r   rf   r/   r0   r        z*Dispatcher.poll_handler.<locals>.decoratorr/   rf   r   r   r   r   r/   r  r0   poll_handler     zDispatcher.poll_handlerc                O   r   )a<  
        Register handler for poll_answer

        Example:

        .. code-block:: python3

            dp.register_poll_answer_handler(some_poll_answer_handler)

        :param callback:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        N)rN   r   rZ   r`   r   r   r/   r/   r0   register_poll_answer_handler  r  z'Dispatcher.register_poll_answer_handlerc                   r  )aR  
        Decorator for poll_answer handler

        Example:

        .. code-block:: python3

            @dp.poll_answer_handler()
            async def some_poll_answer_handler(poll_answer: types.PollAnswer)

        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r  r  )r  r   r  r/   r0   r     r  z1Dispatcher.poll_answer_handler.<locals>.decoratorr/   r  r/   r  r0   poll_answer_handler  r
  zDispatcher.poll_answer_handlerr   r   c                O   8   | j j| jg|R i |}| jj| |||d dS )aE  
        Register handler for my_chat_member

        Example:

        .. code-block:: python3

            dp.register_my_chat_member_handler(some_my_chat_member_handler)

        :param callback:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        handlerfiltersN)rN   r   r[   r`   r   r   r/   r/   r0   register_my_chat_member_handler     

z*Dispatcher.register_my_chat_member_handlerc                   r  )aV  
        Decorator for my_chat_member handler

        Example:

        .. code-block:: python3

            @dp.my_chat_member_handler()
            async def some_handler(my_chat_member: types.ChatMemberUpdated)

        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r  r  )r  r   r  r/   r0   r   8     z4Dispatcher.my_chat_member_handler.<locals>.decoratorr/   r  r/   r  r0   my_chat_member_handler(     	z!Dispatcher.my_chat_member_handlerc                O   r  )a<  
        Register handler for chat_member

        Example:

        .. code-block:: python3

            dp.register_chat_member_handler(some_chat_member_handler)

        :param callback:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        r  N)rN   r   r\   r`   r   r   r/   r/   r0   register_chat_member_handlerC  r  z'Dispatcher.register_chat_member_handlerc                   r  )aM  
        Decorator for chat_member handler

        Example:

        .. code-block:: python3

            @dp.chat_member_handler()
            async def some_handler(chat_member: types.ChatMemberUpdated)

        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r  r  )r  r   r  r/   r0   r   p  r  z1Dispatcher.chat_member_handler.<locals>.decoratorr/   r  r/   r  r0   chat_member_handler`  r  zDispatcher.chat_member_handlerc                O   r  )a>  
        Register handler for chat_join_request

        Example:

        .. code-block:: python3

            dp.register_chat_join_request(some_chat_join_request)

        :param callback:
        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        r  N)rN   r   r]   r`   r   r   r/   r/   r0   "register_chat_join_request_handler{  r  z-Dispatcher.register_chat_join_request_handlerc                   r  )aO  
        Decorator for chat_join_request handler

        Example:

        .. code-block:: python3

            @dp.chat_join_request()
            async def some_handler(chat_member: types.ChatJoinRequest)

        :param custom_filters:
        :param run_task: run callback in task (no wait results)
        :param kwargs:
        c                    r  r  )r  r   r  r/   r0   r     r  z7Dispatcher.chat_join_request_handler.<locals>.decoratorr/   r  r/   r  r0   chat_join_request_handler  r  z$Dispatcher.chat_join_request_handler)r   r   c                O   r   )z
        Register handler for errors

        :param callback:
        :param exception: you can make handler for specific errors type
        :param run_task: run callback in task (no wait results)
        r   N)rN   r   r^   r`   r   )rf   r   r   r   r   r   r   r/   r/   r0   register_errors_handler  s   z"Dispatcher.register_errors_handlerc                   r   )z
        Decorator for errors handler

        :param exception: you can make handler for specific errors type
        :param run_task: run callback in task (no wait results)
        :return:
        c                    s(   j | g R di | S )Nr   )r  r   r   r   r   r   r   rf   r/   r0   r     s   z,Dispatcher.errors_handler.<locals>.decoratorr/   )rf   r   r   r   r   r   r/   r  r0   errors_handler  s   	zDispatcher.errors_handlerr   r   r   r   c                C   sP   |du rt j }|r|jnd}|du r t j }|r|jnd}t| j||dS )aZ  
        Get current state for user in chat as context

        .. code-block:: python3

            with dp.current_state(chat=message.chat.id, user=message.user.id) as state:
                pass

            state = dp.current_state()
            state.set_state('my_state')

        :param chat:
        :param user:
        :return:
        N)r3   r   r   )r"   r   get_currentidr   r   r3   )rf   r   r   chat_objuser_objr/   r/   r0   current_state  s   

zDispatcher.current_stateuser_idz3.0   )old_namenew_nameuntil_version
stacklevelchat_id   rater#  r)  no_errorc                   s`  | j  s
td|du r| j}|du r| j}|du r8|du r8tj }|r*|jnd}tj	 }|r6|jnd}t

 }| j j||dI dH }	|	du rO|i i}	||	vrWi |	|< |	| }
|
t|}|| }||kpl|dk}||
t< ||
t< ||
t< ||
t< |s|
t  d7  < nd|
t< |	| |
 | j j|||	dI dH  |s|std|||d|
|S )	a  
        Execute throttling manager.
        Returns True if limit has not exceeded otherwise raises ThrottleError or returns False

        :param key: key in storage
        :param rate: limit (by default is equal to default rate limit)
        :param user_id: user id
        :param chat_id: chat id
        :param no_error: return boolean value instead of raising error
        :return: bool
        *This storage does not provide Leaky BucketNr  r   r   r   r   bucketkeyr   r   r/   )r3   
has_bucketr   rM   rL   r"   r   r  r  r   time
get_bucketgetr   r   r   r   r   r6   
set_bucketr%   )rf   r2  r,  r#  r)  r-  r   r!  nowr0  datacalleddeltaresultr/   r/   r0   throttle  sD   


zDispatcher.throttlec                    s   | j  s
td|du r*|du r*tj }|r|jnd}tj }|r(|jnd}| j j||dI dH }|	|i }t
d|||d|S )z
        Get information about key in bucket

        :param key:
        :param chat_id:
        :param user_id:
        :return:
        r.  Nr  r1  r/   )r3   r3  r   r"   r   r  r  r   r5  r6  r%   )rf   r2  r)  r#  r   r!  r0  r9  r/   r/   r0   	check_key*  s   


zDispatcher.check_keyc                    s   | j  s
td|du r*|du r*tj }|r|jnd}tj }|r(|jnd}| j j||dI dH }|rL||v rL||= | j j	|||dI dH  dS dS )z{
        Release blocked key

        :param key:
        :param chat_id:
        :param user_id:
        :return:
        r.  Nr  r/  TF)
r3   r3  r   r"   r   r  r  r   r5  r7  )rf   r2  r)  r#  r   r!  r0  r/   r/   r0   release_keyC  s   


zDispatcher.release_keyc                    s(   fddt   fdd}|S )a  
        Execute handler as task and return None.
        Use this decorator for slow handlers (with timeouts)

        .. code-block:: python3

            @dp.message_handler(commands=['command'])
            @dp.async_task
            async def cmd_with_timeout(message: types.Message):
                await asyncio.sleep(120)
                return SendMessage(message.chat.id, 'KABOOM').reply(message)

        :param func:
        :return:
        c              
      sr   z|   }W n  ty& } zt jtj | W Y d }~d S d }~ww t	|t
r7t| j d S d S rk   )r<  r   r,   r   r^   r{   r"   r   r  r+   r    r   rJ   )taskr   r   rl   r/   r0   process_responsep  s   
z/Dispatcher.async_task.<locals>.process_responsec                     s$   t  | i |}| d S rk   )r,   r   add_done_callback)argsr   r@  )funcrA  r/   r0   wrapperz  s   z&Dispatcher.async_task.<locals>.wrapper	functoolswraps)rf   rD  rE  r/   )rD  rA  rf   r0   
async_task_  s   
zDispatcher.async_taskc                 C   s    |d u r| j }|r| |S |S rk   )r4   rI  )rf   r   r   r/   r/   r0   r     s
   
zDispatcher._wrap_async_taskon_throttledc                    s    fdd}|S )a  
        Meta-decorator for throttling.
        Invokes on_throttled if the handler was throttled.

        Example:

        .. code-block:: python3

            async def handler_throttled(message: types.Message, **kwargs):
                await message.answer("Throttled!")

            @dp.throttled(handler_throttled)
            async def some_handler(message: types.Message):
                await message.answer("Didn't throttled!")

        :param on_throttled: the callable object that should be either a function or return a coroutine
        :param key: key in storage
        :param rate: limit (by default is equal to default rate limit)
        :param user_id: user id
        :param chat_id: chat id
        :return: decorator
        c                    s&   t   fdd}|S )Nc                     s   j d ur	nj ddI d H }|r!| i |I d H S | d r]tr>| i |I d H  d S |dt i tjg| R i |}t d | d S d S )NTr+  )r,  r2  r#  r)  rg   )	r=  rI   r6   r,   iscoroutinefunctionget_running_looprG  partialrun_in_executor)rC  r   is_not_throttledpartial_func)r)  rD  r2  rJ  r,  rf   r#  r/   r0   wrapped  s<   	
z8Dispatcher.throttled.<locals>.decorator.<locals>.wrappedrF  )rD  rQ  r)  r2  rJ  r,  rf   r#  )rD  r0   r     s   z'Dispatcher.throttled.<locals>.decoratorr/   )rf   rJ  r2  r,  r#  r)  r   r/   rR  r0   	throttled  s    zDispatcher.throttled	validatorrs   rr   c                 C   s   | j j||||d dS )a0  
        Register filter

        :param callback: callable or subclass of :obj:`AbstractFilter`
        :param validator: custom validator.
        :param event_handlers: list of instances of :obj:`Handler`
        :param exclude_event_handlers: list of excluded event handlers (:obj:`Handler`)
        )r   rT  rs   rr   N)rN   rt   )rf   r   rT  rs   rr   r/   r/   r0   bind_filter  s   
zDispatcher.bind_filterc                 C   s   | j j|d dS )zk
        Unregister filter

        :param callback: callable of subclass of :obj:`AbstractFilter`
        r   N)rN   unbind)rf   r   r/   r/   r0   unbind_filter  s   zDispatcher.unbind_filterc                 C   s   | j | dS )zO
        Setup middleware

        :param middleware:
        :return:
        N)r_   setup)rf   r_   r/   r/   r0   setup_middleware  s   zDispatcher.setup_middleware)ri   rn   )T)r   r(   NNTr   N)NNrk   )NNNNN)NNN)PrI   
__module____qualname____doc__DEFAULT_RATE_LIMITtypingOptionalr   boolrh   propertyr,   r-   rg   rq   re   rv   ry   r   r"   r   ra   r   r   intListstrr   r   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  Callabler  r  r  r  r  r  r  r  Unionr   r"  r   r=  r>  r?  rI  callabler   rS  r   r   IterablerU  rW  rY  r/   r/   r/   r0   r2   %   s   
1i	L
H
$O



<"=

r2   )r)   r*   )<r,   rG  r   loggingr4  r^  r   aiohttp.helpersr   aiogram.utils.deprecatedr   r  r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r  r   middlewaresr   r3   r   r   r   r   r   r   r   r   webhookr     r"   rJ   r#   utils.exceptionsr$   r%   utils.mixinsr&   r'   	getLoggerrI   r   r]  r1   r2   r/   r/   r/   r0   <module>   s,    L(

