mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Unfinished network stuff
This commit is contained in:
parent
d42c1a6cd7
commit
3d01e4a984
3 changed files with 63 additions and 8 deletions
|
@ -1,21 +1,27 @@
|
||||||
import telegram
|
import telegram
|
||||||
import asyncio
|
import asyncio
|
||||||
import typing
|
import typing
|
||||||
|
import multiprocessing
|
||||||
from ..commands import NullCommand
|
from ..commands import NullCommand
|
||||||
from ..utils import asyncify, Call, Command
|
from ..utils import asyncify, Call, Command
|
||||||
|
|
||||||
|
|
||||||
class TelegramBot:
|
class TelegramBot:
|
||||||
def __init__(self, api_key: str, commands: typing.List[typing.Type[Command]], *, missing_command: Command=NullCommand):
|
def __init__(self,
|
||||||
self.bot = telegram.Bot(api_key)
|
api_key: str,
|
||||||
self.should_run = False
|
*,
|
||||||
self.offset = -100
|
commands: typing.List[typing.Type[Command]],
|
||||||
self.commands = commands
|
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.missing_command: typing.Callable = missing_command
|
||||||
|
self.network: multiprocessing.connection.Connection = network
|
||||||
# Generate commands
|
# Generate commands
|
||||||
self._commands = {}
|
self.commands = {}
|
||||||
for command in self.commands:
|
for command in commands:
|
||||||
self._commands[f"/{command.command_name}"] = command
|
self.commands[f"/{command.command_name}"] = command
|
||||||
|
|
||||||
class TelegramCall(Call):
|
class TelegramCall(Call):
|
||||||
interface_name = "telegram"
|
interface_name = "telegram"
|
||||||
|
@ -23,6 +29,9 @@ class TelegramBot:
|
||||||
|
|
||||||
async def reply(self, text: str):
|
async def reply(self, text: str):
|
||||||
await asyncify(self.channel.send_message, text, parse_mode="HTML")
|
await asyncify(self.channel.send_message, text, parse_mode="HTML")
|
||||||
|
|
||||||
|
async def network(self, data):
|
||||||
|
self.network.send
|
||||||
self.Call = TelegramCall
|
self.Call = TelegramCall
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import typing
|
||||||
from .command import Command, CommandArgs
|
from .command import Command, CommandArgs
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,6 +13,11 @@ class Call:
|
||||||
"""Send a text message to the channel the call was made."""
|
"""Send a text message to the channel the call was made."""
|
||||||
raise NotImplementedError()
|
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
|
# These parameters / methods should be left alone
|
||||||
def __init__(self, channel, command: Command, *args, **kwargs):
|
def __init__(self, channel, command: Command, *args, **kwargs):
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
|
|
40
royalnet/utils/networkdict.py
Normal file
40
royalnet/utils/networkdict.py
Normal 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):
|
Loading…
Reference in a new issue