refactor, feat: separate main.py, add matrix_rooms_sql

This commit is contained in:
def 2023-01-10 00:57:05 +04:00
parent 8556316cd8
commit 6f904df4ff
4 changed files with 100 additions and 65 deletions

View file

@ -6,47 +6,39 @@ import sys
import aiogram as telegram
import nio as matrix
TELEGRAM_USER_MATRIX_CHATS_SQL = '''
CREATE TABLE IF NOT EXISTS telegram_user_matrix_chats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_user_id INTEGER NOT NULL,
matrix_chat_id INTEGER NOT NULL,
FOREIGN KEY(telegram_user_id) REFERENCES telegram_users(id),
FOREIGN KEY(matrix_chat_id) REFERENCES matrix_chats(id)
);
'''
from matrix import MatrixLoop
from telegram import TelegramLoop
TELEGRAM_USERS_SQL = '''
CREATE TABLE IF NOT EXISTS telegram_users
(
id INTEGER PRIMARY KEY NOT NULL (15),
first_name TEXT NOT NULL (50),
last_name TEXT (50),
username TEXT (50),
);
'''
from mirrortea.init_db import (
MATRIX_ROOMS_SQL,
TELEGRAM_USER_MATRIX_CHATS_SQL,
TELEGRAM_USERS_SQL,
)
def main():
config = Config(
db_path=os.environ['DB_PATH'],
matrix_bot_id=os.environ['MATRIX_BOT_ID'],
matrix_homeserver_url=os.environ['MATRIX_HOMESERVER_URL'],
matrix_owner_id=os.environ['MATRIX_OWNER_ID'],
matrix_password=os.environ['MATRIX_PASSWORD'],
telegram_bot_token=os.environ['TELEGRAM_BOT_TOKEN'],
db_path=os.environ["DB_PATH"],
matrix_bot_id=os.environ["MATRIX_BOT_ID"],
matrix_homeserver_url=os.environ["MATRIX_HOMESERVER_URL"],
matrix_owner_id=os.environ["MATRIX_OWNER_ID"],
matrix_password=os.environ["MATRIX_PASSWORD"],
telegram_bot_token=os.environ["TELEGRAM_BOT_TOKEN"],
)
asyncio.run(Application(config).run())
class Config:
def __init__(self, **kwargs):
self.db_path = kwargs['db_path']
self.matrix_bot_id = kwargs['matrix_bot_id']
self.matrix_homeserver_url = kwargs['matrix_homeserver_url']
self.matrix_owner_id = kwargs['matrix_owner_id']
self.matrix_password = kwargs['matrix_password']
self.telegram_bot_token = kwargs['telegram_bot_token']
self.db_path = kwargs["db_path"]
self.matrix_bot_id = kwargs["matrix_bot_id"]
self.matrix_homeserver_url = kwargs["matrix_homeserver_url"]
self.matrix_owner_id = kwargs["matrix_owner_id"]
self.matrix_password = kwargs["matrix_password"]
self.telegram_bot_token = kwargs["telegram_bot_token"]
# выглядит ужасно :(
class Application:
def __init__(self, config):
@ -66,39 +58,6 @@ class Application:
if self.matrix_loop:
await self.matrix_loop.finish()
class MatrixLoop:
def __init__(self, app):
self.app = app
self.client = matrix.AsyncClient(
app.config.matrix_homeserver_url,
app.config.matrix_bot_id,
)
self.client.add_event_callback(self.on_message, matrix.RoomMessage)
async def prepare(self):
await self.client.login(self.app.config.matrix_password)
async def finish(self):
await self.client.close()
async def run(self):
await self.client.sync_forever(timeout=30000)
async def on_message(self, room, event):
print(room, event, file=sys.stderr)
class TelegramLoop:
def __init__(self, app):
self.app = app
self.bot = telegram.Bot(token=app.config.telegram_bot_token)
self.dispatcher = telegram.Dispatcher(bot=self.bot)
self.dispatcher.register_message_handler(self.on_message)
async def run(self):
await self.dispatcher.start_polling()
async def on_message(self, msg):
print(msg, file=sys.stderr)
class SqliteAdapter:
def __init__(self, app, path):
@ -108,8 +67,15 @@ class SqliteAdapter:
self._create_tables()
def _create_tables(self):
self.conn.execute(TELEGRAM_USER_MATRIX_CHATS_SQL)
for table in [
TELEGRAM_USER_MATRIX_CHATS_SQL,
TELEGRAM_USERS_SQL,
MATRIX_ROOMS_SQL,
]:
self.conn.execute(table)
self.conn.commit()
if __name__ == '__main__':
if __name__ == "__main__":
main()

26
mirrortea/init_db.py Normal file
View file

@ -0,0 +1,26 @@
TELEGRAM_USER_MATRIX_CHATS_SQL = """
CREATE TABLE IF NOT EXISTS telegram_user_matrix_chats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_user_id INTEGER NOT NULL,
matrix_chat_id INTEGER NOT NULL,
FOREIGN KEY(telegram_user_id) REFERENCES telegram_users(id),
FOREIGN KEY(matrix_chat_id) REFERENCES matrix_chats(id)
);
"""
TELEGRAM_USERS_SQL = """
CREATE TABLE IF NOT EXISTS telegram_users
(
id INTEGER PRIMARY KEY NOT NULL (15),
first_name TEXT NOT NULL (50),
last_name TEXT (50),
username TEXT (50),
);
"""
MATRIX_ROOMS_SQL = """
CREATE TABLE IF NOT EXISTS telegram_users
(
id TEXT PRIMARY KEY NOT NULL
);
"""

31
mirrortea/matrix.py Normal file
View file

@ -0,0 +1,31 @@
class MatrixLoop:
def __init__(self, app):
self.app = app
self.client = matrix.AsyncClient(
app.config.matrix_homeserver_url,
app.config.matrix_bot_id,
)
self.client.add_event_callback(self.on_message, matrix.RoomMessage)
async def prepare(self):
await self.client.login(self.app.config.matrix_password)
async def finish(self):
await self.client.close()
async def run(self):
await self.client.sync_forever(timeout=30000)
async def on_message(self, room, event):
print(room, event, file=sys.stderr)
def upgrade_room(self, room, telegram_nickname):
event_dict = matrix.event_builders.event_builder.EventBuilder(
name=telegram_nickname
).as_dict()
client.room_send(
room_id=room,
message_type=event_dict["type"],
content=event_dict["content"],
) # предположу что оно так работает
# https://matrix-nio.readthedocs.io/en/latest/nio.html#module-nio.event_builders.state_events

12
mirrortea/telegram.py Normal file
View file

@ -0,0 +1,12 @@
class TelegramLoop:
def __init__(self, app):
self.app = app
self.bot = telegram.Bot(token=app.config.telegram_bot_token)
self.dispatcher = telegram.Dispatcher(bot=self.bot)
self.dispatcher.register_message_handler(self.on_message)
async def run(self):
await self.dispatcher.start_polling()
async def on_message(self, msg):
print(msg, file=sys.stderr)