Fix Telethon event loop lifecycle
This commit is contained in:
+17
-6
@@ -6,7 +6,7 @@ import re
|
||||
import settings
|
||||
from telethon import TelegramClient, events
|
||||
|
||||
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)
|
||||
web = None
|
||||
api = lambda route: settings.DYNMAP_ADDRESS + route
|
||||
md5 = lambda name: int(hashlib.md5(name.encode()).hexdigest(), 16)
|
||||
@@ -58,9 +58,7 @@ async def new_message(event):
|
||||
logging.exception('Problem sending message to dynmap:')
|
||||
|
||||
|
||||
async def main():
|
||||
global web
|
||||
web = aiohttp.ClientSession()
|
||||
async def bridge_loop():
|
||||
last_time = 0
|
||||
last_msg = ''
|
||||
logging.info('Bridge loaded')
|
||||
@@ -93,5 +91,18 @@ async def main():
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
with bot:
|
||||
bot.loop.run_until_complete(main())
|
||||
|
||||
async def main():
|
||||
global web
|
||||
await bot.start(bot_token=settings.API_TOKEN)
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
web = session
|
||||
await bridge_loop()
|
||||
finally:
|
||||
web = None
|
||||
await bot.disconnect()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user