diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index cc7eb58f..63827995 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -1,21 +1,27 @@ import telegram import asyncio import typing +import multiprocessing from ..commands import NullCommand from ..utils import asyncify, Call, Command class TelegramBot: - def __init__(self, api_key: str, commands: typing.List[typing.Type[Command]], *, missing_command: Command=NullCommand): - self.bot = telegram.Bot(api_key) - self.should_run = False - self.offset = -100 - self.commands = commands + def __init__(self, + api_key: str, + *, + commands: typing.List[typing.Type[Command]], + missing_command: Command = NullCommand, + network: multiprocessing.connection.Connection): + 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 # Generate commands - self._commands = {} - for command in self.commands: - self._commands[f"/{command.command_name}"] = command + self.commands = {} + for command in commands: + self.commands[f"/{command.command_name}"] = command class TelegramCall(Call): interface_name = "telegram" @@ -23,6 +29,9 @@ 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 self.Call = TelegramCall async def run(self): diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index e7862200..61694b5b 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -1,3 +1,4 @@ +import typing from .command import Command, CommandArgs @@ -12,6 +13,11 @@ class Call: """Send a text message to the channel the call was made.""" raise NotImplementedError() + async def network(self, data): + """Send data to the rest of the Royalnet, and optionally wait for an answer. + The data must be pickleable.""" + raise NotImplementedError() + # These parameters / methods should be left alone def __init__(self, channel, command: Command, *args, **kwargs): self.channel = channel diff --git a/royalnet/utils/networkdict.py b/royalnet/utils/networkdict.py new file mode 100644 index 00000000..2711e1b2 --- /dev/null +++ b/royalnet/utils/networkdict.py @@ -0,0 +1,40 @@ +import uuid +import typing +from asyncio import Event + + +class RoyalnetData: + """A class to hold data to be sent to the Royalnet.""" + def __init__(self, data): + self.uuid = str(uuid.uuid4()) + self.request = data + self.event = Event() + self.response = None + + def send(self): + """TODO EVERYTHING""" + + + +class RoyalnetWait: + """A class that represents a data request sent to the Royalnet.""" + def __init__(self): + self.event = Event() + self.data = None + + def receive(self, data): + self.data = data + self.event.set() + + async def get(self): + await self.event.wait() + return self.data + + +class RoyalnetDict: + """A dictionary used to asyncrounosly hold data received from the Royalnet.""" + + def __init__(self): + self.dict: typing.Dict[str, RoyalnetRequest] = {} + + async def request(self, data: RoyalnetWait):