localhost_contrib #2
|
@ -38,7 +38,11 @@ For telegram, it can be represented as a single bot.
|
||||||
|
|
||||||
```mv .env.example .env```
|
```mv .env.example .env```
|
||||||
|
|
||||||
```nano .env```
|
```nano .env```
|
||||||
|
|
||||||
|
Make sure that .env file have permissions 600 (read\write only for owner)
|
||||||
|
|
||||||
|
```chmod 600 .env```
|
||||||
|
|
||||||
|
|
||||||
#### Run on *unix-like systems:
|
#### Run on *unix-like systems:
|
||||||
|
@ -50,5 +54,3 @@ For telegram, it can be represented as a single bot.
|
||||||
#### Run with docker:
|
#### Run with docker:
|
||||||
|
|
||||||
```docker-compose up --build```
|
```docker-compose up --build```
|
||||||
|
|
||||||
kotov isprav eto
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import asyncio
|
import sys
|
||||||
|
|
||||||
from matrix import MatrixLoop
|
import asyncio as telegram
|
||||||
from abstract_source_platform.telegram import Telegram
|
import nio as matrix
|
||||||
|
|
||||||
from config_dataclass import Config
|
from config_dataclass import Config
|
||||||
|
|
||||||
|
@ -11,14 +11,61 @@ CONFIG_FILE_NAME = "config.yaml"
|
||||||
def main():
|
def main():
|
||||||
config = Config.from_yaml_config(CONFIG_FILE_NAME)
|
config = Config.from_yaml_config(CONFIG_FILE_NAME)
|
||||||
|
|
||||||
asyncio.run(Application(config).run())
|
telegram.run(Application(config).run())
|
||||||
|
|
||||||
|
|
||||||
|
class TelegramLopp:
|
||||||
|
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) -> None:
|
||||||
|
await self.dispatcher.start_polling()
|
||||||
|
|
||||||
|
async def on_message(self, msg):
|
||||||
|
print(msg, file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
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 = TelegramLopp(self)
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -7,3 +7,7 @@ class AbstractSourcePlatform(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_user_information(self) -> User:
|
def get_user_information(self) -> User:
|
||||||
"""Init tables in database"""
|
"""Init tables in database"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def run(self) -> None:
|
||||||
|
"""Platform launch"""
|
||||||
|
|
|
@ -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,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
|
|
Reference in a new issue