1
0
Fork 0

Compare commits

...

9 Commits

Author SHA1 Message Date
Alex Kotov 2ce7587d0a
Create table "telegram_user_matrix_chats" 2023-01-09 01:33:19 +04:00
Alex Kotov 1d77e32174
Use config instead of forwarding all the variables 2023-01-09 01:18:45 +04:00
Alex Kotov ca1af360a4
Improve env vars 2023-01-09 01:14:14 +04:00
Alex Kotov 9c1a12fb89
Fix code 2023-01-09 00:53:15 +04:00
Alex Kotov 23491f17c7
Add SQLite database 2023-01-09 00:39:15 +04:00
Alex Kotov e6f40ac0a6
Forward the application object 2023-01-08 23:33:30 +04:00
Alex Kotov dfa4c10138
Add config 2023-01-08 23:31:42 +04:00
Alex Kotov 2f5694cffb
Add an application class 2023-01-08 23:31:42 +04:00
def 3c40b08b02 Update 'README.md' 2023-01-08 22:12:55 +03:00
6 changed files with 75 additions and 31 deletions

View File

@ -1,4 +1,6 @@
DB_PATH=data/database.sqlite3
MATRIX_BOT_ID=@fckidiots:matrix.org
MATRIX_HOMESERVER_URL=https://matrix.org
MATRIX_FULL_USER_ID=@fckidiots:matrix.org
MATRIX_OWNER_ID=@kotovalexarian:matrix.org
MATRIX_PASSWORD=...
TELEGRAM_BOT_TOKEN=5890667880:...

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
__pycache__/
/.env
/data/
!/data/.keep

View File

@ -1,5 +1,5 @@
FROM ubuntu:22.10
RUN mkdir -p /app/mirrortea/
RUN mkdir -p /app/data/ /app/mirrortea/
WORKDIR /app
RUN apt-get update --yes
RUN apt-get install --yes python3 python3-pip

View File

@ -3,6 +3,3 @@ MirrorTea
New cool Matrix <-> Telegram bridge for personal use, replacement of
**mautrix-telegram**.
Новый мост Matrix <-> Telegram для персонального использования, на замену
**mautrix-telegram**.

View File

@ -5,4 +5,5 @@ services:
build: .
env_file: '.env'
volumes:
- './mirrortea:/app/mirrortea/'
- './data/:/app/data/'
- './mirrortea/:/app/mirrortea/'

View File

@ -1,41 +1,71 @@
import asyncio
import os
import sqlite3
import sys
import aiogram as telegram
import nio as matrix
MATRIX_HOMESERVER_URL = os.environ['MATRIX_HOMESERVER_URL']
MATRIX_FULL_USER_ID = os.environ['MATRIX_FULL_USER_ID']
MATRIX_PASSWORD = os.environ['MATRIX_PASSWORD']
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
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)
)
'''
async def main():
try:
matrix_loop = None
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'],
)
matrix_loop = MatrixLoop(MATRIX_HOMESERVER_URL, MATRIX_FULL_USER_ID,
MATRIX_PASSWORD)
await matrix_loop.prepare()
asyncio.run(Application(config).run())
telegram_loop = TelegramLoop(TELEGRAM_BOT_TOKEN)
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']
await asyncio.gather(
matrix_loop.run(),
telegram_loop.run(),
)
finally:
if matrix_loop:
await matrix_loop.finish()
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)
async def run(self):
try:
await self.matrix_loop.prepare()
await asyncio.gather(
self.matrix_loop.run(),
self.telegram_loop.run(),
)
finally:
if self.matrix_loop:
await self.matrix_loop.finish()
class MatrixLoop:
def __init__(self, homeserver_url, full_user_id, password):
self.password = password
self.client = matrix.AsyncClient(homeserver_url, full_user_id)
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.password)
await self.client.login(self.app.config.matrix_password)
async def finish(self):
await self.client.close()
@ -47,8 +77,9 @@ class MatrixLoop:
print(room, event, file=sys.stderr)
class TelegramLoop:
def __init__(self, bot_token):
self.bot = telegram.Bot(token=bot_token)
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)
@ -58,5 +89,16 @@ class TelegramLoop:
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__':
asyncio.run(main())
main()