Add webhook config

This commit is contained in:
Ilya Bezrukov 2024-07-30 02:59:58 +03:00
parent f8c09d963e
commit e230d6f3dc
2 changed files with 22 additions and 0 deletions

View File

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

View File

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