diff --git a/.gitignore b/.gitignore index 66f8fb50..2aeb627a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ .vscode/ +__pycache__ +test.py diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index c7748592..153c19b8 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -1,25 +1,28 @@ import telegram import asyncio import typing -from ..commands import PingCommand -from ..utils import asyncify, Call +from ..commands import NullCommand +from ..utils import asyncify, Call, Command class TelegramBot: - def __init__(self, api_key: str): + def __init__(self, api_key: str, commands: typing.List[Command], *, missing_command: Command=NullCommand): self.bot = telegram.Bot(api_key) self.should_run = False self.offset = -100 - self.commands = { - "/ping": PingCommand - } + self.commands = commands + self.missing_command: typing.Callable = missing_command + # Generate commands + self._commands = {} + for command in self.commands: + self._commands[f"/{command.command_name}"] = command class TelegramCall(Call): interface_name = "telegram" interface_obj = self - async def reply(call, text: str): - await asyncify(call.channel.send_message, text, parse_mode="HTML") + async def reply(self, text: str): + await asyncify(self.channel.send_message, text, parse_mode="HTML") self.Call = TelegramCall async def run(self): @@ -53,9 +56,9 @@ class TelegramBot: command_text.replace(f"@{self.bot.username}", "") # Find the function try: - command = self.commands[command_text] + command = self._commands[command_text] except KeyError: # Skip inexistent commands - return + command = self.missing_command # Call the command - return await self.Call(message.chat, command).run() + return await self.Call(message.chat, command, parameters).run() diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 55d2e0d7..0fc0550e 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -1,3 +1,4 @@ +from .null import NullCommand from .ping import PingCommand -__all__ = ["PingCommand"] +__all__ = ["NullCommand", "PingCommand"] diff --git a/royalnet/commands/null.py b/royalnet/commands/null.py new file mode 100644 index 00000000..4503ef16 --- /dev/null +++ b/royalnet/commands/null.py @@ -0,0 +1,10 @@ +from ..utils import Command, Call + + +class NullCommand(Command): + + command_name = "null" + command_title = "Do nothing" + + async def common(self, call: Call, *args, **kwargs): + pass diff --git a/royalnet/commands/ping.py b/royalnet/commands/ping.py index 432598dd..517f9512 100644 --- a/royalnet/commands/ping.py +++ b/royalnet/commands/ping.py @@ -2,5 +2,9 @@ from ..utils import Command, Call class PingCommand(Command): + + command_name = "ping" + command_title = "Ping pong!" + async def common(self, call: Call, *args, **kwargs): await call.reply("Pong!") diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index 2cbd624f..045ea4fb 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -1,3 +1,4 @@ +import typing from .command import Command @@ -8,14 +9,18 @@ class Call: interface_name = NotImplemented interface_obj = NotImplemented - async def reply(cls, text: str): + async def reply(self, text: str): """Send a text message to the channel the call was made.""" raise NotImplementedError() # These parameters / methods should be left alone - def __init__(self, channel, command: Command): + def __init__(self, channel, command: Command, parameters: typing.List[str]=None): self.channel = channel self.command = command + if parameters is None: + self.parameters = [] + else: + self.parameters = parameters async def run(self, *args, **kwargs): try: diff --git a/royalnet/utils/command.py b/royalnet/utils/command.py index bc1c1ff0..3bb44155 100644 --- a/royalnet/utils/command.py +++ b/royalnet/utils/command.py @@ -6,5 +6,8 @@ if typing.TYPE_CHECKING: class Command: """A generic command, called from any source.""" + command_name: str = NotImplemented + command_title: str = NotImplemented + async def common(self, call: "Call", *args, **kwargs): raise NotImplementedError()