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 import multiprocessing
from ..commands import NullCommand from ..commands import NullCommand
from ..utils import asyncify, Call, Command from ..utils import asyncify, Call, Command
from ..network import RoyalnetLink, Message
async def null(message: Message):
pass
class TelegramBot: class TelegramBot:
def __init__(self, def __init__(self,
api_key: str, api_key: str,
*, master_server_uri: str,
commands: typing.List[typing.Type[Command]], commands: typing.List[typing.Type[Command]],
missing_command: Command = NullCommand, missing_command: Command = NullCommand):
network: multiprocessing.connection.Connection):
self.bot: telegram.Bot = telegram.Bot(api_key) self.bot: telegram.Bot = telegram.Bot(api_key)
self.should_run: bool = False self.should_run: bool = False
self.offset: int = -100 self.offset: int = -100
self.missing_command: typing.Callable = missing_command self.missing_command: typing.Callable = missing_command
self.network: multiprocessing.connection.Connection = network self.network: RoyalnetLink = RoyalnetLink(master_server_uri, "Telegram", null)
# Generate commands # Generate commands
self.commands = {} self.commands = {}
for command in commands: for command in commands:
@ -30,8 +34,10 @@ 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): async def net_request(self, message: Message, destination: str):
self.network.send response = await self.network.request(message, destination)
return response
self.Call = TelegramCall self.Call = TelegramCall
async def run(self): 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 functools
import typing import typing
import pickle import pickle
from .messages import Message, IdentifyMessage, ErrorMessage from .messages import Message, ErrorMessage
from .packages import Package, TwoWayPackage from .packages import Package, TwoWayPackage
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -35,8 +35,9 @@ class PendingRequest:
class RoyalnetLink: 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.master_uri: str = master_uri
self.link_type: str = link_type
self.nid: str = str(uuid.uuid4()) self.nid: str = str(uuid.uuid4())
self.websocket: typing.Optional[websockets.WebSocketClientProtocol] = None self.websocket: typing.Optional[websockets.WebSocketClientProtocol] = None
self.identified: bool = False self.identified: bool = False

View file

@ -1,4 +1,5 @@
import typing import typing
from ..network.messages import Message
from .command import Command, CommandArgs from .command import Command, CommandArgs
@ -13,7 +14,7 @@ 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): async def net_request(self, message: Message, destination: str):
"""Send data to the rest of the Royalnet, and optionally wait for an answer. """Send data to the rest of the Royalnet, and optionally wait for an answer.
The data must be pickleable.""" The data must be pickleable."""
raise NotImplementedError() raise NotImplementedError()