Fix Telethon event loop lifecycle

This commit is contained in:
Tanner
2026-11-22 17:09:42 +00:00
parent 9bd30bbeec
commit a52c1e57c5
2 changed files with 39 additions and 6 deletions
+11 -1
View File
@@ -8,7 +8,7 @@ from telethon import TelegramClient, events
TANNER = 79316791
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH)
routes = web.RouteTableDef()
logging.basicConfig(level=logging.INFO)
@@ -51,6 +51,16 @@ async def index(request):
else:
return web.Response(text=data)
async def telegram_lifecycle(app):
# Connect Telethon on aiohttp's event loop and disconnect before it closes.
await bot.start(bot_token=settings.API_TOKEN)
try:
yield
finally:
await bot.disconnect()
app = web.Application()
app.add_routes(routes)
app.cleanup_ctx.append(telegram_lifecycle)
web.run_app(app)