diff --git a/mybot/__init__.py b/mybot/__init__.py index 51ff870..f697dad 100644 --- a/mybot/__init__.py +++ b/mybot/__init__.py @@ -21,6 +21,11 @@ def create_bot(config: Config, i18n: I18N, engine): register_handlers(bot) setup_middlewares(bot, i18n, engine) add_custom_filters(bot, config) + if config.use_webhook: + bot.delete_webhook() + bot.set_webhook(config.webhook.url, + drop_pending_updates=config.webhook.drop_pending_updates, + max_connections=config.webhook.max_connections) return bot diff --git a/mybot/config.py b/mybot/config.py index 24c84b7..435beb7 100644 --- a/mybot/config.py +++ b/mybot/config.py @@ -21,6 +21,19 @@ class BotConfig: os.getenv("BOT_PARSE_MODE", "html")) +@dataclass +class WebhookConfig: + url: str + max_connections: int + drop_pending_updates: bool + + @classmethod + def from_env(cls): + return cls(os.getenv("WEBHOOK_URL"), + int(os.getenv("WEBHOOK_MAX_CONNECTIONS", 40)), + bool(int(os.getenv("WEBHOOK_DROP_PENDING", True)))) + + @dataclass class I18NConfig: path: str @@ -70,7 +83,9 @@ class Config: i18n: I18NConfig states: StateStorageConfig database: DatabaseConfig + webhook: WebhookConfig + use_webhook: bool log_level: str owner_id: int @@ -81,6 +96,8 @@ class Config: i18n=I18NConfig.from_env(), states=StateStorageConfig.from_env(), database=DatabaseConfig.from_env(), + webhook=WebhookConfig.from_env(), + use_webhook=bool(int(os.getenv("USE_WEBHOOK", False))), log_level=os.getenv("LOG_LEVEL", "INFO"), owner_id=int(os.getenv("OWNER_ID", 1)), )