
    i7                     t   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 d dl	m
Z
 d dlmZmZmZmZ d dlZd dlmZ 	 d d	lmZ d
Zn# e$ r dZY nw xY w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j"        j#                  Z$ G d dej"        j%                  Z&dS )    N)
HTTPStatus)Path)socket)
SSLContext)TracebackType)TYPE_CHECKINGOptionalTypeUnion)
HTTPServer)bind_unix_socketTF)Update)
get_logger)ExtBot)BotUpdater)
class_namec                       e Zd ZdZdZ	 ddededddee         d	ee	ee
ef                  f
d
Zddeej                 ddfdZddZdS )WebhookServerz6Thin wrapper around ``tornado.httpserver.HTTPServer``.)_http_server_server_lock_shutdown_lock
is_runninglistenportunixNr   r   webhook_appWebhookAppClassssl_ctxr   c                 ~   |rt           st          d          t          ||          | _        || _        || _        d| _        d | _        |rt          |t                    r|| _        n#|r!t          t          |                    | _        t          j                    | _        t          j                    | _        d S )Nz.This OS does not support binding unix sockets.)ssl_optionsF)UNIX_AVAILABLERuntimeErrorr   r   r   r   r   r   
isinstancer   r   strasyncioLockr   r   )selfr   r   r   r   r   s         h/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/telegram/ext/_utils/webhookhandler.py__init__zWebhookServer.__init__A   s      	Q 	QOPPP&{HHH		 	4JtV,, 	4DII 	4(T33DI#LNN%lnn    readyreturnc                 v  K   | j         4 d {V  | j        r | j                            | j                   n&| j                            | j        | j                   d| _        ||                                 t          	                    d           d d d           d {V  d S # 1 d {V swxY w Y   d S )N)addressTzWebhook Server started.)
r   r   r   
add_socketr   r   r   set_LOGGERdebug)r(   r,   s     r)   serve_foreverzWebhookServer.serve_foreverW   sA     $ 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5y I!,,TY7777!((DK(HHH"DO 		MM3444
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5 
	5s   BB((
B25B2c                   K   | j         4 d {V  | j        s.t                              d           	 d d d           d {V  d S d| _        | j                                         | j                                         d {V  t                              d           d d d           d {V  d S # 1 d {V swxY w Y   d S )Nz.Webhook Server is already shut down. ReturningFzWebhook Server stopped)r   r   r2   r3   r   stopclose_all_connectionsr(   s    r)   shutdownzWebhookServer.shutdownd   s     & 	4 	4 	4 	4 	4 	4 	4 	4? NOOO	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 $DO""$$$#99;;;;;;;;;MM2333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   #B2AB22
B<?B<Nr-   N)__name__
__module____qualname____doc__	__slots__r%   intr	   r   r   r   r   r*   r&   Eventr4   r9    r+   r)   r   r   4   s        @@I  48- -- - '	-
 *%- uS$./0- - - -,5 5'-)@ 5D 5 5 5 54 4 4 4 4 4r+   r   c            
       h    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dS )r   z!Application used in the WebserverNwebhook_pathbotr   update_queuesecret_tokenc                     |||d| _         | dt          | j         fg}t          j        j                            | |           d S )N)rF   rG   rH   z/?)shared_objectsTelegramHandlertornadowebApplicationr*   )r(   rE   rF   rG   rH   handlerss         r)   r*   zWebhookAppClass.__init__r   sZ     ((
 

 &)))?D<OPQ((x88888r+   handlerr-   c                     dS )zIOverrides the default implementation since we have our own logging setup.NrC   )r(   rP   s     r)   log_requestzWebhookAppClass.log_request   s      r+   r:   )r<   r=   r>   r?   r%   r&   Queuer	   r*   rL   rM   RequestHandlerrR   rC   r+   r)   r   r   o   s        ++ '+9 99 9 m	9
 sm9 9 9 9X7;#= X$ X X X X 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d	S )rK   z:BaseHandler that processes incoming requests from Telegram)rF   rH   rG   )POSTrF   r   rG   rH   r-   Nc                 l    || _         || _        || _        |rt                              d           dS dS )zFInitialize for each request - that's the interface provided by tornadozLThe webhook server has a secret token, expecting it in incoming requests nowN)rF   rG   rH   r2   r3   )r(   rF   rG   rH   s       r)   
initializezTelegramHandler.initialize   sP     (( 	MM^    	 	r+   c                 2    |                      dd           dS )zSets default headersContent-Typez!application/json; charset="utf-8"N)
set_headerr8   s    r)   set_default_headersz#TelegramHandler.set_default_headers   s    (KLLLLLr+   c                   K   t                               d           |                                  | j        j                                        }t          j        |          }|                     t          j
                   t                               d|           	 t          j        || j                  }nZ# t          $ rM}t                               d|           t           j                            t          j        d          |d}~ww xY w|rvt                               d|j                   t+          | j        t,                    r| j                            |           | j                            |           d{V  dS dS )	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_infozUpdate could not be processedreasonNz%Received Update with ID %d on Webhook)r2   r3   _validate_postrequestbodydecodejsonloads
set_statusr   OKr   de_jsonrF   	ExceptioncriticalrL   rM   	HTTPErrorBAD_REQUEST	update_idr$   r   insert_callback_datarG   put)r(   json_stringdataupdateexcs        r)   postzTelegramHandler.post   s     )***l'..00z+&&
&&&1;???
	^D$(33FF 	 	 	5    
 +''&/N (  	  	0MM7    $(F++ 6--f555#''///////////	0 	0s   B9 9
DADDc                 $   | j         j                            dd          }|dk    r)t          j                            t          j                  | j        | j         j                            d          }|sEt          
                    d           t          j                            t          j        d          || j        k    rHt          
                    d|           t          j                            t          j        d          dS dS )	z+Only accept requests with content type JSONrZ   Nzapplication/jsonzX-Telegram-Bot-Api-Secret-Tokenz(Request did not include the secret tokenr`   z&Request had the wrong secret token: %sz"Request had the wrong secret token)rc   headersgetrL   rM   rm   r   	FORBIDDENrH   r2   r3   )r(   	ct_headertokens      r)   rb   zTelegramHandler._validate_post   s   L(,,^TBB	***+''
(<===(L(,,-NOOE HIIIk++(1[ ,    )))FNNNk++(1U ,    )( *)r+   typvaluetbc                 l    t                               d| j        j        d|r	|r|r|||fn|           dS )z@Override the default logging and instead use our custom logging.z%s - %szException in TelegramHandlerr^   N)r2   r3   rc   	remote_ip)r(   r}   r~   r   s       r)   log_exceptionzTelegramHandler.log_exception   sU     	L"*),HH2Hc5"%%5	 	 	
 	
 	
 	
 	
r+   r;   )r<   r=   r>   r?   r@   SUPPORTED_METHODSr&   rS   r%   rX   r\   rv   rb   r	   r
   BaseExceptionr   r   rC   r+   r)   rK   rK      s        DD7I!	e 	7= 	PS 	X\ 	 	 	 	M M M M!0 !0 !0 !0F   &
d=)*
 &
 ]#	

 

 
 
 
 
 
r+   rK   )'r&   rf   httpr   pathlibr   r   sslr   typesr   typingr   r	   r
   r   tornado.webrL   tornado.httpserverr   tornado.netutilr   r"   ImportErrortelegramr   telegram._utils.loggingr   telegram.ext._extbotr   r   r<   r2   r   rM   rN   r   rT   rK   rC   r+   r)   <module>r      s  (                                 7 7 7 7 7 7 7 7 7 7 7 7     ) ) ) ) ) )000000NN   NNN       . . . . . . ' ' ' ' ' '  *X)
4
4
484 84 84 84 84 84 84 84vX X X X Xgk- X X X.X
 X
 X
 X
 X
gk0 X
 X
 X
 X
 X
s   A AA