Compare commits
No commits in common. "f0d8e55da59be79a41114b14ea46d13773f54afc" and "8556316cd83220aef6a8cde8203a8d94136fc358" have entirely different histories.
f0d8e55da5
...
8556316cd8
|
@ -1,4 +1,6 @@
|
||||||
|
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_BOT_ID=@fckidiots:matrix.org
|
MATRIX_OWNER_ID=@kotovalexarian:matrix.org
|
||||||
MATRIX_BOT_PASSWORD=...
|
MATRIX_PASSWORD=...
|
||||||
TELEGRAM_BOT_TOKEN=5890667880:...
|
TELEGRAM_BOT_TOKEN=5890667880:...
|
||||||
|
|
|
@ -6,5 +6,4 @@ RUN apt-get install --yes python3 python3-pip
|
||||||
COPY requirements.txt /app/
|
COPY requirements.txt /app/
|
||||||
RUN pip3 install -r requirements.txt
|
RUN pip3 install -r requirements.txt
|
||||||
COPY mirrortea/* /app/mirrortea/
|
COPY mirrortea/* /app/mirrortea/
|
||||||
COPY config.yaml /app/
|
|
||||||
ENTRYPOINT ["/usr/bin/python3", "/app/mirrortea"]
|
ENTRYPOINT ["/usr/bin/python3", "/app/mirrortea"]
|
||||||
|
|
55
README.md
55
README.md
|
@ -1,54 +1,5 @@
|
||||||
MirrorTea 🐦
|
MirrorTea
|
||||||
=========
|
=========
|
||||||
|
|
||||||
**New lite Matrix <-> Telegram bridge for personal use, replacement of
|
New cool Matrix <-> Telegram bridge for personal use, replacement of
|
||||||
mautrix-telegram**.
|
**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 papits
|
|
||||||
- No support for groups
|
|
||||||
|
|
||||||
## Install 🌺
|
|
||||||
|
|
||||||
#### Clone repository
|
|
||||||
|
|
||||||
|
|
||||||
```git clone https://inex.dev/def/MirrorTea && cd MirrorTea```
|
|
||||||
|
|
||||||
|
|
||||||
#### Then 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
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
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 }}"
|
|
|
@ -1,36 +1,115 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import os
|
||||||
|
import sqlite3
|
||||||
|
import sys
|
||||||
|
|
||||||
from matrix import MatrixLoop
|
import aiogram as telegram
|
||||||
from abstract_source_platform.telegram import Telegram
|
import nio as matrix
|
||||||
|
|
||||||
from config_dataclass import Config
|
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)
|
||||||
|
);
|
||||||
|
'''
|
||||||
|
|
||||||
CONFIG_FILE_NAME = "config.yaml"
|
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),
|
||||||
|
);
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = Config.from_yaml_config(CONFIG_FILE_NAME)
|
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'],
|
||||||
|
)
|
||||||
|
|
||||||
asyncio.run(Application(config).run())
|
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:
|
class Application:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self.sqlite_adapter = SqliteAdapter(self, config.db_path)
|
||||||
self.matrix_loop = MatrixLoop(self)
|
self.matrix_loop = MatrixLoop(self)
|
||||||
self.telegram = Telegram(self)
|
self.telegram_loop = TelegramLoop(self)
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
try:
|
try:
|
||||||
await self.matrix_loop.prepare()
|
await self.matrix_loop.prepare()
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
self.matrix_loop.run(),
|
self.matrix_loop.run(),
|
||||||
self.telegram.run(),
|
self.telegram_loop.run(),
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
if self.matrix_loop:
|
if self.matrix_loop:
|
||||||
await self.matrix_loop.finish()
|
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)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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):
|
||||||
|
self.app = app
|
||||||
|
self.path = path
|
||||||
|
self.conn = sqlite3.connect(path)
|
||||||
|
self._create_tables()
|
||||||
|
|
||||||
|
def _create_tables(self):
|
||||||
|
self.conn.execute(TELEGRAM_USER_MATRIX_CHATS_SQL)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
from abstract_source_platform.telegram import Telegram
|
|
||||||
|
|
||||||
platforms = [Telegram]
|
|
|
@ -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"""
|
|
|
@ -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
|
|
|
@ -1,34 +0,0 @@
|
||||||
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_homeserver_url: 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)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_yaml_config(config_class, path: Path):
|
|
||||||
with open(path) as raw:
|
|
||||||
return config_class.from_dict(render_env_template(raw.read()))
|
|
|
@ -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
|
|
|
@ -1,15 +0,0 @@
|
||||||
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]
|
|
|
@ -1,5 +0,0 @@
|
||||||
from repository.sqlite_repository import (
|
|
||||||
SQLiteDatabaseRepository,
|
|
||||||
)
|
|
||||||
|
|
||||||
repository = SQLiteDatabaseRepository()
|
|
|
@ -1,7 +0,0 @@
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractDatabaseRepository(ABC):
|
|
||||||
@abstractmethod
|
|
||||||
def create_tables(self) -> None:
|
|
||||||
"""Init tables in database"""
|
|
|
@ -1,27 +0,0 @@
|
||||||
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),
|
|
||||||
avatar_hash BLOB,
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
MATRIX_ROOMS_SQL = """
|
|
||||||
CREATE TABLE IF NOT EXISTS telegram_users
|
|
||||||
(
|
|
||||||
id TEXT PRIMARY KEY NOT NULL
|
|
||||||
);
|
|
||||||
"""
|
|
|
@ -1,31 +0,0 @@
|
||||||
import sqlite3
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from repository.abstract_db_repository import AbstractDatabaseRepository
|
|
||||||
|
|
||||||
from 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()
|
|
|
@ -1,5 +1,2 @@
|
||||||
aiogram==2.24
|
aiogram==2.24
|
||||||
matrix-nio==0.20.1
|
matrix-nio==0.20.1
|
||||||
jinja2==3.1.2
|
|
||||||
pydantic==1.10.2
|
|
||||||
pyyaml==6.0
|
|
Reference in a new issue