o
    -˜Žj  ã                   @   sP   d dl mZ d dlmZ d dlZd dlmZmZ ddlm	Z	 G dd„ de	ƒZ
dS )	é    )Úiscoroutinefunction)ÚCallableN)ÚMessageÚ
Identifieré   )ÚHandlerc                       s^   e Zd ZdZddef‡ fdd„Zdddefd	d
„Zdddefdd„Zdddefdd„Z	‡  Z
S )ÚBotBusinessMessageHandlera¶  The Bot Business Message handler class. Used to handle new bot business messages.
    It is intended to be used with :meth:`~pyrogram.Client.add_handler`

    For a nicer way to register this handler, have a look at the
    :meth:`~pyrogram.Client.on_bot_business_message` decorator.

    Parameters:
        callback (``Callable``):
            Pass a function that will be called when a new Message arrives. It takes *(client, message)*
            as positional arguments (look at the section below for a detailed description).

        filters (:obj:`Filters`):
            Pass one or more filters to allow only a subset of messages to be passed
            in your callback function.

    Other parameters:
        client (:obj:`~pyrogram.Client`):
            The Client itself, useful when you want to call other API methods inside the message handler.

        message (:obj:`~pyrogram.types.Message`):
            The received message.
    NÚcallbackc                    s   || _ tƒ  | j|¡ d S ©N)Úoriginal_callbackÚsuperÚ__init__Úresolve_future_or_callback)Úselfr	   Úfilters©Ú	__class__© úk/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/pyrogram/handlers/bot_business_message_handler.pyr   4   s   z"BotBusinessMessageHandler.__init__Úclientzpyrogram.ClientÚmessagec                 Ã   sÌ   �|j }|r	|jnd}|r|jnd}t|dt|ddƒƒ}t||jj|jjg||gd�}| |tjj	j
¡}d}	|rb|j}
t|
ƒr`t|
jƒrP|
||ƒI dH }	|	|fS |j d|
||¡I dH }	|	|fS d}	|	|fS )aJ  
        Checks if the message has a matching listener.

        :param client: The Client object to check with.
        :param message: The Message object to check with.
        :return: A tuple of whether the message has a matching listener and its filters does match with the Message
        and the matching listener;
        NÚidÚ
message_id)r   Úchat_idÚfrom_user_idFT)Ú	from_userr   ÚusernameÚgetattrr   ÚchatÚget_listener_matching_with_dataÚpyrogramÚenumsÚListenerTypesÚMESSAGEr   Úcallabler   Ú__call__ÚloopÚrun_in_executor)r   r   r   r   r   Úfrom_user_usernamer   ÚdataÚlistenerÚlistener_does_matchr   r   r   r   Úcheck_if_has_matching_listener8   s0   €	ý
ú
ÿþz8BotBusinessMessageHandler.check_if_has_matching_listenerc                 Ã   sj   �|   ||¡I dH d }t| jƒr/t| jjƒr!|  ||¡I dH }n|j d| j||¡I dH }nd}|p4|S )aW  
        Checks if the message has a matching listener or handler and its filters does match with the Message.

        :param client: Client object to check with.
        :param message: Message object to check with.
        :return: Whether the message has a matching listener or handler and its filters does match with the Message.
        Nr   T)r,   r$   r   r   r%   r&   r'   )r   r   r   r+   Úhandler_does_matchr   r   r   Úcheck_   s   €	þ

ÿzBotBusinessMessageHandler.checkc                 Ç   s¼   �|   ||¡I dH \}}|rO|rO| |¡ |jr&|j ¡ s&|j |¡ tj‚|jrKt|jƒr>|j||g|¢R Ž I dH  tj‚|j||g|¢R Ž  tj‚t	dƒ‚| j
||g|¢R Ž I dH  dS )aB  
        Resolves the future or calls the callback of the listener if the message has a matching listener.

        :param client: Client object to resolve or call with.
        :param message: Message object to resolve or call with.
        :param args: Arguments to call the callback with.
        :return: None
        Nz0Listener must have either a future or a callback)r,   Úremove_listenerÚfutureÚdoneÚ
set_resultr    ÚStopPropagationr	   r   Ú
ValueErrorr   )r   r   r   Úargsr+   r*   r   r   r   r   y   s"   €	ÿ

þz4BotBusinessMessageHandler.resolve_future_or_callbackr
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r,   r.   r   Ú__classcell__r   r   r   r   r      s    'r   )Úinspectr   Útypingr   r    Úpyrogram.typesr   r   Úhandlerr   r   r   r   r   r   Ú<module>   s   