a
    /3e                     @   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yJ   dZY n0 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                   @   sb   e Zd Zdeee ee ee ee ddddZedd Z	d	d
 Z
eed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fastapiZFastAPIapp_secret_token_bot_port_host_ssl_context	_url_path_prepare_endpoint_urls)selfZbotr	   r
   r   r   r    r   L/home/pi/bot/my_env/lib/python3.9/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)Zendpointpathmethods)r   Z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rR| 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   Zprocess_new_updatesr   Z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   Zssl_certfileZ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$   r3   r   r   r   r   r      s"       0

r   )__doc__r   r   Zfastapi.responsesr   Zfastapi.requestsr   r1   r   Ztelebot.typesr   typingr   r   r   r   r   r   <module>   s   
