service
This commit is contained in:
37
bot/main.py
37
bot/main.py
@@ -5,6 +5,7 @@ import sys
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.enums import ParseMode
|
||||
from aiohttp import web
|
||||
|
||||
from config import settings
|
||||
from handlers import start, marathons, link
|
||||
@@ -23,14 +24,41 @@ logger = logging.getLogger(__name__)
|
||||
# Set aiogram logging level
|
||||
logging.getLogger("aiogram").setLevel(logging.INFO)
|
||||
|
||||
# Health check state
|
||||
bot_running = False
|
||||
|
||||
|
||||
async def health_handler(request):
|
||||
"""Health check endpoint"""
|
||||
if bot_running:
|
||||
return web.json_response({"status": "ok", "service": "telegram-bot"})
|
||||
return web.json_response({"status": "starting"}, status=503)
|
||||
|
||||
|
||||
async def start_health_server():
|
||||
"""Start health check HTTP server"""
|
||||
app = web.Application()
|
||||
app.router.add_get("/health", health_handler)
|
||||
runner = web.AppRunner(app)
|
||||
await runner.setup()
|
||||
site = web.TCPSite(runner, "0.0.0.0", 8080)
|
||||
await site.start()
|
||||
logger.info("Health check server started on port 8080")
|
||||
return runner
|
||||
|
||||
|
||||
async def main():
|
||||
global bot_running
|
||||
|
||||
logger.info("="*50)
|
||||
logger.info("Starting Game Marathon Bot...")
|
||||
logger.info(f"API_URL: {settings.API_URL}")
|
||||
logger.info(f"BOT_TOKEN: {settings.TELEGRAM_BOT_TOKEN[:20]}...")
|
||||
logger.info("="*50)
|
||||
|
||||
# Start health check server
|
||||
health_runner = await start_health_server()
|
||||
|
||||
bot = Bot(
|
||||
token=settings.TELEGRAM_BOT_TOKEN,
|
||||
default=DefaultBotProperties(parse_mode=ParseMode.HTML)
|
||||
@@ -54,11 +82,18 @@ async def main():
|
||||
dp.include_router(marathons.router)
|
||||
logger.info("Routers registered: start, link, marathons")
|
||||
|
||||
# Mark bot as running
|
||||
bot_running = True
|
||||
|
||||
# Start polling
|
||||
logger.info("Deleting webhook and starting polling...")
|
||||
await bot.delete_webhook(drop_pending_updates=True)
|
||||
logger.info("Polling started! Waiting for messages...")
|
||||
await dp.start_polling(bot)
|
||||
try:
|
||||
await dp.start_polling(bot)
|
||||
finally:
|
||||
bot_running = False
|
||||
await health_runner.cleanup()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user