2022-07-08 19:13:07 +03:00
|
|
|
"""
|
2022-07-24 21:14:09 +03:00
|
|
|
This file is used by TeleBot.run_webhooks() function.
|
2022-07-08 19:13:07 +03:00
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
Fastapi is required to run this script.
|
2022-07-08 19:13:07 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
# modules required for running this script
|
|
|
|
fastapi_installed = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
import fastapi
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from fastapi.requests import Request
|
|
|
|
import uvicorn
|
|
|
|
except ImportError:
|
|
|
|
fastapi_installed = False
|
|
|
|
|
|
|
|
|
|
|
|
from telebot.types import Update
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SyncWebhookListener:
|
|
|
|
def __init__(self, bot,
|
|
|
|
secret_token: str, host: Optional[str]="127.0.0.1",
|
|
|
|
port: Optional[int]=443,
|
|
|
|
ssl_context: Optional[tuple]=None,
|
|
|
|
url_path: Optional[str]=None,
|
|
|
|
debug: Optional[bool]=False
|
|
|
|
) -> None:
|
|
|
|
"""
|
2022-07-24 21:14:09 +03:00
|
|
|
Aynchronous implementation of webhook listener
|
|
|
|
for asynchronous version of telebot.
|
|
|
|
Not supposed to be used manually by user.
|
|
|
|
Use AsyncTeleBot.run_webhooks() instead.
|
|
|
|
|
|
|
|
:param bot: AsyncTeleBot instance.
|
|
|
|
:type bot: telebot.async_telebot.AsyncTeleBot
|
2022-07-08 19:13:07 +03:00
|
|
|
|
|
|
|
:param secret_token: Telegram secret token
|
2022-07-24 21:14:09 +03:00
|
|
|
:type secret_token: str
|
|
|
|
|
2022-07-08 19:13:07 +03:00
|
|
|
:param host: Webhook host
|
2022-07-24 21:14:09 +03:00
|
|
|
:type host: str
|
|
|
|
|
2022-07-08 19:13:07 +03:00
|
|
|
:param port: Webhook port
|
2022-07-24 21:14:09 +03:00
|
|
|
:type port: int
|
|
|
|
|
2022-07-08 19:13:07 +03:00
|
|
|
:param ssl_context: SSL context
|
2022-07-24 21:14:09 +03:00
|
|
|
:type ssl_context: tuple
|
|
|
|
|
2022-07-08 19:13:07 +03:00
|
|
|
:param url_path: Webhook url path
|
2022-07-24 21:14:09 +03:00
|
|
|
:type url_path: str
|
|
|
|
|
2022-07-08 19:13:07 +03:00
|
|
|
:param debug: Debug mode
|
2022-07-24 21:14:09 +03:00
|
|
|
:type debug: bool
|
|
|
|
|
|
|
|
:raises ImportError: If FastAPI or uvicorn is not installed.
|
|
|
|
:raises ImportError: If Starlette version is too old.
|
|
|
|
|
|
|
|
:return: None
|
2022-07-08 19:13:07 +03:00
|
|
|
"""
|
|
|
|
self._check_dependencies()
|
|
|
|
|
|
|
|
self.app = fastapi.FastAPI()
|
|
|
|
self._secret_token = secret_token
|
|
|
|
self._bot = bot
|
|
|
|
self._port = port
|
|
|
|
self._host = host
|
|
|
|
self._ssl_context = ssl_context
|
|
|
|
self._url_path = url_path
|
|
|
|
self._debug = debug
|
|
|
|
self._prepare_endpoint_urls()
|
|
|
|
|
|
|
|
|
|
|
|
def _check_dependencies(self):
|
|
|
|
if not fastapi_installed:
|
|
|
|
raise ImportError('Fastapi or uvicorn is not installed. Please install it via pip.')
|
|
|
|
|
|
|
|
import starlette
|
|
|
|
if starlette.__version__ < '0.20.2':
|
|
|
|
raise ImportError('Starlette version is too old. Please upgrade it: `pip3 install starlette -U`')
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def _prepare_endpoint_urls(self):
|
|
|
|
self.app.add_api_route(endpoint=self.process_update,path= self._url_path, methods=["POST"])
|
|
|
|
|
|
|
|
|
|
|
|
def process_update(self, request: Request, update: dict):
|
|
|
|
"""
|
|
|
|
Processes updates.
|
2022-07-24 21:14:09 +03:00
|
|
|
|
|
|
|
:meta private:
|
2022-07-08 19:13:07 +03:00
|
|
|
"""
|
|
|
|
# header containsX-Telegram-Bot-Api-Secret-Token
|
|
|
|
if request.headers.get('X-Telegram-Bot-Api-Secret-Token') != self._secret_token:
|
|
|
|
# secret token didn't match
|
|
|
|
return JSONResponse(status_code=403, content={"error": "Forbidden"})
|
|
|
|
if request.headers.get('content-type') == 'application/json':
|
|
|
|
self._bot.process_new_updates([Update.de_json(update)])
|
|
|
|
return JSONResponse('', status_code=200)
|
|
|
|
|
|
|
|
return JSONResponse(status_code=403, content={"error": "Forbidden"})
|
|
|
|
|
|
|
|
|
|
|
|
def run_app(self):
|
|
|
|
"""
|
2022-07-24 21:14:09 +03:00
|
|
|
Run app with the given parameters to init.
|
|
|
|
Not supposed to be used manually by user.
|
|
|
|
|
|
|
|
:return: None
|
2022-07-08 19:13:07 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
uvicorn.run(app=self.app,
|
|
|
|
host=self._host,
|
|
|
|
port=self._port,
|
|
|
|
debug=self._debug,
|
|
|
|
ssl_certfile=self._ssl_context[0],
|
|
|
|
ssl_keyfile=self._ssl_context[1]
|
|
|
|
)
|