Use config instead of forwarding all the variables

This commit is contained in:
Alex Kotov 2023-01-09 01:18:45 +04:00
parent ca1af360a4
commit 1d77e32174
No known key found for this signature in database
GPG key ID: 553C0EBBEB5D5F08

View file

@ -31,17 +31,8 @@ class Application:
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.sqlite_adapter = SqliteAdapter(self, config.db_path) self.sqlite_adapter = SqliteAdapter(self, config.db_path)
self.matrix_loop = MatrixLoop( self.matrix_loop = MatrixLoop(self)
self, self.telegram_loop = TelegramLoop(self)
config.matrix_bot_id,
config.matrix_homeserver_url,
config.matrix_owner_id,
config.matrix_password,
)
self.telegram_loop = TelegramLoop(
self,
config.telegram_bot_token,
)
async def run(self): async def run(self):
try: try:
@ -55,18 +46,16 @@ class Application:
await self.matrix_loop.finish() await self.matrix_loop.finish()
class MatrixLoop: class MatrixLoop:
def __init__(self, app, bot_id, homeserver_url, owner_id, password): def __init__(self, app):
self.app = app self.app = app
self.bot_id = bot_id self.client = matrix.AsyncClient(
self.homeserver_url = homeserver_url app.config.matrix_homeserver_url,
self.owner_id = owner_id app.config.matrix_bot_id,
self.password = password )
self.client = matrix.AsyncClient(homeserver_url, bot_id)
self.client.add_event_callback(self.on_message, matrix.RoomMessage) self.client.add_event_callback(self.on_message, matrix.RoomMessage)
async def prepare(self): async def prepare(self):
await self.client.login(self.password) await self.client.login(self.app.config.matrix_password)
async def finish(self): async def finish(self):
await self.client.close() await self.client.close()
@ -78,9 +67,9 @@ class MatrixLoop:
print(room, event, file=sys.stderr) print(room, event, file=sys.stderr)
class TelegramLoop: class TelegramLoop:
def __init__(self, app, bot_token): def __init__(self, app):
self.app = app self.app = app
self.bot = telegram.Bot(token=bot_token) self.bot = telegram.Bot(token=app.config.telegram_bot_token)
self.dispatcher = telegram.Dispatcher(bot=self.bot) self.dispatcher = telegram.Dispatcher(bot=self.bot)
self.dispatcher.register_message_handler(self.on_message) self.dispatcher.register_message_handler(self.on_message)