U
    h#                     @   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)Zblacklist_chatsr   matchzInvalid pattern type given)super__init__
isinstancestrrecompiler   r   callablehasattr	TypeError)selfZusersr   r   r   	__class__ ?/tmp/pip-unpacked-wheel-c81u5j2r/telethon/events/inlinequery.pyr   2   s    

zInlineQuery.__init__c                 C   s   t |tjr| |S d S N)r   r	   ZUpdateBotInlineQueryEvent)clsupdateZ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)Z	chat_peerF)	r   r   r	   ZPeerUserZuser_idr   queryr'   	_answered)r   r*   r   r   r   r   \   s
    zInlineQuery.Event.__init__c                    s,   t  | t| j| j|j\| _| _d S r    )	r   _set_clientr   Z_get_entity_pairZ	sender_idZ	_entitiesZ_mb_entity_cacheZ_senderZ_input_sender)r   clientr   r   r   r,   c   s      zInlineQuery.Event._set_clientc                 C   s   | j jS )zI
            Returns the unique identifier for the query ID.
            )r*   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.
            )r*   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`.
            )r*   offsetr/   r   r   r   r1   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.
            )r*   geor/   r   r   r   r2   ~   s    zInlineQuery.Event.geoc                 C   s   t | jS )z~
            Returns a new `InlineBuilder
            <telethon.tl.custom.inlinebuilder.InlineBuilder>` instance.
            )r   Z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   )resultr<   r   r   r   r?      s     )r.   results
cache_timer6   r7   r8   r9   )r+   asynciowaitr	   ZInlineBotSwitchPMr3   r
   messagesZSetInlineBotResultsRequestr*   r.   )	r   rA   rB   r6   r7   r8   r9   r:   Zfuturesr   r/   r   answer   s(    AzInlineQuery.Event.answerc                 C   s.   t | rt| S t  }||  |S r    )inspectisawaitablerC   Zensure_futurer   Zget_running_loopZcreate_futureZ
set_result)objfr   r   r   r;      s
    


zInlineQuery.Event._as_future)Nr   )__name__
__module____qualname____doc__r   r,   propertyr0   r&   r1   r2   r4   rF   staticmethodr;   __classcell__r   r   r   r   r!   M   s0   




      ar!   )N)NN)rK   rL   rM   rN   r   classmethodr%   r(   r   r   r!   rQ   r   r   r   r   r      s   %   	r   )rG   r   rC   commonr   r   r   r5   r   r   tlr	   r
   r   Ztl.custom.sendergetterr   r   r   r   r   r   <module>   s   