1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 11:34:18 +00:00

Unfinished network stuff

This commit is contained in:
Steffo 2019-03-15 16:46:47 +01:00
parent d42c1a6cd7
commit 3d01e4a984
3 changed files with 63 additions and 8 deletions

View file

@ -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):

View file

@ -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

View file

@ -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):