
    'i2                     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 d dlZd dlmZ 	 d d	lmZ d
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j>                  j@                        Z! G d dej>                  jD                        Z#y# e$ r dZY vw xY w)    N)
HTTPStatus)Path)socket)
SSLContext)TracebackType)TYPE_CHECKING)
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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y)WebhookServerz6Thin wrapper around ``tornado.httpserver.HTTPServer``.)_http_server_server_lock_shutdown_lock
is_runninglistenportunixNr   r   webhook_appWebhookAppClassssl_ctxr   c                 T   |rt         st        d      t        ||      | _        || _        || _        d| _        d | _        |rt        |t              r|| _        n|rt        t        |            | _        t        j                         | _        t        j                         | _        y )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         l/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/telegram/ext/_utils/webhookhandler.py__init__zWebhookServer.__init__A   s     OPP&{H		JtV,DI(T3DI#LLN%lln    readyreturnc                   K   | j                   4 d {    | j                  r&| j                  j                  | j                         n1| j                  j	                  | j
                  | j                         d| _        ||j                          t        j                  d       d d d       d {    y 7 7 # 1 d {  7  sw Y   y xY ww)N)addressTzWebhook Server started.)
r   r   r   
add_socketr   r   r   set_LOGGERdebug)r%   r)   s     r&   serve_foreverzWebhookServer.serve_foreverW   s     $$ 
	5 
	5yy!!,,TYY7!!((DKK(H"DO 		MM34
	5 
	5 
	5 
	5 
	5 
	5 
	5sE   CB:CBB>)C4B<5C<C>CCCC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/   r0   r   stopclose_all_connectionsr%   s    r&   shutdownzWebhookServer.shutdownd   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__r"   intr   r   r   r'   r#   Eventr1   r6    r(   r&   r   r   4   sz    @I  ,0-- - '	-
 d"- Dj6!D(-,5)= 5 54r(   r   c            
       ~    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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)rC   rD   rE   z/?)shared_objectsTelegramHandlertornadowebApplicationr'   )r%   rB   rC   rD   rE   handlerss         r&   r'   zWebhookAppClass.__init__r   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%   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 mm	9
 Dj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   d	z  ded	z  ded	z  dd	fdZy	)rH   z:BaseHandler that processes incoming requests from Telegram)rC   rE   rD   )POSTrC   r   rD   rE   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)rC   rD   rE   r/   r0   )r%   rC   rD   rE   s       r&   
initializezTelegramHandler.initialize   s2     ((MM^ r(   c                 (    | j                  dd       y)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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/                  |       | j0                  j3                  |       d{    yy# t        $ rM}t         j                  d||       t         j"                  j%                  t        j&                  d      |d}~ww xY w7 _w)	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>    )*ll''..0zz+&
&1;?	^^D$((3F MM7   $((F+--f5##''///   		K	   ++''&&/N ( 		, 0s8   BF D3 7A5F,F-F3	F	<AFF		F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 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    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 TelegramHandlerr[   N)r/   r0   r`   	remote_ip)r%   rz   r{   r|   s       r&   log_exceptionzTelegramHandler.log_exception   sI     	LL""*),2c5"%	 	 	
 DI	 	 	
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7I!	e 	7== 	PS 	X\ 	M"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N  . ' X)
484 84vXgkk-- X.Y
gkk00 Y
A  Ns   B- -B76B7