o
    -˜Žj  ã                   @   sd   d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	m
Z
mZ ddlmZ G dd	„ d	eƒZdS )
é    )Úiscoroutinefunction)ÚCallableÚTupleN)ÚPyromodConfig)ÚCallbackQueryÚ
IdentifierÚListeneré   )ÚHandlerc                       sx   e Zd ZdZddef‡ fdd„Zdefdd„Zd	d
dedee	e
f fdd„Zd	d
defdd„Zd	d
defdd„Z‡  ZS )ÚCallbackQueryHandleraä  The CallbackQuery handler class. Used to handle callback queries coming from inline buttons.
    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_callback_query` decorator.

    Parameters:
        callback (``Callable``):
            Pass a function that will be called when a new CallbackQuery arrives. It takes *(client, callback_query)*
            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 callback queries 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.

        callback_query (:obj:`~pyrogram.types.CallbackQuery`):
            The received callback query.
    NÚcallbackc                    s   || _ tƒ  | j|¡ d S ©N)Úoriginal_callbackÚsuperÚ__init__Úresolve_future_or_callback)Úselfr   Úfilters©Ú	__class__© úe/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/pyrogram/handlers/callback_query_handler.pyr   7   s   zCallbackQueryHandler.__init__Úqueryc                 C   sz   |j }|r|jnd}|r|jnd}d}d}|jr2t|jdt|jddƒƒ}|jjr2|jjj|jjjg}t||||g|jd�S )z²
        Composes an Identifier object from a CallbackQuery object.

        :param query: The CallbackQuery object to compose of.
        :return: An Identifier object.
        NÚidÚ
message_id)r   Úchat_idÚfrom_user_idÚinline_message_id)Ú	from_userr   ÚusernameÚmessageÚgetattrÚchatr   r   )r   r   r   r   Úfrom_user_usernamer   r   r   r   r   Úcompose_data_identifier;   s"   ÿüz,CallbackQueryHandler.compose_data_identifierÚclientzpyrogram.ClientÚreturnc                 Ã   s‚   �|   |¡}| |tjjj¡}d}|r=|j}t|ƒr;t|j	ƒr+|||ƒI dH }||fS |j
 d|||¡I dH }||fS d}||fS )a³  
        Checks if the CallbackQuery object has a matching listener.

        :param client: The Client object to check with.
        :param query: The CallbackQuery object to check with.
        :return: A tuple of a boolean and a Listener object. The boolean indicates whether
        the found listener has filters and its filters matches with the CallbackQuery object.
        The Listener object is the matching listener.
        FNT)r$   Úget_listener_matching_with_dataÚpyrogramÚenumsÚListenerTypesÚCALLBACK_QUERYr   Úcallabler   Ú__call__ÚloopÚrun_in_executor)r   r%   r   ÚdataÚlistenerÚlistener_does_matchr   r   r   r   Úcheck_if_has_matching_listenerX   s$   €

ÿ
ú
ÿþz3CallbackQueryHandler.check_if_has_matching_listenerc           
      Ã   sØ   �|   ||¡I dH \}}t| jƒr/t| jjƒr!|  ||¡I dH }n|j d| j||¡I dH }nd}|  |¡}tj	rht
|j|j|jdd�}| |¡}|rh|rh|sh|j	rht|j	tƒr[|j	ntj}	| |	¡I dH  dS |pk|S )aU  
        Checks if the CallbackQuery object has a matching listener or handler.

        :param client: The Client object to check with.
        :param query: The CallbackQuery object to check with.
        :return: A boolean indicating whether the CallbackQuery object has a matching listener or the handler
        filter matches.
        NT)r   r   r   r   F)r3   r,   r   r   r-   r.   r/   r$   r   Úunallowed_click_alertr   r   r   r   ÚmatchesÚ
isinstanceÚstrÚunallowed_click_alert_textÚanswer)
r   r%   r   r2   r1   Úhandler_does_matchr0   Úpermissive_identifierr5   Úalertr   r   r   Úcheckz   sF   €	ÿ

ÿ
ü
ÿþþý
ÿýzCallbackQueryHandler.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 )aZ  
        Resolves the future or calls the callback of the listener. Will call the original handler if no listener.

        :param client: The Client object to resolve or call with.
        :param query: The CallbackQuery object to resolve or call with.
        :param args: The arguments to call the callback with.
        :return: None
        Nz0Listener must have either a future or a callback)r3   Úremove_listenerÚfutureÚdoneÚ
set_resultr(   ÚStopPropagationr   r   Ú
ValueErrorr   )r   r%   r   Úargsr2   r1   r   r   r   r   ¯   s"   €ÿ

þz/CallbackQueryHandler.resolve_future_or_callbackr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r$   r   Úboolr   r3   r=   r   Ú__classcell__r   r   r   r   r      s"    ÿÿ

þ"5ÿÿr   )Úasyncior   Útypingr   r   r(   Úpyrogram.utilsr   Úpyrogram.typesr   r   r   Úhandlerr
   r   r   r   r   r   Ú<module>   s   