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():
try:
matrix_loop = None
def main():
asyncio.run(Application().run())
matrix_loop = MatrixLoop(MATRIX_HOMESERVER_URL, MATRIX_FULL_USER_ID,
MATRIX_PASSWORD)
await matrix_loop.prepare()
telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
await asyncio.gather(
matrix_loop.run(),
telegram_loop.run(),
class Application:
def __init__(self):
self.matrix_loop = MatrixLoop(
MATRIX_HOMESERVER_URL,
MATRIX_FULL_USER_ID,
MATRIX_PASSWORD,
)
finally:
if matrix_loop:
await matrix_loop.finish()
self.telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
async def run(self):
try:
await self.matrix_loop.prepare()
await asyncio.gather(
self.matrix_loop.run(),
self.telegram_loop.run(),
)
finally:
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()