Add an application class
This commit is contained in:
parent
3c40b08b02
commit
2f5694cffb
|
@ -10,23 +10,28 @@ MATRIX_FULL_USER_ID = os.environ['MATRIX_FULL_USER_ID']
|
||||||
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD']
|
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD']
|
||||||
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
|
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
|
||||||
|
|
||||||
async def main():
|
def main():
|
||||||
try:
|
asyncio.run(Application().run())
|
||||||
matrix_loop = None
|
|
||||||
|
|
||||||
matrix_loop = MatrixLoop(MATRIX_HOMESERVER_URL, MATRIX_FULL_USER_ID,
|
class Application:
|
||||||
MATRIX_PASSWORD)
|
def __init__(self):
|
||||||
await matrix_loop.prepare()
|
self.matrix_loop = MatrixLoop(
|
||||||
|
MATRIX_HOMESERVER_URL,
|
||||||
telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
|
MATRIX_FULL_USER_ID,
|
||||||
|
MATRIX_PASSWORD,
|
||||||
await asyncio.gather(
|
|
||||||
matrix_loop.run(),
|
|
||||||
telegram_loop.run(),
|
|
||||||
)
|
)
|
||||||
finally:
|
self.telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
|
||||||
if matrix_loop:
|
|
||||||
await matrix_loop.finish()
|
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:
|
class MatrixLoop:
|
||||||
def __init__(self, homeserver_url, full_user_id, password):
|
def __init__(self, homeserver_url, full_user_id, password):
|
||||||
|
@ -59,4 +64,4 @@ class TelegramLoop:
|
||||||
print(msg, file=sys.stderr)
|
print(msg, file=sys.stderr)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
|
Reference in a new issue