o
    @Mh                     @   sv   d Z dZzddlZddlmZ ddlmZ ddlZW n ey%   dZY nw ddl	m
Z
 ddlmZ G d	d
 d
ZdS )z_
This file is used by TeleBot.run_webhooks() function.
Fastapi is required to run this script.
T    N)JSONResponse)RequestF)Update)Optionalc                   @   sv   e Zd Z				ddedee dee dee dee d	dfd
dZedd Z	dd Z
dedefddZdd ZdS )SyncWebhookListener	127.0.0.1  Nsecret_tokenhostportssl_contexturl_pathreturnc                 C   sB   |    t | _|| _|| _|| _|| _|| _|| _	| 
  dS )a
  
        Synchronous implementation of webhook listener
        for synchronous version of telebot.
        Not supposed to be used manually by user.
        Use TeleBot.run_webhooks() instead.

        :param bot: TeleBot instance.
        :type bot: telebot.TeleBot

        :param secret_token: Telegram secret token
        :type secret_token: str

        :param host: Webhook host
        :type host: str

        :param port: Webhook port
        :type port: int

        :param ssl_context: SSL context
        :type ssl_context: tuple

        :param url_path: Webhook url path
        :type url_path: str

        :raises ImportError: If FastAPI or uvicorn is not installed.
        :raises ImportError: If Starlette version is too old.

        :return: None
        N)_check_dependenciesfastapiFastAPIapp_secret_token_bot_port_host_ssl_context	_url_path_prepare_endpoint_urls)selfbotr	   r
   r   r   r    r   L/var/www/html/venv/lib/python3.10/site-packages/telebot/ext/sync/webhooks.py__init__   s   $
zSyncWebhookListener.__init__c                  C   s*   t stddd l} | jdk rtdd S )Nz?Fastapi or uvicorn is not installed. Please install it via pip.r   z0.20.2zLStarlette version is too old. Please upgrade it: `pip3 install starlette -U`)fastapi_installedImportError	starlette__version__)r!   r   r   r   r   G   s   
z'SyncWebhookListener._check_dependenciesc                 C   s   | j j| j| jdgd d S )NPOST)endpointpathmethods)r   add_api_routeprocess_updater   r   r   r   r   r   R   s   z*SyncWebhookListener._prepare_endpoint_urlsrequestupdatec                 C   sb   |j d| jkrtdddidS |j ddkr)| jt|g tdd	d
S tdddidS )z<
        Processes updates.

        :meta private:
        zX-Telegram-Bot-Api-Secret-Tokeni  error	Forbidden)status_codecontentzcontent-typezapplication/json    )r.   )headersgetr   r   r   process_new_updatesr   de_json)r   r*   r+   r   r   r   r(   V   s   z"SyncWebhookListener.process_updatec                 C   s*   t j| j| j| j| jd | jd d dS )z
        Run app with the given parameters to init.
        Not supposed to be used manually by user.

        :return: None
        r      )r   r
   r   ssl_certfilessl_keyfileN)uvicornrunr   r   r   r   r)   r   r   r   run_appg   s   
zSyncWebhookListener.run_app)r   r   NN)__name__
__module____qualname__strr   inttupler   staticmethodr   r   r   dictr(   r;   r   r   r   r   r      s.    
0

r   )__doc__r   r   fastapi.responsesr   fastapi.requestsr   r9   r    telebot.typesr   typingr   r   r   r   r   r   <module>   s    