From 6e8238243a0a4d2f9fe44753a9e0b4538b5840b0 Mon Sep 17 00:00:00 2001 From: Ilya Bezrukov Date: Thu, 8 Aug 2024 02:55:51 +0300 Subject: [PATCH] Add ability to use self-signed certificate, refactor config.py --- mybot/__init__.py | 3 ++- mybot/config.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/mybot/__init__.py b/mybot/__init__.py index e04db7a..4e68126 100644 --- a/mybot/__init__.py +++ b/mybot/__init__.py @@ -28,7 +28,8 @@ def create_bot(config: Config, i18n: I18N, engine): bot.set_webhook(config.webhook.url, drop_pending_updates=config.webhook.drop_pending_updates, max_connections=config.webhook.max_connections, - secret_token=config.webhook.secret_token) + secret_token=config.webhook.secret_token, + certificate=config.webhook.cert_path) return bot diff --git a/mybot/config.py b/mybot/config.py index 776943b..11e7967 100644 --- a/mybot/config.py +++ b/mybot/config.py @@ -1,6 +1,7 @@ import os import secrets from dataclasses import dataclass +from typing import Optional @dataclass @@ -24,12 +25,17 @@ class BotConfig: @dataclass class WebhookConfig: - domain: str + domain: Optional[str] url_path: str max_connections: int drop_pending_updates: bool + + # secret token use_secret_token: bool - secret_token: str + secret_token: Optional[str] + + # self-signed certificate + cert_path: Optional[str] def __post_init__(self): if self.use_secret_token and not self.secret_token: @@ -37,7 +43,7 @@ class WebhookConfig: @property def url(self): - return f"https://{self.domain}/{self.url_path}" + return f"https://{self.domain}{self.url_path}" @classmethod def from_env(cls): @@ -46,7 +52,8 @@ class WebhookConfig: int(os.getenv("WEBHOOK_MAX_CONNECTIONS", 40)), bool(int(os.getenv("WEBHOOK_DROP_PENDING", True))), bool(int(os.getenv("WEBHOOK_USE_SECRET_TOKEN", True))), - os.getenv("WEBHOOK_SECRET_TOKEN")) + os.getenv("WEBHOOK_SECRET_TOKEN"), + os.getenv("WEBHOOK_CERT_PATH")) @dataclass