From 7c75f786cb40459cf51e93b6fdae62bcbb6db6c7 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 16:49:30 +0300 Subject: [PATCH 1/5] update README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index a86bd34..7a3a6d0 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,11 @@ For telegram, it can be represented as a single bot. ```nano .env``` +#### Edit ```config.yaml``` + +```nano config.yaml``` + +Do not change options with brackets ```"{{ }}"```, these are templates. #### Run on *unix-like systems: From 381b1984271f5fffc111dcf909ea431b255b1f40 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 10 Jan 2023 21:46:31 +0300 Subject: [PATCH 2/5] add warning about .env file --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7a3a6d0..3fcdc23 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ 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``` From 5931f252a02687e4450faa00dfd56397df6e525f Mon Sep 17 00:00:00 2001 From: def Date: Wed, 11 Jan 2023 23:17:34 +0300 Subject: [PATCH 3/5] fix typo in readme, add links --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3fcdc23..d2eeed4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ MirrorTea 🐦 ========= -**New lite Matrix <-> Telegram bridge for personal use, replacement of +**New lite [Matrix](https://matrix.org) <-> [Telegram](https://telegram.org) bridge for personal use, replacement of mautrix-telegram**. @@ -15,7 +15,7 @@ For telegram, it can be represented as a single bot. - 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 From f212a8d411c10fafcdbc553bff2cd9787819854f Mon Sep 17 00:00:00 2001 From: def Date: Wed, 11 Jan 2023 23:19:01 +0300 Subject: [PATCH 4/5] add links to mautrix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d2eeed4..c5ffafe 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ MirrorTea 🐦 ========= **New lite [Matrix](https://matrix.org) <-> [Telegram](https://telegram.org) bridge for personal use, replacement of -mautrix-telegram**. +[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) From 2ea41e118721451e58c11ba2efba7528e96153dd Mon Sep 17 00:00:00 2001 From: kotovalexarian Date: Sat, 14 Jan 2023 20:27:41 +0300 Subject: [PATCH 5/5] Move messengers to separate module (#3) Co-authored-by: Alex Kotov Reviewed-on: https://inex.dev/def/MirrorTea/pulls/3 --- mirrortea/__main__.py | 7 ++-- .../abstract_source_platform/__init__.py | 3 -- .../abstact_source_platform.py | 9 ----- .../abstract_source_platform/telegram.py | 24 ------------- mirrortea/matrix.py | 35 ------------------- mirrortea/messengers/__init__.py | 2 ++ mirrortea/messengers/matrix.py | 23 ++++++++++++ mirrortea/messengers/telegram.py | 15 ++++++++ 8 files changed, 43 insertions(+), 75 deletions(-) delete mode 100644 mirrortea/abstract_source_platform/__init__.py delete mode 100644 mirrortea/abstract_source_platform/abstact_source_platform.py delete mode 100644 mirrortea/abstract_source_platform/telegram.py delete mode 100644 mirrortea/matrix.py create mode 100644 mirrortea/messengers/__init__.py create mode 100644 mirrortea/messengers/matrix.py create mode 100644 mirrortea/messengers/telegram.py diff --git a/mirrortea/__main__.py b/mirrortea/__main__.py index 2dc2aba..c37c69d 100644 --- a/mirrortea/__main__.py +++ b/mirrortea/__main__.py @@ -1,7 +1,6 @@ import asyncio -from matrix import MatrixLoop -from abstract_source_platform.telegram import Telegram +from messengers import Matrix as MatrixLoop, Telegram as TelegramLoop from config_dataclass import Config @@ -18,14 +17,14 @@ class Application: def __init__(self, config): self.config = config self.matrix_loop = MatrixLoop(self) - self.telegram = Telegram(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/abstract_source_platform/abstact_source_platform.py b/mirrortea/abstract_source_platform/abstact_source_platform.py deleted file mode 100644 index f12d8b3..0000000 --- a/mirrortea/abstract_source_platform/abstact_source_platform.py +++ /dev/null @@ -1,9 +0,0 @@ -from abc import ABC, abstractmethod - -from models.user import User - - -class AbstractSourcePlatform(ABC): - @abstractmethod - def get_user_information(self) -> User: - """Init tables in database""" diff --git a/mirrortea/abstract_source_platform/telegram.py b/mirrortea/abstract_source_platform/telegram.py deleted file mode 100644 index 0cb3916..0000000 --- a/mirrortea/abstract_source_platform/telegram.py +++ /dev/null @@ -1,24 +0,0 @@ -import sys -import aiogram as telegram - -from abstract_source_platform.abstact_source_platform import ( - AbstractSourcePlatform, -) -from models.user import User - - -class Telegram(AbstractSourcePlatform): - 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) - - async def get_user_information(self) -> User: - pass diff --git a/mirrortea/matrix.py b/mirrortea/matrix.py deleted file mode 100644 index 061b4a3..0000000 --- a/mirrortea/matrix.py +++ /dev/null @@ -1,35 +0,0 @@ -import nio as matrix -import sys - - -class MatrixLoop: - def __init__(self, app): - self.app = app - self.client = matrix.AsyncClient( - app.config.matrix_homeserver_url, - 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_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) - - 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/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)