
    i2                     h   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 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)
HTTPServer)bind_unix_socketTF)Update)
get_logger)ExtBot)BotUpdater)
class_namec                   x    e Zd ZdZdZ	 ddededddedz  d	eez  e	z  dz  f
d
Z
ddej        dz  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         b/Users/shanyulin/my-agent/.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   r/   r0   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#   Eventr1   r6    r(   r&   r   r   4   s        @@I  ,0- -- - '	-
 d"- Dj6!D(- - - -,5 5)= 5 5 5 5 54 4 4 4 4 4r(   r   c            
       b    e Zd ZdZ	 ddedddej        dedz  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)rC   rD   rE   z/?)shared_objectsTelegramHandlertornadowebApplicationr'   )r%   rB   rC   rD   rE   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.Nr@   )r%   rM   s     r&   log_requestzWebhookAppClass.log_request   s      r(   r7   )r9   r:   r;   r<   r"   r#   Queuer'   rI   rJ   RequestHandlerrO   r@   r(   r&   r   r   o   s        ++ $(9 99 9 m	9
 Dj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         d	z  ded	z  ded	z  dd	fdZd	S )rH   z:BaseHandler that processes incoming requests from Telegram)rC   rE   rD   )POSTrC   r   rD   rE   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)rC   rD   rE   r/   r0   )r%   rC   rD   rE   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_headerr5   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                  }n[# t          $ rN}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: %szySomething went wrong processing the data received from Telegram. Received data was *not* processed! Received data was: %rexc_infozUpdate could not be processedreasonNz%Received Update with ID %d on Webhook)r/   r0   _validate_postrequestbodydecodejsonloads
set_statusr   OKr   de_jsonrC   	ExceptioncriticalrI   rJ   	HTTPErrorBAD_REQUEST	update_idr!   r   insert_callback_datarD   put)r%   json_stringdataupdateexcs        r&   postzTelegramHandler.post   s     )***l'..00z+&&
&&&1;???	^D$(33FF 		 		 		K	     +''&/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 JSONrW   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)r`   headersgetrI   rJ   rj   r   	FORBIDDENrE   r/   r0   )r%   	ct_headertokens      r&   r_   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)r/   r0   r`   	remote_ip)r%   rz   r{   r|   s       r&   log_exceptionzTelegramHandler.log_exception   sU     	L"*),HH2Hc5"%%5	 	 	
 	
 	
 	
 	
r(   r8   )r9   r:   r;   r<   r=   SUPPORTED_METHODSr#   rP   r"   rU   rY   rs   r_   typeBaseExceptionr   r   r@   r(   r&   rH   rH      s        DD7I!	e 	7= 	PS 	X\ 	 	 	 	M M M M"0 "0 "0 "0H   &
- 4'
 t#
 D 	

 

 
 
 
 
 
r(   rH   )$r#   rc   httpr   pathlibr   r   sslr   typesr   typingr   tornado.webrI   tornado.httpserverr	   tornado.netutilr
   r   ImportErrortelegramr   telegram._utils.loggingr   telegram.ext._extbotr   r   r9   r/   r   rJ   rK   r   rQ   rH   r@   r(   r&   <module>r      s  (                                                 ) ) ) ) ) )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.Y
 Y
 Y
 Y
 Y
gk0 Y
 Y
 Y
 Y
 Y
s   A A
A