Files
duty-teller/main.py
Nikolay Tatarinov 1948618394 Refactor configuration and enhance Telegram initData validation
- Improved formatting and readability in config.py and other files by adding line breaks.
- Introduced INIT_DATA_MAX_AGE_SECONDS to enforce replay protection for Telegram initData.
- Updated validate_init_data function to include max_age_seconds parameter for validation.
- Enhanced API to reject old initData based on the new max_age_seconds setting.
- Added tests for auth_date expiry and validation of initData in test_telegram_auth.py.
- Updated README with details on the new INIT_DATA_MAX_AGE_SECONDS configuration.
2026-02-17 17:31:20 +03:00

50 lines
1.2 KiB
Python

"""Single entry point: build Application, run HTTP server + polling. Migrations run in Docker entrypoint."""
import asyncio
import logging
import threading
import config
from telegram.ext import ApplicationBuilder
from handlers import register_handlers
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
def _run_uvicorn(web_app, port: int) -> None:
"""Run uvicorn in a dedicated thread with its own event loop."""
import uvicorn
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
server = uvicorn.Server(
uvicorn.Config(web_app, host="0.0.0.0", port=port, log_level="info"),
)
loop.run_until_complete(server.serve())
def main() -> None:
app = ApplicationBuilder().token(config.BOT_TOKEN).build()
register_handlers(app)
from api.app import app as web_app
t = threading.Thread(
target=_run_uvicorn,
args=(web_app, config.HTTP_PORT),
daemon=True,
)
t.start()
logger.info("Bot starting (polling)... HTTP API on port %s", config.HTTP_PORT)
app.run_polling(allowed_updates=["message"])
if __name__ == "__main__":
main()