Improve env vars

This commit is contained in:
Alex Kotov 2023-01-09 01:08:52 +04:00
parent 9c1a12fb89
commit ca1af360a4
No known key found for this signature in database
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 16 additions and 8 deletions

View file

@ -1,5 +1,6 @@
DB_PATH=data/database.sqlite3 DB_PATH=data/database.sqlite3
MATRIX_BOT_ID=@fckidiots:matrix.org
MATRIX_HOMESERVER_URL=https://matrix.org MATRIX_HOMESERVER_URL=https://matrix.org
MATRIX_FULL_USER_ID=@fckidiots:matrix.org MATRIX_OWNER_ID=@kotovalexarian:matrix.org
MATRIX_PASSWORD=... MATRIX_PASSWORD=...
TELEGRAM_BOT_TOKEN=5890667880:... TELEGRAM_BOT_TOKEN=5890667880:...

View file

@ -9,8 +9,9 @@ import nio as matrix
def main(): def main():
config = Config( config = Config(
db_path=os.environ['DB_PATH'], db_path=os.environ['DB_PATH'],
matrix_bot_id=os.environ['MATRIX_BOT_ID'],
matrix_homeserver_url=os.environ['MATRIX_HOMESERVER_URL'], matrix_homeserver_url=os.environ['MATRIX_HOMESERVER_URL'],
matrix_full_user_id=os.environ['MATRIX_FULL_USER_ID'], matrix_owner_id=os.environ['MATRIX_OWNER_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'],
) )
@ -20,8 +21,9 @@ def main():
class Config: class Config:
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.db_path = kwargs['db_path'] self.db_path = kwargs['db_path']
self.matrix_bot_id = kwargs['matrix_bot_id']
self.matrix_homeserver_url = kwargs['matrix_homeserver_url'] self.matrix_homeserver_url = kwargs['matrix_homeserver_url']
self.matrix_full_user_id = kwargs['matrix_full_user_id'] self.matrix_owner_id = kwargs['matrix_owner_id']
self.matrix_password = kwargs['matrix_password'] self.matrix_password = kwargs['matrix_password']
self.telegram_bot_token = kwargs['telegram_bot_token'] self.telegram_bot_token = kwargs['telegram_bot_token']
@ -31,8 +33,9 @@ class Application:
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,
config.matrix_bot_id,
config.matrix_homeserver_url, config.matrix_homeserver_url,
config.matrix_full_user_id, config.matrix_owner_id,
config.matrix_password, config.matrix_password,
) )
self.telegram_loop = TelegramLoop( self.telegram_loop = TelegramLoop(
@ -52,10 +55,14 @@ class Application:
await self.matrix_loop.finish() await self.matrix_loop.finish()
class MatrixLoop: class MatrixLoop:
def __init__(self, app, homeserver_url, full_user_id, password): def __init__(self, app, bot_id, homeserver_url, owner_id, password):
self.app = app self.app = app
self.bot_id = bot_id
self.homeserver_url = homeserver_url
self.owner_id = owner_id
self.password = password self.password = password
self.client = matrix.AsyncClient(homeserver_url, full_user_id)
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):