a
    fiZ                     @   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 | _|r6t|tsFtdd d d d }t	|j
|j| _|j
| _|j| _|j | _t|jtjr|jjn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entityZdraft r"   M/home/ec2-user/.local/lib/python3.9/site-packages/telethon/tl/custom/draft.py__init__   s    

zDraft.__init__c                 C   s   | 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   sB   | j s<z"| jjt| jdd | _ W n ty:   Y n0 | j S )z.
        Input version of the entity.
        F)Zadd_mark)r   r   Z_mb_entity_cachegetr   r   Z_as_input_peerAttributeErrorr%   r"   r"   r#   input_entity1   s    zDraft.input_entityc                    sH   | j sB|  I dH rBz| j| jI dH | _W n ty@   Y n0 | 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   s   | j S )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   s    z
Draft.textc                 C   s   | j S )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\   s    zDraft.raw_textc                 C   s   | j  S )zM
        Convenience bool to determine if the draft is empty or not.
        r,   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 r^dnt||dI dH }|r|| _ || _	|| _|| _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   Z_parse_message_textr   r   r   r   r   datetimenowtimezoneutcr   )r   r-   r   
parse_moder   r.   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   r6   Zclear_draftN)r   send_messager   r-   r   r   )r   clearr6   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   )r-   N)r8   r%   r"   r"   r#   delete   s    zDraft.deletec              
   C   sN   z
| j }W n( ty2 } z|}W Y d }~n
d }~0 0 d| j|| j| j| jdS )Nr   )_r-   r!   r   r   r   )r!   r   r-   r   r   r   )r   r!   er"   r"   r#   to_dict   s    
zDraft.to_dictc                 C   s   t |  S )Nr   Zpretty_formatr?   r%   r"   r"   r#   __str__   s    zDraft.__str__c                 C   s   t j|  ddS )Nr   )indentr@   r%   r"   r"   r#   	stringify   s    zDraft.stringify)Nr   r"   N)Tr"   )__name__
__module____qualname____doc__r$   propertyr!   r(   r*   r)   r-   r.   r/   r8   r;   r<   r?   rA   rC   r"   r"   r"   r#   r      s,   




  
0
r   )r2   r   r   r   Zfunctions.messagesr   r   errorsr   
extensionsr	   utilsr
   r   r   r   r"   r"   r"   r#   <module>   s   