2023-01-07 20:11:46 +00:00
|
|
|
import asyncio
|
2023-01-08 13:01:33 +00:00
|
|
|
import os
|
2023-01-08 20:39:15 +00:00
|
|
|
import sqlite3
|
2023-01-08 12:55:14 +00:00
|
|
|
import sys
|
2023-01-07 21:13:27 +00:00
|
|
|
|
|
|
|
import aiogram as telegram
|
|
|
|
import nio as matrix
|
2023-01-07 20:11:46 +00:00
|
|
|
|
2023-01-08 21:33:19 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
|
2023-01-08 19:28:13 +00:00
|
|
|
def main():
|
2023-01-08 19:31:11 +00:00
|
|
|
config = Config(
|
2023-01-08 20:39:15 +00:00
|
|
|
db_path=os.environ['DB_PATH'],
|
2023-01-08 21:08:52 +00:00
|
|
|
matrix_bot_id=os.environ['MATRIX_BOT_ID'],
|
2023-01-08 19:31:11 +00:00
|
|
|
matrix_homeserver_url=os.environ['MATRIX_HOMESERVER_URL'],
|
2023-01-08 21:08:52 +00:00
|
|
|
matrix_owner_id=os.environ['MATRIX_OWNER_ID'],
|
2023-01-08 19:31:11 +00:00
|
|
|
matrix_password=os.environ['MATRIX_PASSWORD'],
|
|
|
|
telegram_bot_token=os.environ['TELEGRAM_BOT_TOKEN'],
|
|
|
|
)
|
|
|
|
|
|
|
|
asyncio.run(Application(config).run())
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
def __init__(self, **kwargs):
|
2023-01-08 20:39:15 +00:00
|
|
|
self.db_path = kwargs['db_path']
|
2023-01-08 21:08:52 +00:00
|
|
|
self.matrix_bot_id = kwargs['matrix_bot_id']
|
2023-01-08 19:31:11 +00:00
|
|
|
self.matrix_homeserver_url = kwargs['matrix_homeserver_url']
|
2023-01-08 21:08:52 +00:00
|
|
|
self.matrix_owner_id = kwargs['matrix_owner_id']
|
2023-01-08 19:31:11 +00:00
|
|
|
self.matrix_password = kwargs['matrix_password']
|
|
|
|
self.telegram_bot_token = kwargs['telegram_bot_token']
|
2023-01-07 21:42:02 +00:00
|
|
|
|
2023-01-08 19:28:13 +00:00
|
|
|
class Application:
|
2023-01-08 19:31:11 +00:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
2023-01-08 20:53:15 +00:00
|
|
|
self.sqlite_adapter = SqliteAdapter(self, config.db_path)
|
2023-01-08 21:18:45 +00:00
|
|
|
self.matrix_loop = MatrixLoop(self)
|
|
|
|
self.telegram_loop = TelegramLoop(self)
|
2023-01-08 19:28:13 +00:00
|
|
|
|
|
|
|
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()
|
2023-01-08 18:43:37 +00:00
|
|
|
|
|
|
|
class MatrixLoop:
|
2023-01-08 21:18:45 +00:00
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
self.client = matrix.AsyncClient(
|
|
|
|
app.config.matrix_homeserver_url,
|
|
|
|
app.config.matrix_bot_id,
|
|
|
|
)
|
2023-01-08 18:43:37 +00:00
|
|
|
self.client.add_event_callback(self.on_message, matrix.RoomMessage)
|
|
|
|
|
|
|
|
async def prepare(self):
|
2023-01-08 21:18:45 +00:00
|
|
|
await self.client.login(self.app.config.matrix_password)
|
2023-01-08 18:43:37 +00:00
|
|
|
|
|
|
|
async def finish(self):
|
|
|
|
await self.client.close()
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
await self.client.sync_forever(timeout=30000)
|
2023-01-07 21:05:21 +00:00
|
|
|
|
2023-01-08 18:43:37 +00:00
|
|
|
async def on_message(self, room, event):
|
|
|
|
print(room, event, file=sys.stderr)
|
2023-01-07 20:11:46 +00:00
|
|
|
|
2023-01-08 18:43:37 +00:00
|
|
|
class TelegramLoop:
|
2023-01-08 21:18:45 +00:00
|
|
|
def __init__(self, app):
|
2023-01-08 19:33:30 +00:00
|
|
|
self.app = app
|
2023-01-08 21:18:45 +00:00
|
|
|
self.bot = telegram.Bot(token=app.config.telegram_bot_token)
|
2023-01-08 18:43:37 +00:00
|
|
|
self.dispatcher = telegram.Dispatcher(bot=self.bot)
|
|
|
|
self.dispatcher.register_message_handler(self.on_message)
|
2023-01-07 21:29:12 +00:00
|
|
|
|
2023-01-08 18:43:37 +00:00
|
|
|
async def run(self):
|
2023-01-08 18:51:45 +00:00
|
|
|
await self.dispatcher.start_polling()
|
2023-01-08 12:46:08 +00:00
|
|
|
|
2023-01-08 18:43:37 +00:00
|
|
|
async def on_message(self, msg):
|
|
|
|
print(msg, file=sys.stderr)
|
2023-01-08 13:54:22 +00:00
|
|
|
|
2023-01-08 20:39:15 +00:00
|
|
|
class SqliteAdapter:
|
|
|
|
def __init__(self, app, path):
|
|
|
|
self.app = app
|
|
|
|
self.path = path
|
|
|
|
self.conn = sqlite3.connect(path)
|
2023-01-08 21:33:19 +00:00
|
|
|
self._create_tables()
|
|
|
|
|
|
|
|
def _create_tables(self):
|
|
|
|
self.conn.execute(TELEGRAM_USER_MATRIX_CHATS_SQL)
|
|
|
|
self.conn.commit()
|
2023-01-08 20:39:15 +00:00
|
|
|
|
2023-01-07 20:11:46 +00:00
|
|
|
if __name__ == '__main__':
|
2023-01-08 19:28:13 +00:00
|
|
|
main()
|