From 6f904df4ffbc7bbd90cb715b47dff087a9af11bf Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 00:57:05 +0400 Subject: [PATCH 1/9] refactor, feat: separate main.py, add matrix_rooms_sql --- mirrortea/__main__.py | 96 ++++++++++++++----------------------------- mirrortea/init_db.py | 26 ++++++++++++ mirrortea/matrix.py | 31 ++++++++++++++ mirrortea/telegram.py | 12 ++++++ 4 files changed, 100 insertions(+), 65 deletions(-) create mode 100644 mirrortea/init_db.py create mode 100644 mirrortea/matrix.py create mode 100644 mirrortea/telegram.py diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index 5d076cd..2db6e70 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -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() diff --git a/mirrortea/init_db.py b/mirrortea/init_db.py new file mode 100644 index 0000000..0d6e762 --- /dev/null +++ b/mirrortea/init_db.py @@ -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 +); +""" diff --git a/mirrortea/matrix.py b/mirrortea/matrix.py new file mode 100644 index 0000000..cf31b5f --- /dev/null +++ b/mirrortea/matrix.py @@ -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 diff --git a/mirrortea/telegram.py b/mirrortea/telegram.py new file mode 100644 index 0000000..a00f027 --- /dev/null +++ b/mirrortea/telegram.py @@ -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) From 84b42b495039218d3c221c4df7bb3d79df00676e Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 04:48:55 +0400 Subject: [PATCH 2/9] THIS IS REFACTOR MOTHER FUCKER --- config.yaml | 2 + mirrortea/__main__.py | 56 ++----------------- .../abstract_source_platform/__init__.py | 3 + .../abstact_source_platform.py | 9 +++ .../telegram.py | 14 ++++- mirrortea/config_dataclass.py | 32 +++++++++++ mirrortea/matrix.py | 3 + mirrortea/models/user.py | 15 +++++ mirrortea/repository/__init__.py | 5 ++ .../repository/abstract_db_repository.py | 7 +++ .../sqlite_init_tables.py} | 1 + mirrortea/repository/sqlite_repository.py | 31 ++++++++++ requirements.txt | 1 + 13 files changed, 128 insertions(+), 51 deletions(-) create mode 100644 config.yaml create mode 100644 mirrortea/abstract_source_platform/__init__.py create mode 100644 mirrortea/abstract_source_platform/abstact_source_platform.py rename mirrortea/{ => abstract_source_platform}/telegram.py (57%) create mode 100644 mirrortea/config_dataclass.py create mode 100644 mirrortea/models/user.py create mode 100644 mirrortea/repository/__init__.py create mode 100644 mirrortea/repository/abstract_db_repository.py rename mirrortea/{init_db.py => repository/sqlite_init_tables.py} (96%) create mode 100644 mirrortea/repository/sqlite_repository.py diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..a5c0786 --- /dev/null +++ b/config.yaml @@ -0,0 +1,2 @@ +matrix_owner_id: "@dettlaff:inex.rocks" +db_path: "data/database.sqlite3" diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index 2db6e70..ebc1718 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -1,51 +1,25 @@ import asyncio import os -import sqlite3 -import sys - -import aiogram as telegram -import nio as matrix from matrix import MatrixLoop -from telegram import TelegramLoop +from mirrortea.abstract_source_platform.telegram import Telegram -from mirrortea.init_db import ( - MATRIX_ROOMS_SQL, - TELEGRAM_USER_MATRIX_CHATS_SQL, - TELEGRAM_USERS_SQL, -) +from config_dataclass import Config + +CONFIG_FILE_NAME = "config.yaml" 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"], - ) + config = Config.from_yaml_config(CONFIG_FILE_NAME) 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"] - # выглядит ужасно :( - - class Application: def __init__(self, config): self.config = config - self.sqlite_adapter = SqliteAdapter(self, config.db_path) self.matrix_loop = MatrixLoop(self) - self.telegram_loop = TelegramLoop(self) + self.telegram = Telegram(self) async def run(self): try: @@ -59,23 +33,5 @@ class Application: await self.matrix_loop.finish() -class SqliteAdapter: - def __init__(self, app, path): - self.app = app - self.path = path - self.conn = sqlite3.connect(path) - self._create_tables() - - def _create_tables(self): - for table in [ - TELEGRAM_USER_MATRIX_CHATS_SQL, - TELEGRAM_USERS_SQL, - MATRIX_ROOMS_SQL, - ]: - self.conn.execute(table) - - self.conn.commit() - - if __name__ == "__main__": main() diff --git a/mirrortea/abstract_source_platform/__init__.py b/mirrortea/abstract_source_platform/__init__.py new file mode 100644 index 0000000..f3ea23b --- /dev/null +++ b/mirrortea/abstract_source_platform/__init__.py @@ -0,0 +1,3 @@ +from mirrortea.abstract_source_platform.telegram import Telegram + +platforms = [Telegram] diff --git a/mirrortea/abstract_source_platform/abstact_source_platform.py b/mirrortea/abstract_source_platform/abstact_source_platform.py new file mode 100644 index 0000000..35cec3b --- /dev/null +++ b/mirrortea/abstract_source_platform/abstact_source_platform.py @@ -0,0 +1,9 @@ +from abc import ABC, abstractmethod + +from mirrortea.models.user import User + + +class AbstractSourcePlatform(ABC): + @abstractmethod + def get_user_information(self) -> User: + """Init tables in database""" diff --git a/mirrortea/telegram.py b/mirrortea/abstract_source_platform/telegram.py similarity index 57% rename from mirrortea/telegram.py rename to mirrortea/abstract_source_platform/telegram.py index a00f027..a8dcb44 100644 --- a/mirrortea/telegram.py +++ b/mirrortea/abstract_source_platform/telegram.py @@ -1,4 +1,13 @@ -class TelegramLoop: +import sys +import aiogram as telegram + +from mirrortea.abstract_source_platform.abstact_source_platform import ( + AbstractSourcePlatform, +) +from mirrortea.models.user import User + + +class Telegram(AbstractSourcePlatform): def __init__(self, app): self.app = app self.bot = telegram.Bot(token=app.config.telegram_bot_token) @@ -10,3 +19,6 @@ class TelegramLoop: async def on_message(self, msg): print(msg, file=sys.stderr) + + async def get_user_information(self) -> User: + pass diff --git a/mirrortea/config_dataclass.py b/mirrortea/config_dataclass.py new file mode 100644 index 0000000..8d87eda --- /dev/null +++ b/mirrortea/config_dataclass.py @@ -0,0 +1,32 @@ +import os +import yaml +from pathlib import Path +from dataclasses import dataclass + +from jinja2 import BaseLoader, Environment + + +def render_env_template(raw_config: str) -> dict: + template = Environment(loader=BaseLoader).from_string(raw_config) + conf = template.render(**os.environ) + return yaml.safe_load(conf) + + +@dataclass +class Config: + db_path: Path + + matrix_bot_id: str + matrix_homeserver_url: str + matrix_password: str + matrix_owner_id: str + telegram_bot_token: str + + @classmethod + def from_dict(config_class, dict): + return config_class(**dict) + + @classmethod + def from_yaml_config(config_class, path: Path): + with open(path) as raw: + return config_class.from_dict(render_env_template(raw.read())) diff --git a/mirrortea/matrix.py b/mirrortea/matrix.py index cf31b5f..7777e20 100644 --- a/mirrortea/matrix.py +++ b/mirrortea/matrix.py @@ -1,3 +1,6 @@ +import nio as matrix + + class MatrixLoop: def __init__(self, app): self.app = app diff --git a/mirrortea/models/user.py b/mirrortea/models/user.py new file mode 100644 index 0000000..e8315e5 --- /dev/null +++ b/mirrortea/models/user.py @@ -0,0 +1,15 @@ +from typing import Optional +from pydantic import BaseModel + + +class User(BaseModel): + """ + Bridge user from platform source + """ + + first_name: str + last_named: Optional[str] + + id: str + username: Optional[str] + avatar_hash: Optional[str] diff --git a/mirrortea/repository/__init__.py b/mirrortea/repository/__init__.py new file mode 100644 index 0000000..4c46a48 --- /dev/null +++ b/mirrortea/repository/__init__.py @@ -0,0 +1,5 @@ +from mirrortea.repository.sqlite_repository import ( + SQLiteDatabaseRepository, +) + +repository = SQLiteDatabaseRepository() diff --git a/mirrortea/repository/abstract_db_repository.py b/mirrortea/repository/abstract_db_repository.py new file mode 100644 index 0000000..a0fd9fc --- /dev/null +++ b/mirrortea/repository/abstract_db_repository.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class AbstractDatabaseRepository(ABC): + @abstractmethod + def create_tables(self) -> None: + """Init tables in database""" diff --git a/mirrortea/init_db.py b/mirrortea/repository/sqlite_init_tables.py similarity index 96% rename from mirrortea/init_db.py rename to mirrortea/repository/sqlite_init_tables.py index 0d6e762..be530c6 100644 --- a/mirrortea/init_db.py +++ b/mirrortea/repository/sqlite_init_tables.py @@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS telegram_users first_name TEXT NOT NULL (50), last_name TEXT (50), username TEXT (50), + avatar_hash BLOB, ); """ diff --git a/mirrortea/repository/sqlite_repository.py b/mirrortea/repository/sqlite_repository.py new file mode 100644 index 0000000..18ccd81 --- /dev/null +++ b/mirrortea/repository/sqlite_repository.py @@ -0,0 +1,31 @@ +import sqlite3 +from pathlib import Path +from typing import Optional + +from mirrortea.repository.abstract_db_repository import AbstractDatabaseRepository + +from mirrortea.repository.sqlite_init_tables import ( + TELEGRAM_USER_MATRIX_CHATS_SQL, + TELEGRAM_USERS_SQL, + MATRIX_ROOMS_SQL, +) + +TABLES_LIST = [ + TELEGRAM_USER_MATRIX_CHATS_SQL, + TELEGRAM_USERS_SQL, + MATRIX_ROOMS_SQL, +] + + +class SQLiteDatabaseRepository(AbstractDatabaseRepository): + def __init__(self, app, path: Path): + self.path = path + self.conn = sqlite3.connect(path) + + @staticmethod + def create_tables(self) -> None: + """Init tables in database""" + + for table in TABLES_LIST: + self.conn.execute(table) + self.conn.commit() diff --git a/requirements.txt b/requirements.txt index 3f3c37f..9dc2e6d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ aiogram==2.24 matrix-nio==0.20.1 +jinja2 \ No newline at end of file From b51b850fa947e7480a0a90c0754adaea40f54d3f Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 06:40:24 +0300 Subject: [PATCH 3/9] fix: delete old from env.example --- .env.example | 2 -- 1 file changed, 2 deletions(-) diff --git a/.env.example b/.env.example index d311fec..2c7e888 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,4 @@ -DB_PATH=data/database.sqlite3 MATRIX_BOT_ID=@fckidiots:matrix.org MATRIX_HOMESERVER_URL=https://matrix.org -MATRIX_OWNER_ID=@kotovalexarian:matrix.org MATRIX_PASSWORD=... TELEGRAM_BOT_TOKEN=5890667880:... From 96ab6c9439e5dff612b7ded98a2d850325d475a4 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 07:45:01 +0400 Subject: [PATCH 4/9] fix: a little --- .env.example | 2 -- mirrortea/__main__.py | 1 - mirrortea/matrix.py | 1 + 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.env.example b/.env.example index d311fec..2c7e888 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,4 @@ -DB_PATH=data/database.sqlite3 MATRIX_BOT_ID=@fckidiots:matrix.org MATRIX_HOMESERVER_URL=https://matrix.org -MATRIX_OWNER_ID=@kotovalexarian:matrix.org MATRIX_PASSWORD=... TELEGRAM_BOT_TOKEN=5890667880:... diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index ebc1718..8bca93e 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -1,5 +1,4 @@ import asyncio -import os from matrix import MatrixLoop from mirrortea.abstract_source_platform.telegram import Telegram diff --git a/mirrortea/matrix.py b/mirrortea/matrix.py index 7777e20..9a2d18f 100644 --- a/mirrortea/matrix.py +++ b/mirrortea/matrix.py @@ -1,4 +1,5 @@ import nio as matrix +import sys class MatrixLoop: From 166b712c6dfa014ccd04284f496c1824b08f8626 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 14:17:12 +0300 Subject: [PATCH 5/9] update README --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1805e99..3e8188d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,54 @@ MirrorTea ========= -New cool Matrix <-> Telegram bridge for personal use, replacement of +New Matrix <-> Telegram bridge for personal use, replacement of **mautrix-telegram**. + + +For the matrix server is represented by a single and regular user. To simulate chats with different users, creates rooms in matrix, and sets /roomnick and /roomavatar corresponding to the simulated user in telegram. + +For telegram, it can be represented as a single bot. + +Advantages over the current mautrix bridge: + +- Supports the bot's private message bridge +- Requires no permanent host (server) +- Can run from a phone (termux) or user computer +- Does not require a domain +- Doesn't require a statistical IP +- No synapse server required +- Easy to deploy, unlike synapse +- Doesn't require any computing power like synapse +- Not tied to a specific matrix server + +Disadvantages: + +- Doesn't support papitas +- no support for groups + +## Install + +#### Clone repository + + +```git clone https://inex.dev/def/MirrorTea && cd MirrorTea``` + + +#### hen edit ```.env``` secrets config + +```mv .env.example .env``` + +```nano .env``` + + +#### Run on *unix-like systems: + +```pip install -r requirements.txt``` + +```python3 mirrortea``` + +#### Run with docker: + +```docker-compose up --build``` + +kotov isprav eto From 7a07d040745d472bcf48899f3a900c620ef41400 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 14:26:56 +0300 Subject: [PATCH 6/9] update README --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3e8188d..a86bd34 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ -MirrorTea +MirrorTea 🐦 ========= -New Matrix <-> Telegram bridge for personal use, replacement of -**mautrix-telegram**. +**New lite Matrix <-> Telegram bridge for personal use, replacement of +mautrix-telegram**. For the matrix server is represented by a single and regular user. To simulate chats with different users, creates rooms in matrix, and sets /roomnick and /roomavatar corresponding to the simulated user in telegram. For telegram, it can be represented as a single bot. -Advantages over the current mautrix bridge: +#### Advantages over the current mautrix bridge: - Supports the bot's private message bridge - Requires no permanent host (server) @@ -21,12 +21,12 @@ Advantages over the current mautrix bridge: - Doesn't require any computing power like synapse - Not tied to a specific matrix server -Disadvantages: +#### Disadvantages: -- Doesn't support papitas -- no support for groups +- Doesn't support papits +- No support for groups -## Install +## Install 🌺 #### Clone repository @@ -34,7 +34,7 @@ Disadvantages: ```git clone https://inex.dev/def/MirrorTea && cd MirrorTea``` -#### hen edit ```.env``` secrets config +#### Then edit ```.env``` secrets config ```mv .env.example .env``` From d2b11ecc11c4b9593255849df771f0e638ee631e Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 16:33:52 +0400 Subject: [PATCH 7/9] fix: imports, requirements --- mirrortea/__main__.py | 2 +- mirrortea/abstract_source_platform/__init__.py | 2 +- mirrortea/abstract_source_platform/abstact_source_platform.py | 2 +- mirrortea/abstract_source_platform/telegram.py | 4 ++-- mirrortea/repository/__init__.py | 2 +- mirrortea/repository/sqlite_repository.py | 4 ++-- requirements.txt | 4 +++- 7 files changed, 11 insertions(+), 9 deletions(-) diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index 8bca93e..37213e1 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -1,7 +1,7 @@ import asyncio from matrix import MatrixLoop -from mirrortea.abstract_source_platform.telegram import Telegram +from abstract_source_platform.telegram import Telegram from config_dataclass import Config diff --git a/mirrortea/abstract_source_platform/__init__.py b/mirrortea/abstract_source_platform/__init__.py index f3ea23b..815fa79 100644 --- a/mirrortea/abstract_source_platform/__init__.py +++ b/mirrortea/abstract_source_platform/__init__.py @@ -1,3 +1,3 @@ -from mirrortea.abstract_source_platform.telegram import Telegram +from abstract_source_platform.telegram import Telegram platforms = [Telegram] diff --git a/mirrortea/abstract_source_platform/abstact_source_platform.py b/mirrortea/abstract_source_platform/abstact_source_platform.py index 35cec3b..f12d8b3 100644 --- a/mirrortea/abstract_source_platform/abstact_source_platform.py +++ b/mirrortea/abstract_source_platform/abstact_source_platform.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod -from mirrortea.models.user import User +from models.user import User class AbstractSourcePlatform(ABC): diff --git a/mirrortea/abstract_source_platform/telegram.py b/mirrortea/abstract_source_platform/telegram.py index a8dcb44..0cb3916 100644 --- a/mirrortea/abstract_source_platform/telegram.py +++ b/mirrortea/abstract_source_platform/telegram.py @@ -1,10 +1,10 @@ import sys import aiogram as telegram -from mirrortea.abstract_source_platform.abstact_source_platform import ( +from abstract_source_platform.abstact_source_platform import ( AbstractSourcePlatform, ) -from mirrortea.models.user import User +from models.user import User class Telegram(AbstractSourcePlatform): diff --git a/mirrortea/repository/__init__.py b/mirrortea/repository/__init__.py index 4c46a48..53fd20a 100644 --- a/mirrortea/repository/__init__.py +++ b/mirrortea/repository/__init__.py @@ -1,4 +1,4 @@ -from mirrortea.repository.sqlite_repository import ( +from repository.sqlite_repository import ( SQLiteDatabaseRepository, ) diff --git a/mirrortea/repository/sqlite_repository.py b/mirrortea/repository/sqlite_repository.py index 18ccd81..65ce56c 100644 --- a/mirrortea/repository/sqlite_repository.py +++ b/mirrortea/repository/sqlite_repository.py @@ -2,9 +2,9 @@ import sqlite3 from pathlib import Path from typing import Optional -from mirrortea.repository.abstract_db_repository import AbstractDatabaseRepository +from repository.abstract_db_repository import AbstractDatabaseRepository -from mirrortea.repository.sqlite_init_tables import ( +from repository.sqlite_init_tables import ( TELEGRAM_USER_MATRIX_CHATS_SQL, TELEGRAM_USERS_SQL, MATRIX_ROOMS_SQL, diff --git a/requirements.txt b/requirements.txt index 9dc2e6d..495b865 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ aiogram==2.24 matrix-nio==0.20.1 -jinja2 \ No newline at end of file +jinja2==3.1.2 +pydantic==1.10.2 +pyyaml==6.0 \ No newline at end of file From d8029623c14cb837b59781cb358b2da9eb804ce2 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 16:45:24 +0400 Subject: [PATCH 8/9] fix: add config to docker --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 5914a9f..9f29f75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,4 +6,5 @@ RUN apt-get install --yes python3 python3-pip COPY requirements.txt /app/ RUN pip3 install -r requirements.txt COPY mirrortea/* /app/mirrortea/ +COPY config.yaml /app/ ENTRYPOINT ["/usr/bin/python3", "/app/mirrortea"] From 9c97b12c196f15193563698b4152f886aff2caa2 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 17:16:50 +0400 Subject: [PATCH 9/9] fix: env config --- .env.example | 4 ++-- config.yaml | 5 +++++ mirrortea/__main__.py | 2 +- mirrortea/config_dataclass.py | 8 +++++--- mirrortea/matrix.py | 4 ++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 2c7e888..c3a41f8 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -MATRIX_BOT_ID=@fckidiots:matrix.org MATRIX_HOMESERVER_URL=https://matrix.org -MATRIX_PASSWORD=... +MATRIX_FULL_BOT_ID=@fckidiots:matrix.org +MATRIX_BOT_PASSWORD=... TELEGRAM_BOT_TOKEN=5890667880:... diff --git a/config.yaml b/config.yaml index a5c0786..6206344 100644 --- a/config.yaml +++ b/config.yaml @@ -1,2 +1,7 @@ matrix_owner_id: "@dettlaff:inex.rocks" db_path: "data/database.sqlite3" + +matrix_homeserver_url: "{{ MATRIX_HOMESERVER_URL }}" +matrix_full_bot_id: "{{ MATRIX_FULL_BOT_ID }}" +matrix_bot_password: "{{ MATRIX_BOT_PASSWORD }}" +telegram_bot_token: "{{ TELEGRAM_BOT_TOKEN }}" diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index 37213e1..2dc2aba 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -25,7 +25,7 @@ class Application: await self.matrix_loop.prepare() await asyncio.gather( self.matrix_loop.run(), - self.telegram_loop.run(), + self.telegram.run(), ) finally: if self.matrix_loop: diff --git a/mirrortea/config_dataclass.py b/mirrortea/config_dataclass.py index 8d87eda..a636c4e 100644 --- a/mirrortea/config_dataclass.py +++ b/mirrortea/config_dataclass.py @@ -16,12 +16,14 @@ def render_env_template(raw_config: str) -> dict: class Config: db_path: Path - matrix_bot_id: str matrix_homeserver_url: str - matrix_password: str - matrix_owner_id: str + matrix_full_bot_id: str + matrix_bot_password: str + telegram_bot_token: str + matrix_owner_id: str + @classmethod def from_dict(config_class, dict): return config_class(**dict) diff --git a/mirrortea/matrix.py b/mirrortea/matrix.py index 9a2d18f..061b4a3 100644 --- a/mirrortea/matrix.py +++ b/mirrortea/matrix.py @@ -7,12 +7,12 @@ class MatrixLoop: self.app = app self.client = matrix.AsyncClient( app.config.matrix_homeserver_url, - app.config.matrix_bot_id, + app.config.matrix_full_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) + await self.client.login(self.app.config.matrix_bot_password) async def finish(self): await self.client.close()