o
    XzjZ  ã                   @   sn   d dl Z ddlmZmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZmZmZ G d
d„ dƒZdS )é    Né   )ÚTLObjectÚtypes)ÚSaveDraftRequest)ÚDraftMessageé   )ÚRPCError)Úmarkdown)Úget_input_peerÚget_peerÚget_peer_idc                   @   sœ   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	edd„ ƒZ
edd„ ƒZedd„ ƒZ		d"dd„Zd#dd„Zdd„ Zdd„ Zdd„ Zd d!„ ZdS )$ÚDraftaÝ  
    Custom class that encapsulates a draft on the Telegram servers, providing
    an abstraction to change the message conveniently. The library will return
    instances of this class when calling :meth:`get_drafts()`.

    Args:
        date (`datetime`):
            The date of the draft.

        link_preview (`bool`):
            Whether the link preview is enabled or not.

        reply_to_msg_id (`int`):
            The message ID that the draft will reply to.
    c                 C   s˜   || _ t|ƒ| _|| _|rt|ƒnd | _|rt|tƒs#tdd d d d ƒ}t 	|j
|j¡| _|j
| _|j| _|j | _t|jtjƒrG|jj| _d S d | _d S )NÚ )Ú_clientr   Ú_peerÚ_entityr
   Ú_input_entityÚ
isinstancer   r	   ÚunparseÚmessageÚentitiesÚ_textÚ	_raw_textÚdateÚ
no_webpageÚlink_previewÚreply_tor   ÚInputReplyToMessageÚreply_to_msg_id)ÚselfÚclientÚentityÚdraft© r#   úS/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/tl/custom/draft.pyÚ__init__   s   

&zDraft.__init__c                 C   ó   | j S )zQ
        The entity that belongs to this dialog (user, chat or channel).
        )r   ©r   r#   r#   r$   r!   *   s   zDraft.entityc                 C   sJ   | j s"z| jj t| jdd�¡ ¡ | _ W | j S  ty!   Y | j S w | j S )z.
        Input version of the entity.
        F)Úadd_mark)r   r   Ú_mb_entity_cacheÚgetr   r   Ú_as_input_peerÚAttributeErrorr'   r#   r#   r$   Úinput_entity1   s   ÿÿýýzDraft.input_entityc                 Ã   sR   �| j s&|  ¡ I dH r&z| j | j¡I dH | _W | jS  ty%   Y | jS w | jS )zJ
        Returns `entity` but will make an API call if necessary.
        N)r!   Úget_input_entityr   Ú
get_entityr   r   Ú
ValueErrorr'   r#   r#   r$   r/   ?   s   €ÿýýzDraft.get_entityc                 Ã   s   �| j S )zP
        Returns `input_entity` but will make an API call if necessary.
        )r-   r'   r#   r#   r$   r.   L   s   €zDraft.get_input_entityc                 C   r&   )z…
        The markdown text contained in the draft. It will be
        empty if there is no text (and hence no draft is set).
        ©r   r'   r#   r#   r$   ÚtextT   ó   z
Draft.textc                 C   r&   )zŽ
        The raw (text without formatting) contained in the draft.
        It will be empty if there is no text (thus draft not set).
        )r   r'   r#   r#   r$   Úraw_text\   r3   zDraft.raw_textc                 C   s   | j  S )zM
        Convenience bool to determine if the draft is empty or not.
        r1   r'   r#   r#   r$   Úis_emptyd   s   zDraft.is_emptyNr   r#   c              	   Ã   s¬   �|du r| j }|dkr| j}|du r| j}| j ||¡I dH \}}|  t| j|| |du r0dnt |¡|d�¡I dH }|rT|| _ || _	|| _|| _t
j
jt
jjd�| _|S )aT  
        Changes the draft message on the Telegram servers. The changes are
        reflected in this object.

        :param str text: New text of the draft.
                         Preserved if left as None.

        :param int reply_to: Message ID to reply to.
                             Preserved if left as 0, erased if set to None.

        :param bool link_preview: Whether to attach a web page preview.
                                  Preserved if left as None.

        :param str parse_mode: The parse mode to be used for the text.
        :return bool: `True` on success.
        Nr   )Úpeerr   r   r   r   )Útz)r   r   r   r   Ú_parse_message_textr   r   r   r   r   ÚdatetimeÚnowÚtimezoneÚutcr   )r   r2   r   Ú
parse_moder   r4   r   Úresultr#   r#   r$   Úset_messagek   s.   €ÿûzDraft.set_messageTc                 Ã   s,   �| j j| j| j| j| j||d�I dH  dS )zŸ
        Sends the contents of this draft to the dialog. This is just a
        wrapper around ``send_message(dialog.input_entity, *args, **kwargs)``.
        )r   r   r=   Úclear_draftN)r   Úsend_messager   r2   r   r   )r   Úclearr=   r#   r#   r$   Úsend›   s   €ýz
Draft.sendc                 Ã   s   �| j dd�I dH S )zD
        Deletes this draft, and returns `True` on success.
        r   )r2   N)r?   r'   r#   r#   r$   Údelete¦   s   €zDraft.deletec              
   C   sN   z| j }W n ty } z|}W Y d }~nd }~ww d| j|| j| j| jdœS )Nr   )Ú_r2   r!   r   r   r   )r!   r   r2   r   r   r   )r   r!   Úer#   r#   r$   Úto_dict¬   s   
€ÿúzDraft.to_dictc                 C   s   t  |  ¡ ¡S )N©r   Úpretty_formatrG   r'   r#   r#   r$   Ú__str__»   s   zDraft.__str__c                 C   s   t j|  ¡ dd�S )Nr   )ÚindentrH   r'   r#   r#   r$   Ú	stringify¾   s   zDraft.stringify)Nr   r#   N)Tr#   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   Úpropertyr!   r-   r/   r.   r2   r4   r5   r?   rC   rD   rG   rJ   rL   r#   r#   r#   r$   r      s.    





þ
0r   )r9   r   r   r   Úfunctions.messagesr   r   Úerrorsr   Ú
extensionsr	   Úutilsr
   r   r   r   r#   r#   r#   r$   Ú<module>   s    