Enhance Telegram bot with database integration and API features
- Added SQLite database support with Alembic for migrations. - Implemented FastAPI for HTTP API to manage duties. - Updated configuration to include database URL and HTTP port. - Created entrypoint script for Docker to handle migrations and permissions. - Expanded command handlers to register users and display duties. - Developed a web application for calendar display of duties. - Included necessary Pydantic schemas and SQLAlchemy models for data handling. - Updated requirements.txt to include new dependencies for FastAPI and SQLAlchemy.
This commit is contained in:
26
main.py
26
main.py
@@ -1,5 +1,7 @@
|
||||
"""Single entry point: build Application, register handlers, run polling."""
|
||||
"""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
|
||||
@@ -13,10 +15,30 @@ logging.basicConfig(
|
||||
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)
|
||||
logger.info("Bot starting (polling)...")
|
||||
|
||||
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"])
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user