From 9b5e06a46c770288100d4749f293271616aacd67 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 3 Sep 2019 01:34:51 +0200 Subject: [PATCH] Add keyboard method! --- royalnet/bots/generic.py | 4 +- royalnet/bots/telegram.py | 50 +++++++++++-- royalnet/commands/commanddata.py | 9 +++ royalnet/commands/commandinterface.py | 6 ++ royalnet/commands/debug/__init__.py | 3 +- royalnet/commands/debug/debug_keyboard.py | 18 +++++ royalnet/commands/royalgames/smecds.py | 87 ++++++++++------------- royalnet/royalgames.py | 48 +++++++------ 8 files changed, 147 insertions(+), 78 deletions(-) create mode 100644 royalnet/commands/debug/debug_keyboard.py diff --git a/royalnet/bots/generic.py b/royalnet/bots/generic.py index 4de3288f..b6a75b67 100644 --- a/royalnet/bots/generic.py +++ b/royalnet/bots/generic.py @@ -132,10 +132,10 @@ class GenericBot: else: self.loop = loop if sentry_dsn: - log.debug("Sentry int") + log.debug("Sentry integration enabled") self.sentry = sentry_sdk.init(sentry_dsn) else: - log.debug("Sentry integration not enabled") + log.debug("Sentry integration disabled") try: if database_config is None: self.alchemy = None diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index 849ec85b..9a4621a4 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -1,6 +1,7 @@ import telegram import telegram.utils.request import typing +import uuid import asyncio import sentry_sdk import logging as _logging @@ -41,13 +42,23 @@ class TelegramBot(GenericBot): name = self.interface_name prefix = "/" + def __init__(self): + super().__init__() + self.keys_callbacks: typing.Dict[typing.Tuple[int, str], typing.Callable] = {} + + def register_keyboard_key(interface, key_name: str, callback: typing.Callable): + interface.keys_callbacks[key_name] = callback + + def unregister_keyboard_key(interface, key_name: str): + del interface.keys_callbacks[key_name] + return TelegramInterface def _data_factory(self) -> typing.Type[CommandData]: # noinspection PyMethodParameters,PyAbstractClass class TelegramData(CommandData): def __init__(data, interface: CommandInterface, update: telegram.Update): - data._interface = interface + data.interface = interface data.update = update async def reply(data, text: str): @@ -61,7 +72,7 @@ class TelegramBot(GenericBot): if error_if_none: raise UnregisteredError("No author for this message") return None - query = data._interface.session.query(self.master_table) + query = data.interface.session.query(self.master_table) for link in self.identity_chain: query = query.join(link.mapper.class_) query = query.filter(self.identity_column == user.id) @@ -70,6 +81,18 @@ class TelegramBot(GenericBot): raise UnregisteredError("Author is not registered") return result + async def keyboard(data, text: str, keyboard: typing.Dict[str, typing.Callable]) -> None: + tg_keyboard = [] + for key in keyboard: + press_id = uuid.uuid4() + tg_keyboard.append([telegram.InlineKeyboardButton(key, callback_data=str(press_id))]) + data.interface.register_keyboard_key(key_name=str(press_id), callback=keyboard[key]) + await asyncify(data.update.effective_chat.send_message, + telegram_escape(text), + reply_markup=telegram.InlineKeyboardMarkup(tg_keyboard), + parse_mode="HTML", + disable_web_page_preview=True) + return TelegramData def __init__(self, *, @@ -87,8 +110,12 @@ class TelegramBot(GenericBot): async def _handle_update(self, update: telegram.Update): # Skip non-message updates - if update.message is None: - return + if update.message is not None: + await self._handle_message(update) + elif update.callback_query is not None: + await self._handle_callback_query(update) + + async def _handle_message(self, update: telegram.Update): message: telegram.Message = update.message text: str = message.text # Try getting the caption instead @@ -122,6 +149,21 @@ class TelegramBot(GenericBot): error_message += '\n'.join(e.args) await data.reply(error_message) + async def _handle_callback_query(self, update: telegram.Update): + query: telegram.CallbackQuery = update.callback_query + source: telegram.Message = query.message + for command in self.commands.values(): + try: + callback = command.interface.keys_callbacks[query.data] + except KeyError: + continue + await callback(data=self._Data(interface=command.interface, update=update)) + await asyncify(query.answer) + break + else: + await asyncify(source.edit_reply_markup, reply_markup=None) + await asyncify(query.answer, text="⛔️ This keyboard has expired.") + async def run(self): while True: # Get the latest 100 updates diff --git a/royalnet/commands/commanddata.py b/royalnet/commands/commanddata.py index 6e323e2c..99f15ce4 100644 --- a/royalnet/commands/commanddata.py +++ b/royalnet/commands/commanddata.py @@ -1,3 +1,6 @@ +import typing + + class CommandData: async def reply(self, text: str) -> None: """Send a text message to the channel where the call was made. @@ -16,3 +19,9 @@ class CommandData: Raises: :py:exc:`royalnet.error.UnregisteredError` if ``error_if_none`` is set to True and no author is found.""" raise NotImplementedError() + + async def keyboard(self, text: str, keyboard: typing.Dict[str, typing.Callable]) -> None: + """Send a keyboard having the keys of the dict as keys and calling the correspondent values on a press. + + The function should be passed the :py:class:`CommandData` instance as a argument.""" + raise NotImplementedError() diff --git a/royalnet/commands/commandinterface.py b/royalnet/commands/commandinterface.py index 81e263ae..3d74dc9f 100644 --- a/royalnet/commands/commandinterface.py +++ b/royalnet/commands/commandinterface.py @@ -31,3 +31,9 @@ class CommandInterface: message: The data to be sent. Must be :py:mod:`pickle`-able. destination: The destination of the request, either in UUID format or node name.""" raise NotImplementedError() + + def register_keyboard_key(self, key_name: str, callback: typing.Callable): + raise NotImplementedError() + + def unregister_keyboard_key(self, key_name: str): + raise NotImplementedError() diff --git a/royalnet/commands/debug/__init__.py b/royalnet/commands/debug/__init__.py index f30b8a26..c657047f 100644 --- a/royalnet/commands/debug/__init__.py +++ b/royalnet/commands/debug/__init__.py @@ -1,3 +1,4 @@ from .debug_error import DebugErrorCommand +from .debug_keyboard import DebugKeyboardCommand -__all__ = ["DebugErrorCommand"] +__all__ = ["DebugErrorCommand", "DebugKeyboardCommand"] diff --git a/royalnet/commands/debug/debug_keyboard.py b/royalnet/commands/debug/debug_keyboard.py new file mode 100644 index 00000000..446234fb --- /dev/null +++ b/royalnet/commands/debug/debug_keyboard.py @@ -0,0 +1,18 @@ +import typing +from ..command import Command +from ..commandargs import CommandArgs +from ..commanddata import CommandData + + +class DebugKeyboardCommand(Command): + name: str = "debug_keyboard" + + description: str = "Invia una tastiera di prova." + + async def _callback(self, data: CommandData): + await data.reply("OK.") + + async def run(self, args: CommandArgs, data: CommandData) -> None: + await data.keyboard("This is a keyboard.", { + "✅ OK": self._callback + }) diff --git a/royalnet/commands/royalgames/smecds.py b/royalnet/commands/royalgames/smecds.py index abac8a90..16da1ff7 100644 --- a/royalnet/commands/royalgames/smecds.py +++ b/royalnet/commands/royalgames/smecds.py @@ -16,39 +16,32 @@ class SmecdsCommand(Command): DS_LIST = ["della secca", "del seccatore", "del secchiello", "del secchio", "del secchione", "del secondino", "del sedano", "del sedativo", "della sedia", "del sedicente", "del sedile", "della sega", "del segale", "della segatura", "della seggiola", "del seggiolino", "della seggiovia", "della segheria", - "del seghetto", - "del segnalibro", "del segnaposto", "del segno", "del segretario", "della segreteria", "del seguace", - "del segugio", "della selce", "della sella", "della selz", "della selva", "della selvaggina", - "del semaforo", - "del seme", "del semifreddo", "del seminario", "della seminarista", "della semola", "del semolino", - "del semplicione", "della senape", "del senatore", "del seno", "del sensore", "della sentenza", - "della sentinella", "del sentore", "della seppia", "del sequestratore", "della serenata", "del sergente", - "del sermone", "della serpe", "del serpente", "della serpentina", "della serra", "del serraglio", - "del serramanico", "della serranda", "della serratura", "del servitore", "della servitù", - "del servizievole", - "del servo", "del set", "della seta", "della setola", "del sidecar", "del siderurgico", "del sidro", - "della siepe", "del sifone", "della sigaretta", "del sigaro", "del sigillo", "della signora", - "della signorina", "del silenziatore", "della silhouette", "del silicio", "del silicone", "del siluro", - "della sinagoga", "della sindacalista", "del sindacato", "del sindaco", "della sindrome", - "della sinfonia", - "del sipario", "del sire", "della sirena", "della siringa", "del sismografo", "del sobborgo", - "del sobillatore", "del sobrio", "del soccorritore", "del socio", "del sociologo", "della soda", - "del sofà", - "della soffitta", "del software", "dello sogghignare", "del soggiorno", "della sogliola", - "del sognatore", - "della soia", "del solaio", "del solco", "del soldato", "del soldo", "del sole", "della soletta", - "della solista", "del solitario", "del sollazzare", "del sollazzo", "del sollecito", "del solleone", - "del solletico", "del sollevare", "del sollievo", "del solstizio", "del solubile", "del solvente", - "della soluzione", "del somaro", "del sombrero", "del sommergibile", "del sommo", "della sommossa", - "del sommozzatore", "del sonar", "della sonda", "del sondaggio", "del sondare", "del sonnacchioso", - "del sonnambulo", "del sonnellino", "del sonnifero", "del sonno", "della sonnolenza", "del sontuoso", - "del soppalco", "del soprabito", "del sopracciglio", "del sopraffare", "del sopraffino", - "del sopraluogo", - "del sopramobile", "del soprannome", "del soprano", "del soprappensiero", "del soprassalto", - "del soprassedere", "del sopravvento", "del sopravvivere", "del soqquadro", "del sorbetto", - "del sordido", - "della sordina", "del sordo", "della sorella", "della sorgente", "del sornione", "del sorpasso", - "della sorpresa", "del sorreggere", "del sorridere", "della sorsata", "del sorteggio", "del sortilegio", + "del seghetto", "del segnalibro", "del segnaposto", "del segno", "del segretario", "della segreteria", + "del seguace", "del segugio", "della selce", "della sella", "della selz", "della selva", + "della selvaggina", "del semaforo", "del seme", "del semifreddo", "del seminario", "della seminarista", + "della semola", "del semolino", "del semplicione", "della senape", "del senatore", "del seno", + "del sensore", "della sentenza", "della sentinella", "del sentore", "della seppia", "del sequestratore", + "della serenata", "del sergente", "del sermone", "della serpe", "del serpente", "della serpentina", + "della serra", "del serraglio", "del serramanico", "della serranda", "della serratura", "del servitore", + "della servitù", "del servizievole", "del servo", "del set", "della seta", "della setola", "del sigaro", + "del sidecar", "del siderurgico", "del sidro", "della siepe", "del sifone", "della sigaretta", + "del sigillo", "della signora", "della signorina", "del silenziatore", "della silhouette", "del silicio", + "del silicone", "del siluro", "della sinagoga", "della sindacalista", "del sindacato", "del sindaco", + "della sindrome", "della sinfonia", "del sipario", "del sire", "della sirena", "della siringa", + "del sismografo", "del sobborgo", "del sobillatore", "del sobrio", "del soccorritore", "del socio", + "del sociologo", "della soda", "del sofà", "della soffitta", "del software", "dello sogghignare", + "del soggiorno", "della sogliola", "del sognatore", "della soia", "del solaio", "del solco", + "del soldato", "del soldo", "del sole", "della soletta", "della solista", "del solitario", + "del sollazzare", "del sollazzo", "del sollecito", "del solleone", "del solletico", "del sollevare", + "del sollievo", "del solstizio", "del solubile", "del solvente", "della soluzione", "del somaro", + "del sombrero", "del sommergibile", "del sommo", "della sommossa", "del sommozzatore", "del sonar", + "della sonda", "del sondaggio", "del sondare", "del sonnacchioso", "del sonnambulo", "del sonnellino", + "del sonnifero", "del sonno", "della sonnolenza", "del sontuoso", "del soppalco", "del soprabito", + "del sopracciglio", "del sopraffare", "del sopraffino", "del sopraluogo", "del sopramobile", + "del soprannome", "del soprano", "del soprappensiero", "del soprassalto", "del soprassedere", + "del sopravvento", "del sopravvivere", "del soqquadro", "del sorbetto", "del sordido", "della sordina", + "del sordo", "della sorella", "della sorgente", "del sornione", "del sorpasso", "della sorpresa", + "del sorreggere", "del sorridere", "della sorsata", "del sorteggio", "del sortilegio", "del sorvegliante", "del sorvolare", "del sosia", "del sospettoso", "del sospirare", "della sosta", "della sostanza", "del sostegno", "del sostenitore", "del sostituto", "del sottaceto", "della sottana", "del sotterfugio", "del sotterraneo", "del sottile", "del sottilizzare", "del sottintendere", @@ -56,22 +49,18 @@ class SmecdsCommand(Command): "del sottoscala", "della sottoscrizione", "del sottostare", "del sottosuolo", "del sottotetto", "del sottotitolo", "del sottovalutare", "del sottovaso", "della sottoveste", "del sottovuoto", "del sottufficiale", "della soubrette", "del souvenir", "del soverchiare", "del sovrano", - "del sovrapprezzo", - "della sovvenzione", "del sovversivo", "del sozzo", "dello suadente", "del sub", "del subalterno", - "del subbuglio", "del subdolo", "del sublime", "del suburbano", "del successore", "del succo", - "della succube", "del succulento", "della succursale", "del sudario", "della sudditanza", "del suddito", - "del sudicio", "del suffisso", "del suffragio", "del suffumigio", "del suggeritore", "del sughero", - "del sugo", "del suino", "della suite", "del sulfureo", "del sultano", "di Steffo", "di Spaggia", - "di Sabrina", "del sas", "del ses", "del sis", "del sos", "del sus", "della supremazia", - "del Santissimo", - "della scatola", "del supercalifragilistichespiralidoso", "del sale", "del salame", "di (Town of) Salem", - "di Stronghold", "di SOMA", "dei Saints", "di S.T.A.L.K.E.R.", "di Sanctum", "dei Sims", "di Sid", - "delle Skullgirls", "di Sonic", "di Spiral (Knights)", "di Spore", "di Starbound", "di SimCity", - "di Sensei", - "di Ssssssssssssss... Boom! E' esploso il dizionario", "della scala", "di Sakura", "di Suzie", - "di Shinji", - "del senpai", "del support", "di Superman", "di Sekiro", "dello Slime God", "del salassato", - "della salsa"] + "del sovrapprezzo", "della sovvenzione", "del sovversivo", "del sozzo", "dello suadente", "del sub", + "del subalterno", "del subbuglio", "del subdolo", "del sublime", "del suburbano", "del successore", + "del succo", "della succube", "del succulento", "della succursale", "del sudario", "della sudditanza", + "del suddito", "del sudicio", "del suffisso", "del suffragio", "del suffumigio", "del suggeritore", + "del sughero", "del sugo", "del suino", "della suite", "del sulfureo", "del sultano", "di Steffo", + "di Spaggia", "di Sabrina", "del sas", "del ses", "del sis", "del sos", "del sus", "della supremazia", + "del Santissimo", "della scatola", "del supercalifragilistichespiralidoso", "del sale", "del salame", + "di (Town of) Salem", "di Stronghold", "di SOMA", "dei Saints", "di S.T.A.L.K.E.R.", "di Sanctum", + "dei Sims", "di Sid", "delle Skullgirls", "di Sonic", "di Spiral (Knights)", "di Spore", "di Starbound", + "di SimCity", "di Sensei", "di Ssssssssssssss... Boom! E' esploso il dizionario", "della scala", + "di Sakura", "di Suzie", "di Shinji", "del senpai", "del support", "di Superman", "di Sekiro", + "dello Slime God", "del salassato", "della salsa"] SMECDS = "🤔 Secondo me, è colpa {ds}." async def run(self, args: CommandArgs, data: CommandData) -> None: diff --git a/royalnet/royalgames.py b/royalnet/royalgames.py index 7fde45d9..030c7448 100644 --- a/royalnet/royalgames.py +++ b/royalnet/royalgames.py @@ -18,35 +18,39 @@ stream_handler = logging.StreamHandler() stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") log.addHandler(stream_handler) -commands = [ - CiaoruoziCommand, - ColorCommand, - CvCommand, - DiarioCommand, - Mp3Command, - PauseCommand, - PingCommand, - PlayCommand, - PlaymodeCommand, - QueueCommand, - RageCommand, - ReminderCommand, - ShipCommand, - SkipCommand, - SmecdsCommand, - SummonCommand, - VideochannelCommand, - DnditemCommand, - DndspellCommand -] + sentry_dsn = os.environ.get("SENTRY_DSN") # noinspection PyUnreachableCode if __debug__: + commands = [ + DebugErrorCommand, + DebugKeyboardCommand + ] log.setLevel(logging.DEBUG) - commands = [*commands, DebugErrorCommand] else: + commands = [ + CiaoruoziCommand, + ColorCommand, + CvCommand, + DiarioCommand, + Mp3Command, + PauseCommand, + PingCommand, + PlayCommand, + PlaymodeCommand, + QueueCommand, + RageCommand, + ReminderCommand, + ShipCommand, + SkipCommand, + SmecdsCommand, + SummonCommand, + VideochannelCommand, + DnditemCommand, + DndspellCommand + ] log.setLevel(logging.INFO) address, port = "127.0.0.1", 1234