diff --git a/.gitignore b/.gitignore index 38cd9a8..7b09b15 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__/ /.env /data/ !/data/.keep +test_database.db \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9b38853 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/README.md b/README.md index a86bd34..c5ffafe 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,21 @@ MirrorTea 🐦 ========= -**New lite Matrix <-> Telegram bridge for personal use, replacement of -mautrix-telegram**. +**New lite [Matrix](https://matrix.org) <-> [Telegram](https://telegram.org) bridge for personal use, replacement of +[mautrix-telegram](https://github.com/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](https://github.com/mautrix/telegram): - 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 +- Doesn't require a static IP - No synapse server required - Easy to deploy, unlike synapse - Doesn't require any computing power like synapse @@ -40,6 +40,15 @@ For telegram, it can be represented as a single bot. ```nano .env``` +Make sure the .env file have permissions 600 (read\write only for owner) + +```chmod 600 .env``` + +#### Edit ```config.yaml``` + +```nano config.yaml``` + +Do not change options with brackets ```"{{ }}"```, these are templates. #### Run on *unix-like systems: diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index c49c7d3..8854027 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -1,7 +1,6 @@ import sys -import asyncio as telegram -import nio as matrix +from messengers import Matrix as MatrixLoop, Telegram as TelegramLoop from config_dataclass import Config @@ -65,14 +64,14 @@ class Application: def __init__(self, config): self.config = config self.matrix_loop = MatrixLoop(self) - self.telegram = TelegramLopp(self) + self.telegram_loop = TelegramLoop(self) async def run(self): try: await self.matrix_loop.prepare() await asyncio.gather( self.matrix_loop.run(), - self.telegram.run(), + self.telegram_loop.run(), ) finally: if self.matrix_loop: diff --git a/mirrortea/abstract_source_platform/__init__.py b/mirrortea/abstract_source_platform/__init__.py deleted file mode 100644 index 815fa79..0000000 --- a/mirrortea/abstract_source_platform/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from abstract_source_platform.telegram import Telegram - -platforms = [Telegram] diff --git a/mirrortea/messengers/__init__.py b/mirrortea/messengers/__init__.py new file mode 100644 index 0000000..11d43e6 --- /dev/null +++ b/mirrortea/messengers/__init__.py @@ -0,0 +1,2 @@ +from .matrix import Matrix +from .telegram import Telegram diff --git a/mirrortea/messengers/matrix.py b/mirrortea/messengers/matrix.py new file mode 100644 index 0000000..4048f27 --- /dev/null +++ b/mirrortea/messengers/matrix.py @@ -0,0 +1,23 @@ +import nio +import sys + +class Matrix: + def __init__(self, app): + self.app = app + self.client = nio.AsyncClient( + app.config.matrix_homeserver_url, + app.config.matrix_full_bot_id, + ) + self.client.add_event_callback(self.on_message, nio.RoomMessage) + + async def prepare(self): + await self.client.login(self.app.config.matrix_bot_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) diff --git a/mirrortea/messengers/telegram.py b/mirrortea/messengers/telegram.py new file mode 100644 index 0000000..c3355e5 --- /dev/null +++ b/mirrortea/messengers/telegram.py @@ -0,0 +1,15 @@ +import aiogram +import sys + +class Telegram: + def __init__(self, app): + self.app = app + self.bot = aiogram.Bot(token=app.config.telegram_bot_token) + self.dispatcher = aiogram.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)