
    > /i!                     8   d dl Z d dl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 d dlZd dlmZ d dlmZ d dlmZ d d	lmZ e	rd d
lmZ  eed      Z G d d      Z G d dej4                  j6                        Z G d dej4                  j:                        Zy)    N)
HTTPStatus)
SSLContext)TracebackType)TYPE_CHECKINGOptionalType)
HTTPServer)Update)
get_logger)ExtBot)BotUpdater)
class_namec            	       f    e Zd ZdZdZdededddee   fdZ	dd
ee
j                     dd	fdZddZy	)WebhookServerz6Thin wrapper around ``tornado.httpserver.HTTPServer``.)_http_serverlistenport
is_running_server_lock_shutdown_lockr   r   webhook_appWebhookAppClassssl_ctxc                     t        ||      | _        || _        || _        d| _        t        j                         | _        t        j                         | _        y )N)ssl_optionsF)	r	   r   r   r   r   asyncioLockr   r   )selfr   r   r   r   s        X/srv/sistema/bot/venv/lib/python3.12/site-packages/telegram/ext/_utils/webhookhandler.py__init__zWebhookServer.__init__7   sD     '{H	#LLN%lln    Nreadyreturnc                 F  K   | j                   4 d {    | j                  j                  | j                  | j                         d| _        ||j                          t        j                  d       d d d       d {    y 7 t7 # 1 d {  7  sw Y   y xY ww)N)addressTzWebhook Server started.)r   r   r   r   r   set_LOGGERdebug)r   r#   s     r    serve_foreverzWebhookServer.serve_foreverA   s     $$ 	5 	5$$TYY$D"DO 		MM34	5 	5 	5 	5 	5 	5 	5sE   B!BB!A B7B!B
B!
B!BBBB!c                   K   | j                   4 d {    | j                  s't        j                  d       	 d d d       d {    y d| _        | j                  j                          | j                  j                          d {    t        j                  d       d d d       d {    y 7 7 p7 .7 # 1 d {  7  sw Y   y xY ww)Nz.Webhook Server is already shut down. ReturningFzWebhook Server stopped)r   r   r(   r)   r   stopclose_all_connectionsr   s    r    shutdownzWebhookServer.shutdownK   s     && 	4 	4??NO	4 	4 	4 $DO""$##99;;;MM23	4 	4 	4 	4 <	4 	4 	4 	4ss   CB4C#B<CB6C>B<	B8
B<#C.B:/C6C8B<:C<CCC
CNr$   N)__name__
__module____qualname____doc__	__slots__strintr   r   r!   r   Eventr*   r/    r"   r    r   r   +   sX    @I--!$-3D-OWXbOc-5'--)@ 5D 54r"   r   c            
       ~    e Zd ZdZ	 ddedddej                  dee   fdZd	e	j                  j                  d
dfdZy)r   z!Application used in the WebserverNwebhook_pathbotr   update_queuesecret_tokenc                     |||d| _         | dt        | j                   fg}t        j                  j                  j                  | |       y )Nr=   r>   r?   z/?)shared_objectsTelegramHandlertornadowebApplicationr!   )r   r<   r=   r>   r?   handlerss         r    r!   zWebhookAppClass.__init__Y   sO     ((

 &b)?D<O<OPQ((x8r"   handlerr$   c                      y)zIOverrides the default implementation since we have our own logging setup.Nr:   )r   rH   s     r    log_requestzWebhookAppClass.log_requesth   s    r"   r0   )r2   r3   r4   r5   r7   r   Queuer   r!   rD   rE   RequestHandlerrJ   r:   r"   r    r   r   V   s]    + '+99 9 mm	9
 sm9X7;;#=#= X$ Xr"   r   c                       e Zd ZdZdZdZdddej                  dedd	fd
Z	ddZ
ddZddZdeee      dee   dee   dd	fdZy	)rC   z:BaseHandler that processes incoming requests from TelegramrA   )POSTr=   r   r>   r?   r$   Nc                 ^    || _         || _        || _        |rt        j	                  d       yy)zFInitialize for each request - that's the interface provided by tornadozLThe webhook server has a secret token, expecting it in incoming requests nowN)r=   r>   r?   r(   r)   )r   r=   r>   r?   s       r    
initializezTelegramHandler.initializet   s2     ((MM^ r"   c                 (    | j                  dd       y)zSets default headersContent-Typez!application/json; charset="utf-8"N)
set_headerr.   s    r    set_default_headersz#TelegramHandler.set_default_headers   s    (KLr"   c                   K   t         j                  d       | j                          | j                  j                  j                         }t        j                  |      }| j                  t        j                         t         j                  d|       	 t        j                  || j                        }ryt         j                  d|j                          t#        | j                  t$              r| j                  j'                  |       | j(                  j+                  |       d{    yy# t        $ r!}t         j                  d|       Y d}~d}~ww xY w7 3w)zHandle incoming POST requestzWebhook triggeredzWebhook received data: %szcSomething went wrong processing the data received from Telegram. Received data was *not* processed!exc_infoNz%Received Update with ID %d on Webhook)r(   r)   _validate_postrequestbodydecodejsonloads
set_statusr   OKr
   de_jsonr=   	Exceptioncritical	update_id
isinstancer   insert_callback_datar>   put)r   json_stringdataupdateexcs        r    postzTelegramHandler.post   s    )*ll''..0zz+&
&1;?	^^D$((3F MM7   $((F+--f5##''///   	5   	$ 0s=   BE" D3 7A5E",E -E"3	E<EE"EE"c                 L   | j                   j                  j                  dd      }|dk7  r-t        j                  j                  t        j                        | j                  | j                   j                  j                  d      }|sDt        j                  d       t        j                  j                  t        j                  d      || j                  k7  rEt        j                  d|       t        j                  j                  t        j                  d      yy)	z+Only accept requests with content type JSONrR   Nzapplication/jsonzX-Telegram-Bot-Api-Secret-Tokenz(Request did not include the secret token)reasonz&Request had the wrong secret token: %sz"Request had the wrong secret token)rY   headersgetrD   rE   	HTTPErrorr   	FORBIDDENr?   r(   r)   )r   	ct_headertokens      r    rX   zTelegramHandler._validate_post   s    LL((,,^TB	**++''
(<(<==(LL((,,-NOEHIkk++((1[ ,   )))FNkk++((1U ,   * )r"   typvaluetbc                     t         j                  d| j                  j                  d|r|r|r|||f       y|       y)z@Override the default logging and instead use our custom logging.z%s - %szException in TelegramHandlerrV   N)r(   r)   rY   	remote_ip)r   rt   ru   rv   s       r    log_exceptionzTelegramHandler.log_exception   sI     	LL""*),2c5"%	 	 	
 DI	 	 	
r"   r1   )r2   r3   r4   r5   r6   SUPPORTED_METHODSr   rK   r7   rP   rT   rk   rX   r   r   BaseExceptionr   ry   r:   r"   r    rC   rC   m   s    D7I!	e 	7== 	PS 	X\ 	M0@&
d=)*
 &
 ]#	

 

r"   rC   )r   r\   httpr   sslr   typesr   typingr   r   r   tornado.webrD   tornado.httpserverr	   telegramr
   telegram._utils.loggingr   telegram.ext._extbotr   r   r2   r(   r   rE   rF   r   rL   rC   r:   r"   r    <module>r      s|   (      0 0  )  . ' X)
4(4 (4VXgkk-- X.U
gkk00 U
r"   