Fix Telethon event loop lifecycle
This commit is contained in:
@@ -5,7 +5,6 @@ logging.basicConfig(
|
||||
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
|
||||
|
||||
import settings
|
||||
import asyncio
|
||||
import json
|
||||
import requests
|
||||
from datetime import datetime
|
||||
@@ -14,7 +13,7 @@ from telethon import TelegramClient, events
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
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)
|
||||
TANNER = 79316791
|
||||
|
||||
messages = {}
|
||||
@@ -126,8 +125,18 @@ async def submit(request):
|
||||
return web.Response(text='Thanks! Your message is pending approval.')
|
||||
|
||||
|
||||
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.router.add_post('/', submit)
|
||||
app.cleanup_ctx.append(telegram_lifecycle)
|
||||
|
||||
if __name__ == '__main__':
|
||||
bot.start()
|
||||
app = web.Application()
|
||||
app.router.add_post('/', submit)
|
||||
web.run_app(app, port=8123)
|
||||
|
||||
Reference in New Issue
Block a user