Add an application class

This commit is contained in:
Alex Kotov 2023-01-08 23:28:13 +04:00
parent 3c40b08b02
commit 2f5694cffb
No known key found for this signature in database
GPG key ID: 553C0EBBEB5D5F08

View file

@ -10,23 +10,28 @@ MATRIX_FULL_USER_ID = os.environ['MATRIX_FULL_USER_ID']
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD']
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
async def main():
def main():
asyncio.run(Application().run())
class Application:
def __init__(self):
self.matrix_loop = MatrixLoop(
MATRIX_HOMESERVER_URL,
MATRIX_FULL_USER_ID,
MATRIX_PASSWORD,
)
self.telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
async def run(self):
try:
matrix_loop = None
matrix_loop = MatrixLoop(MATRIX_HOMESERVER_URL, MATRIX_FULL_USER_ID,
MATRIX_PASSWORD)
await matrix_loop.prepare()
telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
await self.matrix_loop.prepare()
await asyncio.gather(
matrix_loop.run(),
telegram_loop.run(),
self.matrix_loop.run(),
self.telegram_loop.run(),
)
finally:
if matrix_loop:
await matrix_loop.finish()
if self.matrix_loop:
await self.matrix_loop.finish()
class MatrixLoop:
def __init__(self, homeserver_url, full_user_id, password):
@ -59,4 +64,4 @@ class TelegramLoop:
print(msg, file=sys.stderr)
if __name__ == '__main__':
asyncio.run(main())
main()