Add ability to use self-signed certificate, refactor config.py

This commit is contained in:
Ilya Bezrukov 2024-08-08 02:55:51 +03:00
parent 3b12868201
commit 6e8238243a
2 changed files with 13 additions and 5 deletions

View File

@ -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

View File

@ -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