Compare commits

...

2 Commits

Author SHA1 Message Date
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
5 changed files with 25 additions and 5 deletions

View File

@ -1,3 +1,4 @@
DB_PATH=data/database.sqlite3
MATRIX_HOMESERVER_URL=https://matrix.org
MATRIX_FULL_USER_ID=@fckidiots:matrix.org
MATRIX_PASSWORD=...

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

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

View File

@ -1,5 +1,6 @@
import asyncio
import os
import sqlite3
import sys
import aiogram as telegram
@ -7,6 +8,7 @@ import nio as matrix
def main():
config = Config(
db_path=os.environ['DB_PATH'],
matrix_homeserver_url=os.environ['MATRIX_HOMESERVER_URL'],
matrix_full_user_id=os.environ['MATRIX_FULL_USER_ID'],
matrix_password=os.environ['MATRIX_PASSWORD'],
@ -17,6 +19,7 @@ def main():
class Config:
def __init__(self, **kwargs):
self.db_path = kwargs['db_path']
self.matrix_homeserver_url = kwargs['matrix_homeserver_url']
self.matrix_full_user_id = kwargs['matrix_full_user_id']
self.matrix_password = kwargs['matrix_password']
@ -25,12 +28,17 @@ class Config:
class Application:
def __init__(self, config):
self.config = config
self.sqlite_adapter = SqliteAdapter(config.db_path)
self.matrix_loop = MatrixLoop(
self,
config.matrix_homeserver_url,
config.matrix_full_user_id,
config.matrix_password,
)
self.telegram_loop = TelegramLoop(config.telegram_bot_token)
self.telegram_loop = TelegramLoop(
self,
config.telegram_bot_token,
)
async def run(self):
try:
@ -44,7 +52,8 @@ class Application:
await self.matrix_loop.finish()
class MatrixLoop:
def __init__(self, homeserver_url, full_user_id, password):
def __init__(self, app, homeserver_url, full_user_id, password):
self.app = app
self.password = password
self.client = matrix.AsyncClient(homeserver_url, full_user_id)
self.client.add_event_callback(self.on_message, matrix.RoomMessage)
@ -62,7 +71,8 @@ class MatrixLoop:
print(room, event, file=sys.stderr)
class TelegramLoop:
def __init__(self, bot_token):
def __init__(self, app, bot_token):
self.app = app
self.bot = telegram.Bot(token=bot_token)
self.dispatcher = telegram.Dispatcher(bot=self.bot)
self.dispatcher.register_message_handler(self.on_message)
@ -73,5 +83,11 @@ 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)
if __name__ == '__main__':
main()