o
    XzjÉ#  ã                   @   sP   d dl Z ddlmZmZmZmZ ddlmZ ddlm	Z	 eG dd„ deƒƒZ
dS )	é    Né   )ÚEventBuilderÚEventCommonÚname_inner_eventÚ_into_id_seté   )Úutils)Útypesc                	       sj   e Zd ZdZdddddddddœ‡ fdd„Z‡ fdd„Zedd	d
„ƒZ‡ fdd„ZG dd„ de	ƒZ
‡  ZS )Ú
NewMessageaÁ  
    Occurs whenever a new text message or a message with media arrives.

    Args:
        incoming (`bool`, optional):
            If set to `True`, only **incoming** messages will be handled.
            Mutually exclusive with ``outgoing`` (can only set one of either).

        outgoing (`bool`, optional):
            If set to `True`, only **outgoing** messages will be handled.
            Mutually exclusive with ``incoming`` (can only set one of either).

        from_users (`entity`, optional):
            Unlike `chats`, this parameter filters the *senders* of the
            message. That is, only messages *sent by these users* will be
            handled. Use `chats` if you want private messages with this/these
            users. `from_users` lets you filter by messages sent by *one or
            more* users across the desired chats (doesn't need a list).

        forwards (`bool`, optional):
            Whether forwarded messages should be handled or not. By default,
            both forwarded and normal messages are included. If it's `True`
            *only* forwards will be handled. If it's `False` only messages
            that are *not* forwards will be handled.

        pattern (`str`, `callable`, `Pattern`, optional):
            If set, only messages 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

            import asyncio
            from telethon import events

            @client.on(events.NewMessage(pattern='(?i)hello.+'))
            async def handler(event):
                # Respond whenever someone says "Hello" and something else
                await event.reply('Hey!')

            @client.on(events.NewMessage(outgoing=True, pattern='!ping'))
            async def handler(event):
                # Say "!pong" whenever you send "!ping", then delete both messages
                m = await event.respond('!pong')
                await asyncio.sleep(5)
                await client.delete_messages(event.chat_id, [event.id, m.id])
    NF)Úblacklist_chatsÚfuncÚincomingÚoutgoingÚ
from_usersÚforwardsÚpatternc          	   
      s  |r	|r	d  }}n'|d ur|d u r| }n|d ur!|d u r!| }nt dd„ ||fD ƒƒr0tdƒ‚tƒ j|||d� || _|| _|| _|| _t|t	ƒrRt
 |¡j| _n|rXt|ƒr\|| _nt|dƒrkt|jƒrk|j| _ntdƒ‚t dd„ | j| j| j| j| j| j| j| jfD ƒƒ| _d S )Nc                 s   s   � | ]
}|d uo
| V  qd S ©N© ©Ú.0Úxr   r   úU/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/events/newmessage.pyÚ	<genexpr>D   s   € z&NewMessage.__init__.<locals>.<genexpr>zNDon't create an event handler if you don't want neither incoming nor outgoing!)r   r   ÚmatchzInvalid pattern type givenc                 s   s   � | ]}|d u V  qd S r   r   r   r   r   r   r   W   s   € )ÚallÚ
ValueErrorÚsuperÚ__init__r   r   r   r   Ú
isinstanceÚstrÚreÚcompiler   r   ÚcallableÚhasattrÚ	TypeErrorÚchatsr   Ú	_no_check)	Úselfr%   r   r   r   r   r   r   r   ©Ú	__class__r   r   r   ;   s0   


þzNewMessage.__init__c                 ƒ   s,   �t ƒ  |¡I d H  t|| jƒI d H | _d S r   )r   Ú_resolver   r   )r'   Úclientr(   r   r   r*   \   s   €zNewMessage._resolvec                 C   s  t |tjtjfƒrt |jtjƒsd S |  |j¡}|S t |tjƒrQ|  tj|j|j	|j
|j|jt |j¡t |jr:|n|j¡|j|j|j|j|j|j|jd�¡}|S t |tjƒrˆ|  tj|j|j	|j
|j|jt |jrl|n|j¡t |j¡|j|j|j|j|j|j|jd�¡}|S d S )N)ÚoutÚ	mentionedÚmedia_unreadÚsilentÚidÚpeer_idÚfrom_idÚmessageÚdateÚfwd_fromÚ
via_bot_idÚreply_toÚentitiesÚ
ttl_period)r,   r-   r.   r/   r0   r2   r1   r3   r4   r5   r6   r7   r8   r9   )r   r	   ÚUpdateNewMessageÚUpdateNewChannelMessager3   ÚMessageÚEventÚUpdateShortMessager,   r-   r.   r/   r0   ÚPeerUserÚuser_idr4   r5   r6   r7   r8   r9   ÚUpdateShortChatMessager2   ÚPeerChatÚchat_id)ÚclsÚupdateÚothersÚself_idÚeventr   r   r   Úbuild`   sX   
ÿ&Û
ò$ì
òþzNewMessage.buildc                    s    | j r|S | jr|jjrd S | jr|jjsd S | jd ur)t| jƒt|jjƒkr)d S | jd ur7|jj	| jvr7d S | j
rJ|  
|jjpAd¡}|sGd S ||_tƒ  |¡S )NÚ )r&   r   r3   r,   r   r   Úboolr5   r   Ú	sender_idr   Úpattern_matchr   Úfilter)r'   rH   r   r(   r   r   rN   Ž   s$   

zNewMessage.filterc                       s<   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Z‡  ZS )
zNewMessage.EventaË  
        Represents the event of a new message. This event can be treated
        to all effects as a `Message <telethon.tl.custom.message.Message>`,
        so please **refer to its documentation** to know what you can do
        with this event.

        Members:
            message (`Message <telethon.tl.custom.message.Message>`):
                This is the only difference with the received
                `Message <telethon.tl.custom.message.Message>`, and will
                return the `telethon.tl.custom.message.Message` itself,
                not the text.

                See `Message <telethon.tl.custom.message.Message>` for
                the rest of available members and methods.

            pattern_match (`obj`):
                The resulting object from calling the passed ``pattern`` function.
                Here's an example using a string (defaults to regex match):

                >>> from telethon import TelegramClient, events
                >>> client = TelegramClient(...)
                >>>
                >>> @client.on(events.NewMessage(pattern=r'hi (\w+)!'))
                ... async def handler(event):
                ...     # In this case, the result is a ``Match`` object
                ...     # since the `str` pattern was converted into
                ...     # the ``re.compile(pattern).match`` function.
                ...     print('Welcomed', event.pattern_match.group(1))
                ...
                >>>
        c                    s6   d| j d< tƒ j|j|jt|jƒd� d | _|| _d S )NFÚ_init)Ú	chat_peerÚmsg_idÚ	broadcast)	Ú__dict__r   r   r1   r0   rK   ÚpostrM   r3   )r'   r3   r(   r   r   r   Ç   s   

ÿ
zNewMessage.Event.__init__c                    s0   t ƒ  |¡ | j}| || jd ¡ d| jd< d S )NTrO   )r   Ú_set_clientr3   Ú_finish_initÚ	_entitiesrS   )r'   r+   Úmr(   r   r   rU   Ï   s   zNewMessage.Event._set_clientc                 C   s    || j v r
| j | S t| j|ƒS r   )rS   Úgetattrr3   )r'   Úitemr   r   r   Ú__getattr__Õ   s   

zNewMessage.Event.__getattr__c                 C   s4   | j d r
|| j v r|| j |< d S t| j||ƒ d S )NrO   )rS   Úsetattrr3   )r'   ÚnameÚvaluer   r   r   Ú__setattr__Û   s   zNewMessage.Event.__setattr__)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rU   r[   r_   Ú__classcell__r   r   r(   r   r=   ¦   s     r=   r   )NN)r`   ra   rb   rc   r   r*   ÚclassmethodrI   rN   r   r=   rd   r   r   r(   r   r
      s    1þ!-r
   )r    Úcommonr   r   r   r   rJ   r   Útlr	   r
   r   r   r   r   Ú<module>   s    