U
    Vh#                     @   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                       sX   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                   sj   t  j|||d t|tr,t|j| _n:|r8t|r@|| _n&t	|dr^t|jr^|j| _nt
dd S )N)blacklist_chatsr   matchzInvalid pattern type given)super__init__
isinstancestrrecompiler   r   callablehasattr	TypeError)selfusersr   r   r   	__class__ _/var/www/html/arya_register/venvv2/lib/python3.8/site-packages/telethonv2/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   s   | j jS )zI
            Returns the unique identifier for the query ID.
            )r1   query_idr   r    r    r!   idh   s    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   s    zInlineQuery.Event.textc                 C   s   | j jS )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   s   | j jS )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!   r?   ~   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 }|rTt||} 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    )resultrJ   r    r    r!   rM      s     )r;   results
cache_timerD   rE   rF   rG   )r2   asynciowaitr	   InlineBotSwitchPMrA   r
   messagesSetInlineBotResultsRequestr1   r;   )	r   rO   rP   rD   rE   rF   rG   rH   futuresr    r<   r!   answer   s(    AzInlineQuery.Event.answerc                 C   s.   t | rt| S t  }||  |S r"   )inspectisawaitablerQ   ensure_futurer   get_running_loopcreate_future
set_result)objfr    r    r!   rI      s
    


zInlineQuery.Event._as_future)Nr   )__name__
__module____qualname____doc__r   r3   propertyr=   r*   r>   r?   rB   rW   staticmethodrI   __classcell__r    r    r   r!   r$   M   s0   




      ar$   )N)NN)r`   ra   rb   rc   r   classmethodr)   r,   r   r   r$   rf   r    r    r   r!   r      s   %   	r   )rX   r   rQ   commonr   r   r   rC   r   r   tlr	   r
   r   Ztl.custom.sendergetterr   r   r    r    r    r!   <module>   s   