Compare commits
2 Commits
f212a8d411
...
467d40d026
Author | SHA1 | Date |
---|---|---|
Alex Kotov | 467d40d026 | |
Alex Kotov | 5c156e92f1 |
|
@ -1,7 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from matrix import MatrixLoop
|
from messengers import Matrix as MatrixLoop, Telegram as TelegramLoop
|
||||||
from abstract_source_platform.telegram import Telegram
|
|
||||||
|
|
||||||
from config_dataclass import Config
|
from config_dataclass import Config
|
||||||
|
|
||||||
|
@ -18,14 +17,14 @@ class Application:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = config
|
self.config = config
|
||||||
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:
|
||||||
|
|
|
@ -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
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
from .matrix import Matrix
|
||||||
|
from .telegram import Telegram
|
|
@ -1,15 +1,14 @@
|
||||||
import nio as matrix
|
import nio
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
class Matrix:
|
||||||
class MatrixLoop:
|
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.client = matrix.AsyncClient(
|
self.client = nio.AsyncClient(
|
||||||
app.config.matrix_homeserver_url,
|
app.config.matrix_homeserver_url,
|
||||||
app.config.matrix_full_bot_id,
|
app.config.matrix_full_bot_id,
|
||||||
)
|
)
|
||||||
self.client.add_event_callback(self.on_message, matrix.RoomMessage)
|
self.client.add_event_callback(self.on_message, nio.RoomMessage)
|
||||||
|
|
||||||
async def prepare(self):
|
async def prepare(self):
|
||||||
await self.client.login(self.app.config.matrix_bot_password)
|
await self.client.login(self.app.config.matrix_bot_password)
|
||||||
|
@ -24,7 +23,7 @@ class MatrixLoop:
|
||||||
print(room, event, file=sys.stderr)
|
print(room, event, file=sys.stderr)
|
||||||
|
|
||||||
def upgrade_room(self, room, telegram_nickname):
|
def upgrade_room(self, room, telegram_nickname):
|
||||||
event_dict = matrix.event_builders.event_builder.EventBuilder(
|
event_dict = nio.event_builders.event_builder.EventBuilder(
|
||||||
name=telegram_nickname
|
name=telegram_nickname
|
||||||
).as_dict()
|
).as_dict()
|
||||||
client.room_send(
|
client.room_send(
|
|
@ -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)
|
Reference in New Issue