
    sojG                     .   d Z ddlZddlmZmZmZmZmZmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZ  ej        e          Z G d d          Z G d	 d
          ZddededefdZ  G d d          Z! G d d          Z" G d d          Z#dS )z
Drop-in replacements for telegram.Message / telegram.Update / telegram.CallbackQuery
/ telegram.InlineQuery, built from live Telethon events or raw Telethon message
objects. See runtime.py for where these get constructed from actual Telethon
events.
    N)PeerUserMessageActionChatAddUserMessageActionChatDeleteUserMessageActionChatJoinedByLinkMessageActionChatMigrateToMessageActionTopicCreate   )translate_errors)ChatTelegramUser	PhotoSizeVideo	AnimationPoll
PollOptionMessageOriginUserMessageOriginChatMessageOriginChannelMessageOriginHiddencoerce_peerc                       e Zd ZdefdZdS )ForumTopicCreatednamec                     || _         d S Nr   )selfr   s     /app/src/tg_compat/_message.py__init__zForumTopicCreated.__init__'   s    			    N)__name__
__module____qualname__strr    r    r   r   r   &   s/        S      r    r   c                       e Zd ZdZd Zed             Zd Zd Zdde	fdZ
dde	d
d fdZed             Zed             Zeddede	d
d fd            Zedd            ZdS )MessagezKMirrors telegram.Message (only the attributes/methods this codebase reads).c                    |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d	          | _	        |                     d
          | _
        |                     d          pg | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          pd| _        |                     d          | _        |                     d          | _        |                     d          | _        |                     dd          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        |                     d          | _        d S )N
message_idtextchat	from_usersender_chatreply_to_messageforward_originsticker	animationvideophotodicevia_botpollnew_chat_membersr%   left_chat_membermigrate_to_chat_idmessage_thread_idis_topic_messageFauthor_signatureforum_topic_created_client_bot_tl_message)getr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   )r   kwargss     r   r   zMessage.__init__.   s    **\22JJv&&	 **V,,	'-zz+'>'>!'M!:!:+1::6H+I+I$jj)9::zz),,$*JJ{$;$;"JJw//
ZZ((.B
JJv&&	%+ZZ	%:%: **V,,	 &

+= > > D".4jj9K.L.L"(**-A"B"B!',?!@!@ &

+=u E E &

+= > >6<jjAV6W6W zz),,JJv&&	!::m44r    c                     | j         S )a  Mirrors telegram.Message.id, PTB's alias for message_id (added for
        naming consistency with Chat.id/User.id/CallbackQuery.id). Several
        screens in this codebase (daily reward, devil fruit trade, RPS/RR
        games) read message.id directly rather than message.message_id.)r)   r   s    r   idz
Message.idI   s     r    c                     | j         S r   r?   rD   s    r   get_botzMessage.get_botQ   
    yr    c                    K   t          | j                            | j        j        p| j        j        | j        g                     d {V  d S r   )r
   r>   delete_messagesr+   _entityrE   r)   rD   s    r   deletezMessage.deleteT   sh      L(():)JdilT_L]^^
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r    Tdisable_notificationc                    K   t          | j                            | j        j        p| j        j        | j        p| j        |                      d {V  d S )N)notify)r
   r>   pin_messager+   rL   rE   r@   r)   )r   rN   s     r   pinzMessage.pinY   s~      L$$	!1TY\ 3DO// %  
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r    Freturnc                 d  K   t          |          }t          | j                            || j        p| j        | j        j        p| j        j        |                     d {V }t          |t                    r|d         n|}t                              || j        | j        d           d {V S )N)	from_peersilentr   F)fetch_reply)r   r
   r>   forward_messagesr@   r)   r+   rL   rE   
isinstancelistr'   from_telethonr?   )r   chat_idrN   result	tl_results        r   forwardzMessage.forwardb   s      g&&'L)) 3DO)+;ty|+	 *  
 
 
 
 
 
 
 
 ",FD!9!9EF1IIv	**9dlDI[`*aaaaaaaaar    c           	        K   t          | dd           }|d S t          |dd           }|!t          |dd           }t          |          S 	 t          |t                    r>|                    |           d {V }t          t          j        ||                    S |                    |           d {V }t          j        ||           d {V }t          |dd           }|r!t          ||t          |dd                     S t          |	          S # t          $ r  t                              d
d           Y d S w xY w)Nfwd_fromfrom_id	from_name)sender_user_name)sender_userchannel_postpost_author)r+   r)   r<   )r-   z Could not resolve forward originT)exc_info)getattrr   rY   r   
get_entityr   r   from_entityr   r   r   	Exceptionloggerwarning)
tl_messageclientfwdrb   r   entityr+   rf   s           r   _build_forward_originzMessage._build_forward_origino   s     j*d33;4#y$//?3T22D&====	'8,, ;%0099999999(\5MfV\5]5]^^^^%0099999999!-ff========&sNDAA /!#/)0mT)J)J   
 )T:::: 	 	 	NN=NMMM44	s    AD  A)D 
D &EEc                   K   t          | dd           }t          |dd           }|d S t          |dd           pg }g }|D ]J}t          |dd           }|                    |r t          |dd           pt          |          nd           Kt          t          t          |dd           dd           pdd |D             t	          t          |dd	                    
          S )Nmediar6   answersr*    questionc                 .    g | ]}t          |           S ))r*   )r   ).0ts     r   
<listcomp>z'Message._build_poll.<locals>.<listcomp>   s#    >>>AZQ'''>>>r    multiple_choiceF)rx   optionsallows_multiple_answers)ri   appendr$   r   bool)ro   ru   r6   rv   option_textsatext_objs          r   _build_pollzMessage._build_poll   s     
GT22ufd++<4$	4006B 	f 	fAq&$//HT\ d&$ ? ? P3x==bdeeeeWT:t<<fdKKQr>>>>>$(7H%)P)P$Q$Q
 
 
 	
r    Nr+   rW   c           
      x  K   |dS |<|                                  d{V }t          j        |||j                   d{V }d}d}t	          t          |dd          t                    s|j        E|j        t          j	        k    r0|
                                 d{V }	t          j        |	|          }n>|j        7|
                                 d{V }
|
t          j        |
|           d{V }d}t          |dd          rG	 |                    |j                   d{V }t          j        ||          }n# t          $ r Y nw xY wg }t          |dd          rt          |j        |          g}t          |dd          rt#          |j        |          nd}t          |dd          rt'          |j        |          nd}t          |dd          }t          |d	d          }d}d}|rt          |d
d          t          |j        dd          }|r	 |                    |j        p|j        |           d{V }n# t          $ r d}Y nw xY w|dt          |dd          }t	          |t2                    rt5          |j                  }|                     ||||d           d{V }|||_        |                     ||           d{V }|                     |           d{V }tA          t          t          |d
d          dd                    }t          t          |d
d          dd          p|rt          |j        dd          nd} | d%i d|j        d|j!        pdd|d|d|d|d|d|d|d|d|d	|d|d|d|d|dt          |d d          d!|d"|d#|d$|S )&zBBuild a compat Message from a real (non-service) Telethon message.Nr\   rb   
via_bot_idr3   r2   gifr0   r4   reply_toreply_to_msg_id)idsactionr   F)r+   rW   forum_topicreply_to_top_idr)   r*   r+   r,   r-   r.   r/   r1   r5   r6   r:   r;   r<   rg   r=   r>   r?   r@   r%   )"get_chatr   rk   r\   rY   ri   r   rb   typePRIVATE
get_senderr   rj   r   rl   r   r3   r   r2   r   r   r   get_messagesrL   rE   r   r   titler[   r=   rs   r   r   message)clsro   rp   botr+   rW   tl_chatr,   r-   sendersender_entityr5   via_bot_entityr3   r2   r1   r0   r4   r.   r=   r   repliedr   r/   r6   r;   r:   s                              r   r[   zMessage.from_telethon   sl      4<&//11111111G)'6:CUVVVVVVVVVD	gj)T::HEE 	L&49+D+D%0022222222F$0@@II+","7"7"9"9999999M($($4]F$K$KKKKKKK:|T22 	'-'8'89N'O'O!O!O!O!O!O!O&2>6JJ    :w-- 	:z/889E3::wPT3U3U_j&///[_9@UTX9Y9YcIjnf555_c	*i66z6400" 	S7:z4@@L%j&9;LdSSO S#$*$7$78OUd$7$e$eeeeeeeGG  # # #"GGG#&$Wh==F!&*BCC S.?V\.R.R.R+-0->->4U .? . . ( ( ( ( ( ($ (3?R(<"88VLLLLLLLL__Z00000000
J(M(M}^c d dee#GJ
D$I$IK\^bcc 
EU_GJ'):DAAA[_ 	 s 
 
 
!}}
#+t
 
  i	

 $
 .-
 *>
 G
  i
 %
 %
 
 G
 
 0/
  .-!
" %ZEEE#
$ !4 3%
& F'
( )
* #
+
 	
s$   5E 
EE)I IIc                 J  K   |j         }|                                 d{V }t          j        |||j                   d{V }d}	 |                                 d{V }|rt          j        ||          nd}n# t          $ r d}Y nw xY w|L	 t          |dd          }	|	' |	             d{V }
|
t          j        |
|          }n# t          $ r Y nw xY w|&t          |dd          t          |j	                  }d}d}d}|t          |dd          nd}t          |t          f          rg }|j        D ]}|B|j        ;||j        k    r0|                    t          |j        d|j                             F	 |                    |           d{V }|                    t          j        ||                     # t          $ r Y w xY w|}nt          |t"                    r|r|gng }nt          |t$                    rG	 |                    |j                   d{V }t          j        ||          }n<# t          $ r Y n0w xY wt          |t(                    rt+          d	|j                   } | t          |d
d          ||||||||	  	        S )zBuild a compat Message from a Telethon ChatAction event (join / leave /
        migrate / topic creation), matching how Bot API folds these into optional
        fields on a single Message object instead of separate update types.Nr   get_added_by	sender_id)rE   r   T)rE   is_botusernamez-100rE   )	r)   r+   r,   r7   r8   r9   r>   r?   r@   )action_messager   r   rk   r\   r   r   rl   ri   r   rY   r   usersrE   r   r   rj   r   r   user_idr   int
channel_id)r   eventrp   r   ro   r   r+   r,   r   r   added_by_entityr7   r8   r9   r   membersuidrr   s                     r   from_chat_actionzMessage.from_chat_action   s     
 )
((((((((%gvu}MMMMMMMMM		"'"2"2"4"4444444MKXb0GGG^bII 	 	 	III	&undCC+,8LNN&:&:&:&:&:&:O&2$0$<_f$U$U	   T!B!B!N  %888I!8B8NXt444TXf79:: %	AG|  ?sv'9cSVmm NN<36$QTQ]#^#^#^___#)#4#4S#9#9999999FNN<#;FF#K#KLLLL    D& =>> 	A.7?	{{R ;<< 		A%00@@@@@@@@#/#;FF#K#K      :;; 	A "%%?F,=%?%?!@!@sz466--1"

 

 

 
	
sI   	3A= =BB:C 
CCAG


GG5I 
II)T)F)NT)rS   r'   )r!   r"   r#   __doc__r   propertyrE   rH   rM   r   rR   r_   staticmethodrs   r   classmethodr   r[   r   r%   r    r   r'   r'   +   sQ       UU5 5 56   X  
 
 


 
d 
 
 
 
b b4 bI b b b b   \6 
 
 \
  W
 W
 W
Z^ W
js W
 W
 W
 [W
r h
 h
 h
 [h
 h
 h
r    r'   Fr*   
show_alert
cache_timec           
         K   ddl m} t           |  |t          |          |||                               d{V S )zShared implementation for answering a callback query by id, used by
    both CallbackQuery.answer() and Bot.answer_callback_query().r   )SetBotCallbackAnswerRequest)query_idr   alertr   N)telethon.tl.functions.messagesr   r
   r   )rp   callback_query_idr*   r   r   r   s         r   answer_callback_queryr   b  s       KJJJJJ!''.//% 	  	
 	
	 	 	 	 	 	 	 	 	r    c            	       >    e Zd ZdZddedededefdZdd	ed
efdZ	dS )CallbackQueryzMirrors telegram.CallbackQuery.NrE   datar   r,   c                 L    || _         || _        || _        || _        || _        d S r   )rE   r   r   r,   r>   )r   rE   r   r   r,   r>   s         r   r   zCallbackQuery.__init__v  s)    	"r    Fr*   r   c                 P   K   t          | j        | j        ||           d {V  d S )N)r*   r   )r   r>   rE   )r   r*   r   s      r   answerzCallbackQuery.answer}  s:      #DL$'Q[\\\\\\\\\\\\r    r   )NF)
r!   r"   r#   r   r$   r'   r   r   r   r   r%   r    r   r   r   s  s        )) 3 c G     ] ] ] ] ] ] ] ] ]r    r   c                   .    e Zd ZdZddededefdZd ZdS )	InlineQueryzMirrors telegram.InlineQuery.NrE   queryr,   c                 >    || _         || _        || _        || _        d S r   )rE   r   r,   _event)r   rE   r   r,   r   s        r   r   zInlineQuery.__init__  s"    
"r    c                 @   K   ddl m}  || j        |fi | d {V  d S )Nr	   )build_and_answer_inline_query)_inliner   r   )r   resultsrB   r   s       r   r   zInlineQuery.answer  sM      ::::::++DKKKFKKKKKKKKKKKr    r   )r!   r"   r#   r   r$   r   r   r   r%   r    r   r   r     s\        '' 3 s |    L L L L Lr    r   c                       e Zd ZdZ	 	 	 	 ddededefdZedefd            Z	ede
fd	            Zedefd
            Zd ZdS )UpdatezMirrors telegram.Update - a thin container the rest of the codebase reads
    .effective_user / .effective_chat / .effective_message / .message /
    .callback_query / .inline_query from.Nr   callback_queryinline_queryc                 >    || _         || _        || _        || _        d S r   )r   r   r   r?   )r   r   r   r   r?   s        r   r   zUpdate.__init__  s&     ,(			r    rS   c                 H    | j         | j         S | j        | j        j         S d S r   )r   r   rD   s    r   effective_messagezUpdate.effective_message  s,    <#<*&..tr    c                 &    | j         }||j        nd S r   )r   r+   )r   msgs     r   effective_chatzUpdate.effective_chat  s    $?sxx4r    c                 x    | j         | j         j        S | j        | j        j        S | j        | j        j        S d S r   )r   r,   r   r   rD   s    r   effective_userzUpdate.effective_user  sD    *&00($..<#<))tr    c                     | j         S r   rG   rD   s    r   rH   zUpdate.get_bot  rI   r    )NNNN)r!   r"   r#   r   r'   r   r   r   r   r   r   r   r   r   rH   r%   r    r   r   r     s        - -  (,$(
 

 &
 "	
 
 
 
 7    X 5 5 5 5 X5     X    r    r   )NFr   )$r   loggingtelethon.tl.typesr   r   r   r   r   r   _errorsr
   _typesr   r   r   r   r   r   r   r   r   r   r   r   	getLoggerr!   rm   r   r'   r$   r   r   r   r   r   r   r%   r    r   <module>r      s9                    & % % % % %                            
	8	$	$       
t
 t
 t
 t
 t
 t
 t
 t
n	  Y] sv    "] ] ] ] ] ] ] ]L L L L L L L L) ) ) ) ) ) ) ) ) )r    