o
    Xzj#  ã                   @   st   d dl Z d dlZd dlZddlmZmZmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZ eG dd	„ d	eƒƒZdS )
é    Né   )ÚEventBuilderÚEventCommonÚname_inner_eventé   )ÚutilsÚhelpers)ÚtypesÚ	functionsÚcustom)ÚSenderGetterc                       sZ   e Zd ZdZ	dddddœ‡ fdd„Zeddd„ƒZ‡ fd	d
„ZG dd„ dee	ƒZ
‡  ZS )ÚInlineQuerya§  
    Occurs whenever you sign in as a bot and a user
    sends an inline query such as ``@bot query``.

    Args:
        users (`entity`, optional):
            May be one or more entities (username/peer/etc.), preferably IDs.
            By default, only inline queries from these users will be handled.

        blacklist_users (`bool`, optional):
            Whether to treat the users as a blacklist instead of
            as a whitelist (default). This means that every chat
            will be handled *except* those specified in ``users``
            which will be ignored if ``blacklist_users=True``.

        pattern (`str`, `callable`, `Pattern`, optional):
            If set, only queries matching this pattern will be handled.
            You can specify a regex-like string which will be matched
            against the message, a callable function that returns `True`
            if a message is acceptable, or a compiled regex pattern.

    Example
        .. code-block:: python

            from telethon import events

            @client.on(events.InlineQuery)
            async def handler(event):
                builder = event.builder

                # Two options (convert user text to UPPERCASE or lowercase)
                await event.answer([
                    builder.article('UPPERCASE', text=event.text.upper()),
                    builder.article('lowercase', text=event.text.lower()),
                ])
    NF)Úblacklist_usersÚfuncÚpatternc                   sl   t ƒ j|||d� t|tƒrt |¡j| _d S |rt|ƒr"|| _d S t	|dƒr2t|jƒr2|j| _d S t
dƒ‚)N)Úblacklist_chatsr   ÚmatchzInvalid pattern type given)ÚsuperÚ__init__Ú
isinstanceÚstrÚreÚcompiler   r   ÚcallableÚhasattrÚ	TypeError)ÚselfÚusersr   r   r   ©Ú	__class__© úV/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/events/inlinequery.pyr   2   s   

zInlineQuery.__init__c                 C   s   t |tjƒr|  |¡S d S ©N)r   r	   ÚUpdateBotInlineQueryÚEvent)ÚclsÚupdateÚothersÚself_idr    r    r!   Úbuild?   s   
ÿzInlineQuery.buildc                    s,   | j r|   |j¡}|sd S ||_tƒ  |¡S r"   )r   ÚtextÚpattern_matchr   Úfilter)r   Úeventr   r   r    r!   r,   D   s   zInlineQuery.filterc                       sŽ   e Zd ZdZ‡ fdd„Z‡ fdd„Zedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
edd„ ƒZ	dddddddœdd„Zedd„ ƒZ‡  ZS )zInlineQuery.Eventa
  
        Represents the event of a new callback query.

        Members:
            query (:tl:`UpdateBotInlineQuery`):
                The original :tl:`UpdateBotInlineQuery`.

                Make sure to access the `text` property of the query if
                you want the text rather than the actual query object.

            pattern_match (`obj`, optional):
                The resulting object from calling the passed ``pattern``
                function, which is ``re.compile(...).match`` by default.
        c                    s:   t ƒ jt |j¡d� t | |j¡ || _d | _d| _d S )N)Ú	chat_peerF)	r   r   r	   ÚPeerUserÚuser_idr   Úqueryr+   Ú	_answered)r   r1   r   r    r!   r   \   s
   
zInlineQuery.Event.__init__c                    s,   t ƒ  |¡ t | j| j|j¡\| _| _d S r"   )	r   Ú_set_clientr   Ú_get_entity_pairÚ	sender_idÚ	_entitiesÚ_mb_entity_cacheÚ_senderÚ_input_sender)r   Úclientr   r    r!   r3   c   s   ÿzInlineQuery.Event._set_clientc                 C   ó   | j jS )zI
            Returns the unique identifier for the query ID.
            )r1   Úquery_id©r   r    r    r!   Úidh   ó   zInlineQuery.Event.idc                 C   s   | j j S )zR
            Returns the text the user used to make the inline query.
            )r1   r=   r    r    r!   r*   o   r?   zInlineQuery.Event.textc                 C   r;   )z¥
            The string the user's client used as an offset for the query.
            This will either be empty or equal to offsets passed to `answer`.
            )r1   Úoffsetr=   r    r    r!   r@   v   s   zInlineQuery.Event.offsetc                 C   r;   )z×
            If the user location is requested when using inline mode
            and the user's device is able to send it, this will return
            the :tl:`GeoPoint` with the position of the user.
            )r1   Úgeor=   r    r    r!   rA   ~   s   zInlineQuery.Event.geoc                 C   s   t  | j¡S )z~
            Returns a new `InlineBuilder
            <telethon.tl.custom.inlinebuilder.InlineBuilder>` instance.
            )r   ÚInlineBuilderÚ_clientr=   r    r    r!   Úbuilder‡   s   zInlineQuery.Event.builderNr   FÚ )ÚgalleryÚnext_offsetÚprivateÚ	switch_pmÚswitch_pm_paramc          	      ƒ   s€   �ˆ j rdS |r!‡ fdd„|D ƒ}t |¡I dH  dd„ |D ƒ}ng }|r+t ||¡}ˆ  tjjˆ j	j
||||||d�¡I dH S )a¤	  
            Answers the inline query with the given results.

            See the documentation for `builder` to know what kind of answers
            can be given.

            Args:
                results (`list`, optional):
                    A list of :tl:`InputBotInlineResult` to use.
                    You should use `builder` to create these:

                    .. code-block:: python

                        builder = inline.builder
                        r1 = builder.article('Be nice', text='Have a nice day')
                        r2 = builder.article('Be bad', text="I don't like you")
                        await inline.answer([r1, r2])

                    You can send up to 50 results as documented in
                    https://core.telegram.org/bots/api#answerinlinequery.
                    Sending more will raise ``ResultsTooMuchError``,
                    and you should consider using `next_offset` to
                    paginate them.

                cache_time (`int`, optional):
                    For how long this result should be cached on
                    the user's client. Defaults to 0 for no cache.

                gallery (`bool`, optional):
                    Whether the results should show as a gallery (grid) or not.

                next_offset (`str`, optional):
                    The offset the client will send when the user scrolls the
                    results and it repeats the request.

                private (`bool`, optional):
                    Whether the results should be cached by Telegram
                    (not private) or by the user's client (private).

                switch_pm (`str`, optional):
                    If set, this text will be shown in the results
                    to allow the user to switch to private messages.

                switch_pm_param (`str`, optional):
                    Optional parameter to start the bot with if
                    `switch_pm` was used.

            Example:

                .. code-block:: python

                    @bot.on(events.InlineQuery)
                    async def handler(event):
                        builder = event.builder

                        rev_text = event.text[::-1]
                        await event.answer([
                            builder.article('Reverse text', text=rev_text),
                            builder.photo('/path/to/photo.jpg')
                        ])
            Nc                    s   g | ]}ˆ   |¡‘qS r    )Ú
_as_future©Ú.0Úxr=   r    r!   Ú
<listcomp>Ô   s    z,InlineQuery.Event.answer.<locals>.<listcomp>c                 S   s   g | ]}|  ¡ ‘qS r    )ÚresultrL   r    r    r!   rO   Ý   s    )r<   ÚresultsÚ
cache_timerF   rG   rH   rI   )r2   ÚasyncioÚwaitr	   ÚInlineBotSwitchPMrC   r
   ÚmessagesÚSetInlineBotResultsRequestr1   r<   )	r   rQ   rR   rF   rG   rH   rI   rJ   Úfuturesr    r=   r!   Úanswer�   s*   €Aù
ÿzInlineQuery.Event.answerc                 C   s.   t  | ¡r
t | ¡S t ¡  ¡ }| | ¡ |S r"   )ÚinspectÚisawaitablerS   Úensure_futurer   Úget_running_loopÚcreate_futureÚ
set_result)ÚobjÚfr    r    r!   rK   ð   s
   


zInlineQuery.Event._as_future)Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r3   Úpropertyr>   r*   r@   rA   rD   rY   ÚstaticmethodrK   Ú__classcell__r    r    r   r!   r$   M   s*    




ÿýar$   r"   )NN)rb   rc   rd   re   r   Úclassmethodr)   r,   r   r   r$   rh   r    r    r   r!   r      s    %ÿÿ	r   )rZ   r   rS   Úcommonr   r   r   rE   r   r   Útlr	   r
   r   Útl.custom.sendergetterr   r   r    r    r    r!   Ú<module>   s    