Fix Telethon event loop lifecycle
This commit is contained in:
+11
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user