
    ՟fY&                         d Z ddl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mZ ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZmZ erdd
lmZ  e	d      Z G d deeeef         Zy)z.This module contains the CommandHandler class.    N)TYPE_CHECKINGAny	FrozenSetListOptionalTupleTypeVarUnion)MessageEntityUpdate)DEFAULT_TRUE)SCTDVType)filters)BaseHandler)CCTFilterDataDictHandlerCallback)ApplicationRTc                   (    e Zd ZdZdZdedfdddee   dee	e
ef   deej                     d	ee   d
eeeef      f fdZdee   dee   fdZdedeeeeee   eeeef      f   f      fdZde
de	dddeeeeee   ee   f   f      ddf
dZ xZS )CommandHandleraI  Handler class to handle Telegram commands.

    Commands are Telegram messages that start with ``/``, optionally followed by an ``@`` and the
    bot's name and/or some additional text. The handler will add a :obj:`list` to the
    :class:`CallbackContext` named :attr:`CallbackContext.args`. It will contain a list of strings,
    which is the text following the command split on single or consecutive whitespace characters.

    By default, the handler listens to messages as well as edited messages. To change this behavior
    use :attr:`~filters.UpdateType.EDITED_MESSAGE <telegram.ext.filters.UpdateType.EDITED_MESSAGE>`
    in the filter argument.

    Note:
        :class:`CommandHandler` does *not* handle (edited) channel posts and does *not* handle
        commands that are part of a caption. Please use :class:`~telegram.ext.MessageHandler`
        with a suitable combination of filters (e.g.
        :attr:`telegram.ext.filters.UpdateType.CHANNEL_POSTS`,
        :attr:`telegram.ext.filters.CAPTION` and :class:`telegram.ext.filters.Regex`) to handle
        those messages.

    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.

    Examples:
        * :any:`Timer Bot <examples.timerbot>`
        * :any:`Error Handler Bot <examples.errorhandlerbot>`

    .. versionchanged:: 20.0

        * Renamed the attribute ``command`` to :attr:`commands`, which now is always a
          :class:`frozenset`
        * Updating the commands this handler listens to is no longer possible.

    Args:
        command (:obj:`str` | Collection[:obj:`str`]):
            The command or list of commands this handler should listen for. Case-insensitive.
            Limitations are the same as for :attr:`telegram.BotCommand.command`.
        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`.
        filters (:class:`telegram.ext.filters.BaseFilter`, optional): A filter inheriting from
            :class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
            :mod:`telegram.ext.filters`. Filters can be combined using bitwise
            operators (``&`` for :keyword:`and`, ``|`` for :keyword:`or`, ``~`` for :keyword:`not`)
        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`
        has_args (:obj:`bool` | :obj:`int`, optional):
            Determines whether the command handler should process the update or not.
            If :obj:`True`, the handler will process any non-zero number of args.
            If :obj:`False`, the handler will only process if there are no args.
            if :obj:`int`, the handler will only process if there are exactly that many args.
            Defaults to :obj:`None`, which means the handler will process any or no args.

            .. versionadded:: 20.5

    Raises:
        :exc:`ValueError`: When the command is too long or has illegal chars.

    Attributes:
        commands (FrozenSet[:obj:`str`]): The set of commands this handler should listen for.
        callback (:term:`coroutine function`): The callback function for this handler.
        filters (:class:`telegram.ext.filters.BaseFilter`): Optional. Only allow updates with these
            filters.
        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`.
        has_args (:obj:`bool` | :obj:`int` | None):
            Optional argument, otherwise all implementations of :class:`CommandHandler` will break.
            Defaults to :obj:`None`, which means the handler will process any args or no args.

            .. versionadded:: 20.5
    )commandsr   has_argsNselfzCommandHandler[CCT, RT]commandcallbackr   blockr   c                    t         |   ||       t        |t              rt	        |j                         h      }nt	        d |D              }|D ]'  }t        j                  d|      rt        d| d       || _	        ||nt        j                  j                  | _        || _        t        | j                  t              r| j                  dk  rt        d      y y )N)r   c              3   <   K   | ]  }|j                           y w)N)lower).0xs     `/var/www/cvtools/html/venv/lib/python3.12/site-packages/telegram/ext/_handlers/commandhandler.py	<genexpr>z*CommandHandler.__init__.<locals>.<genexpr>   s      <q <s   z^[\da-z_]{1,32}$z	Command `z` is not a valid bot commandr   z=CommandHandler argument has_args cannot be a negative integer)super__init__
isinstancestr	frozensetr!   rematch
ValueErrorr   filters_module
UpdateTypeMESSAGESr   r   int)	r   r   r   r   r   r   r   comm	__class__s	           r$   r'   zCommandHandler.__init__x   s     	/gs# '--/!23H  <G <<H 	QD88/6 9TF2N!OPP	Q )1 *G0I0I0R0R 	 5=t}}c*1B\]] 2C*    argsreturnc                     t        | j                  du xs] | j                  du xr |xsI | j                  du xr | xs4 t        | j                  t              xr t	        |      | j                  k(        S )a   Determines whether the args are correct for this handler. Implemented in check_update().
        Args:
            args (:obj:`list`): The args for the handler.
        Returns:
            :obj:`bool`: Whether the args are valid for this handler.
        NTF)boolr   r(   r1   len)r   r5   s     r$   _check_correct_argsz"CommandHandler._check_correct_args   su     ]]d" O%.$O&3t8O 4==#.M3t93M	
 	
r4   updatec                 X   t        |t              r|j                  r|j                  }|j                  rs|j                  d   j                  t
        j                  k(  rH|j                  d   j                  dk(  r+|j                  r|j                         r|j                  d|j                  d   j                   }|j                  j                         dd }|j                  d      }|j                  |j                         j                         |d   j                         | j                  v r<|d   j                         |j                         j                  j                         k(  sy| j!                  |      sy| j"                  j%                  |      }|r||fS yy)a  Determines whether an update should be passed to this handler's :attr:`callback`.

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

        Returns:
            :obj:`list`: The list of args for the handler.

        r      N@F)r(   r   effective_messageentitiestyper   BOT_COMMANDoffsettextget_botlengthsplitappendusernamer!   r   r:   r   check_update)r   r;   messager   r5   command_partsfilter_results          r$   rJ   zCommandHandler.check_update   s\    ff%&*B*B..G   $$Q',,0I0II$$Q'..!3LLOO%!,,q7+;+;A+>+E+EF||))+AB/ 'c 2$$W__%6%?%?@ "!$**,=%a(..0GOO4E4N4N4T4T4VV//5 $ 9 9& A ..r4   contextapplicationz)Application[Any, CCT, Any, Any, Any, Any]check_resultc                     t        |t              r3|d   |_        t        |d   t              r|j	                  |d          yyy)zAdd text after the command to :attr:`CallbackContext.args` as list, split on single
        whitespaces and add output of data filters to :attr:`CallbackContext` as well.
        r   r=   N)r(   tupler5   dictr;   )r   rN   r;   rO   rP   s        r$   collect_additional_contextz)CommandHandler.collect_additional_context   sA     lE*'?GL,q/40|A/ 1 +r4   )__name__
__module____qualname____doc__	__slots__r   r   r)   r   r   r   r   r   r.   
BaseFilterr   r8   r
   r1   r'   r   r:   objectr   r   rJ   rT   __classcell__)r3   s   @r$   r   r   $   s@   Ob 4I 8<*/3^'^S^ "&#r/2^ .334	^
 d|^ 5s+,^8
S	 
htn 
((	%eDIxdN>R8S/T$TUUV	W(T00 0 A	0
 uT5cHTN1J+K%KLM0 
0r4   r   )rX   r+   typingr   r   r   r   r   r   r	   r
   telegramr   r   telegram._utils.defaultvaluer   telegram._utils.typesr   r   telegram.extr   r.   "telegram.ext._handlers.basehandlerr   telegram.ext._utils.typesr   r   r   r   r   r    r4   r$   <module>re      sS   & 5 	 W W W * 5 - 2 : J J(T]u0[b1 u0r4   