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

Implement RoyalnetLink on TelegramBot

This commit is contained in:
Steffo 2019-03-18 09:40:45 +01:00
parent 94d0c21ff2
commit 2b2432b4d5
4 changed files with 23 additions and 9 deletions

View file

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

View file

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

View file

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

View file

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