Compare commits

...

2 Commits

Author SHA1 Message Date
Alex Kotov dfa4c10138
Add config 2023-01-08 23:31:42 +04:00
Alex Kotov 2f5694cffb
Add an application class 2023-01-08 23:31:42 +04:00
1 changed files with 34 additions and 19 deletions

View File

@ -5,28 +5,43 @@ import sys
import aiogram as telegram import aiogram as telegram
import nio as matrix import nio as matrix
MATRIX_HOMESERVER_URL = os.environ['MATRIX_HOMESERVER_URL'] def main():
MATRIX_FULL_USER_ID = os.environ['MATRIX_FULL_USER_ID'] config = Config(
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD'] matrix_homeserver_url=os.environ['MATRIX_HOMESERVER_URL'],
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN'] 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(): asyncio.run(Application(config).run())
try:
matrix_loop = None
matrix_loop = MatrixLoop(MATRIX_HOMESERVER_URL, MATRIX_FULL_USER_ID, class Config:
MATRIX_PASSWORD) def __init__(self, **kwargs):
await matrix_loop.prepare() self.matrix_homeserver_url = kwargs['matrix_homeserver_url']
self.matrix_full_user_id = kwargs['matrix_full_user_id']
self.matrix_password = kwargs['matrix_password']
self.telegram_bot_token = kwargs['telegram_bot_token']
telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN) class Application:
def __init__(self, config):
await asyncio.gather( self.config = config
matrix_loop.run(), self.matrix_loop = MatrixLoop(
telegram_loop.run(), config.matrix_homeserver_url,
config.matrix_full_user_id,
config.matrix_password,
) )
finally: self.telegram_loop = TelegramLoop(config.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 +74,4 @@ class TelegramLoop:
print(msg, file=sys.stderr) print(msg, file=sys.stderr)
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run(main()) main()