
    i#                         d Z ddlZddlZddlmZ ddlmZmZ ddl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	lmZ dd
lmZmZ erddlmZ  e
d          Z G d deeeef                   ZdS )z4This module contains the CallbackQueryHandler class.    N)Callable)MatchPattern)TYPE_CHECKINGAnyTypeVarcast)Update)DEFAULT_TRUE)DVType)BaseHandler)CCTHandlerCallback)ApplicationRTc                        e Zd ZdZdZddefdddeeee	f         de
ee
         z  ez  eegef         z  dz  de
ee
         z  dz  d	ee         f
 fd
Zdedeez  dz  fdZdededddeee
         z  ddf
dZ xZS )CallbackQueryHandlera  Handler class to handle Telegram
    :attr:`callback queries <telegram.Update.callback_query>`. Optionally based on a regex.

    Read the documentation of the :mod:`re` module for more information.

    Note:
        * If your bot allows arbitrary objects as
          :paramref:`~telegram.InlineKeyboardButton.callback_data`, it may happen that the
          original :attr:`~telegram.InlineKeyboardButton.callback_data` for the incoming
          :class:`telegram.CallbackQuery` can not be found. This is the case when either a
          malicious client tempered with the :attr:`telegram.CallbackQuery.data` or the data was
          simply dropped from cache or not persisted. In these
          cases, an instance of :class:`telegram.ext.InvalidCallbackData` will be set as
          :attr:`telegram.CallbackQuery.data`.

          .. versionadded:: 13.6

        * If neither :paramref:`pattern` nor :paramref:`game_pattern` is set, `any`
          ``CallbackQuery`` will be handled. If only :paramref:`pattern` is set, queries with
          :attr:`~telegram.CallbackQuery.game_short_name` will `not` be considered and vice versa.
          If both patterns are set, queries with either :attr:
          `~telegram.CallbackQuery.game_short_name` or :attr:`~telegram.CallbackQuery.data`
          matching the defined pattern will be handled

          .. versionadded:: 21.5

    Warning:
        When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom
        attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info.

    Args:
        callback (:term:`coroutine function`): The callback function for this handler. Will be
            called when :meth:`check_update` has determined that an update should be processed by
            this handler. Callback signature::

                async def callback(update: Update, context: CallbackContext)

            The return value of the callback is usually ignored except for the special case of
            :class:`telegram.ext.ConversationHandler`.
        pattern (:obj:`str` | :func:`re.Pattern <re.compile>` | :obj:`callable` | :obj:`type`,             optional):
            Pattern to test :attr:`telegram.CallbackQuery.data` against. If a string or a regex
            pattern is passed, :func:`re.match` is used on :attr:`telegram.CallbackQuery.data` to
            determine if an update should be handled by this handler. If your bot allows arbitrary
            objects as :paramref:`~telegram.InlineKeyboardButton.callback_data`, non-strings will
            be accepted. To filter arbitrary objects you may pass:

            - a callable, accepting exactly one argument, namely the
              :attr:`telegram.CallbackQuery.data`. It must return :obj:`True` or
              :obj:`False`/:obj:`None` to indicate, whether the update should be handled.
            - a :obj:`type`. If :attr:`telegram.CallbackQuery.data` is an instance of that type
              (or a subclass), the update will be handled.

            If :attr:`telegram.CallbackQuery.data` is :obj:`None`, the
            :class:`telegram.CallbackQuery` update will not be handled.

            .. seealso:: :wiki:`Arbitrary callback_data <Arbitrary-callback_data>`

            .. versionchanged:: 13.6
               Added support for arbitrary callback data.
        game_pattern (:obj:`str` | :func:`re.Pattern <re.compile>` | optional)
            Pattern to test :attr:`telegram.CallbackQuery.game_short_name` against. If a string or
            a regex pattern is passed, :func:`re.match` is used on
            :attr:`telegram.CallbackQuery.game_short_name` to determine if an update should be
            handled by this handler.

            .. versionadded:: 21.5
        block (:obj:`bool`, optional): Determines whether the return value of the callback should
            be awaited before processing the next handler in
            :meth:`telegram.ext.Application.process_update`. Defaults to :obj:`True`.

            .. seealso:: :wiki:`Concurrency`

    Attributes:
        callback (:term:`coroutine function`): The callback function for this handler.
        pattern (:func:`re.Pattern <re.compile>` | :obj:`callable` | :obj:`type`): Optional.
            Regex pattern, callback or type to test :attr:`telegram.CallbackQuery.data` against.

            .. versionchanged:: 13.6
               Added support for arbitrary callback data.
        game_pattern (:func:`re.Pattern <re.compile>`): Optional.
            Regex pattern to test :attr:`telegram.CallbackQuery.game_short_name`
        block (:obj:`bool`): Determines whether the return value of the callback should be
            awaited before processing the next handler in
            :meth:`telegram.ext.Application.process_update`.

    )game_patternpatternNselfzCallbackQueryHandler[CCT, RT]callbackr   r   blockc                 r   t                                          ||           t          |          r#t          j        |          rt          d          t          |t                    rt          j	        |          }t          |t                    rt          j	        |          }|| _
        || _        d S )N)r   zQThe `pattern` must not be a coroutine function! Use an ordinary function instead.)super__init__callableasyncioiscoroutinefunction	TypeError
isinstancestrrecompiler   r   )r   r   r   r   r   	__class__s        k/Users/shanyulin/my-agent/.venv/lib/python3.11/site-packages/telegram/ext/_handlers/callbackqueryhandler.pyr   zCallbackQueryHandler.__init__   s     	///G 	!<W!E!E 	c   gs## 	*j))GlC(( 	4:l33LT[7C    updatereturnc                 6   t          |t                    r|j        sdS |j        j        }|j        j        }t          | j        | j        g          sdS |r| j        sdS t          | j        t                    rt          || j                  S t          | j                  r|                     |          S t          |t                    sdS t          j        | j        |          x}r|S n,|r(| j        sdS t          j        | j        |          x}r|S ndS dS )zDetermines whether an update should be passed to this handler's :attr:`callback`.

        Args:
            update (:class:`telegram.Update` | :obj:`object`): Incoming update.

        Returns:
            :obj:`bool`

        NTF)r    r
   callback_querydatagame_short_nameanyr   r   typer   r!   r"   match)r   r'   callback_datar,   r/   s        r%   check_updatez!CallbackQueryHandler.check_update   sE    66** 	v/D 	4-2 /?DL$"3455 	4  	< u$,-- ?!->>>%% 3||M222mS11 u}===u   	$ u!2ODDDu  4ur&   contextapplicationz)Application[Any, CCT, Any, Any, Any, Any]check_resultc                 H    | j         rt          d|          }|g|_        dS dS )zAdd the result of ``re.match(pattern, update.callback_query.data)`` to
        :attr:`CallbackContext.matches` as list with one element.
        r   N)r   r	   matches)r   r2   r'   r3   r4   s        r%   collect_additional_contextz/CallbackQueryHandler.collect_additional_context   s4     < 	-66L+nGOOO	- 	-r&   )__name__
__module____qualname____doc__	__slots__r   r   r
   r   r   r!   r   r.   r   objectboolr   r   r1   r   r7   __classcell__)r$   s   @r%   r   r   '   sW       V Vp ,I
 PT26*D D-D!&#r/2D ws|#d*Xvhn-EELD GCL(4/	D
 d|D D D D D D*)6 )dVmd.B ) ) ) )V-- - A	-
 U3Z'- 
- - - - - - - -r&   r   )r;   r   r"   collections.abcr   r   r   typingr   r   r   r	   telegramr
   telegram._utils.defaultvaluer   telegram._utils.typesr   "telegram.ext._handlers.basehandlerr   telegram.ext._utils.typesr   r   telegram.extr   r   r    r&   r%   <module>rI      s=  & ; :  				 $ $ $ $ $ $         4 4 4 4 4 4 4 4 4 4 4 4       5 5 5 5 5 5 ( ( ( ( ( ( : : : : : : : : : : : : : : )((((((WT]]g- g- g- g- g-;vsB7 g- g- g- g- g-r&   