From 2b2432b4d53b7f31fdaf911ad9ef9b8417cb8e15 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 18 Mar 2019 09:40:45 +0100 Subject: [PATCH] Implement RoyalnetLink on TelegramBot --- royalnet/bots/telegram.py | 18 ++++++++++++------ royalnet/network/__init__.py | 6 ++++++ royalnet/network/royalnetlink.py | 5 +++-- royalnet/utils/call.py | 3 ++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index 63827995..4d4087d1 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -4,20 +4,24 @@ import typing import multiprocessing from ..commands import NullCommand from ..utils import asyncify, Call, Command +from ..network import RoyalnetLink, Message + + +async def null(message: Message): + pass class TelegramBot: def __init__(self, api_key: str, - *, + master_server_uri: str, commands: typing.List[typing.Type[Command]], - missing_command: Command = NullCommand, - network: multiprocessing.connection.Connection): + missing_command: Command = NullCommand): self.bot: telegram.Bot = telegram.Bot(api_key) self.should_run: bool = False self.offset: int = -100 self.missing_command: typing.Callable = missing_command - self.network: multiprocessing.connection.Connection = network + self.network: RoyalnetLink = RoyalnetLink(master_server_uri, "Telegram", null) # Generate commands self.commands = {} for command in commands: @@ -30,8 +34,10 @@ class TelegramBot: async def reply(self, text: str): await asyncify(self.channel.send_message, text, parse_mode="HTML") - async def network(self, data): - self.network.send + async def net_request(self, message: Message, destination: str): + response = await self.network.request(message, destination) + return response + self.Call = TelegramCall async def run(self): diff --git a/royalnet/network/__init__.py b/royalnet/network/__init__.py index e69de29b..8393cb8f 100644 --- a/royalnet/network/__init__.py +++ b/royalnet/network/__init__.py @@ -0,0 +1,6 @@ +from .messages import Message, ErrorMessage, InvalidSecretErrorMessage +from .royalnetlink import RoyalnetLink, NetworkError, NotConnectedError, NotIdentifiedError +from .packages import Package, TwoWayPackage + +__all__ = ["Message", "ErrorMessage", "InvalidSecretErrorMessage", "RoyalnetLink", "NetworkError", "NotConnectedError", + "NotIdentifiedError", "Package", "TwoWayPackage"] diff --git a/royalnet/network/royalnetlink.py b/royalnet/network/royalnetlink.py index 4d62e19c..08e38bab 100644 --- a/royalnet/network/royalnetlink.py +++ b/royalnet/network/royalnetlink.py @@ -5,7 +5,7 @@ import uuid import functools import typing import pickle -from .messages import Message, IdentifyMessage, ErrorMessage +from .messages import Message, ErrorMessage from .packages import Package, TwoWayPackage loop = asyncio.get_event_loop() @@ -35,8 +35,9 @@ class PendingRequest: class RoyalnetLink: - def __init__(self, master_uri: str, request_handler): + def __init__(self, master_uri: str, link_type: str, request_handler): self.master_uri: str = master_uri + self.link_type: str = link_type self.nid: str = str(uuid.uuid4()) self.websocket: typing.Optional[websockets.WebSocketClientProtocol] = None self.identified: bool = False diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index 61694b5b..a3181168 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -1,4 +1,5 @@ import typing +from ..network.messages import Message from .command import Command, CommandArgs @@ -13,7 +14,7 @@ class Call: """Send a text message to the channel the call was made.""" raise NotImplementedError() - async def network(self, data): + async def net_request(self, message: Message, destination: str): """Send data to the rest of the Royalnet, and optionally wait for an answer. The data must be pickleable.""" raise NotImplementedError()