From 3fd0cecf6335507f0280c2907fadbe1b065a2d0e Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 6 Nov 2020 00:42:38 +0100 Subject: [PATCH] unknown_command: add plugin (WolframAlpha) --- config.json.example | 4 ++ edmond/plugins/requirements.txt | 3 ++ edmond/plugins/unknown_command.py | 85 +++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 edmond/plugins/unknown_command.py diff --git a/config.json.example b/config.json.example index 563cf4d..7025175 100644 --- a/config.json.example +++ b/config.json.example @@ -119,6 +119,10 @@ "param_source": "from", "param_dest": "to" }, + "unknowncommand": { + "api_key": "", + "max_pods": 1 + }, "unknownquestion": { "pass_to_misc_rate": 50 }, diff --git a/edmond/plugins/requirements.txt b/edmond/plugins/requirements.txt index 50c6209..81f4faf 100644 --- a/edmond/plugins/requirements.txt +++ b/edmond/plugins/requirements.txt @@ -12,3 +12,6 @@ translate # Music scaruffi==0.0.2 + +# WolframAlpha +wolframalpha diff --git a/edmond/plugins/unknown_command.py b/edmond/plugins/unknown_command.py new file mode 100644 index 0000000..454353b --- /dev/null +++ b/edmond/plugins/unknown_command.py @@ -0,0 +1,85 @@ +try: + import wolframalpha + DEPENDENCIES_FOUND = True +except ImportError: + DEPENDENCIES_FOUND = False + +from edmond.plugin import Plugin + + +class UnknownCommandPlugin(Plugin): + """Handle unknown command by sending it to WolframAlpha.""" + + REQUIRED_CONFIGS = ["api_key", "max_pods"] + MAX_LENGTH = 256 + CUT_MARK = "..." + + def __init__(self, bot): + super().__init__(bot) + self.priority = -9 + self._client = None + + @property + def client(self): + if self._client is None and self.has_api_key(): + self._client = wolframalpha.Client(self.config["api_key"]) + return self._client + + def has_api_key(self): + return self.config["api_key"] != "" + + def on_pubmsg(self, event): + if not self.has_api_key: + return False + message = self.should_read_message(event.arguments[0]) + if not message: + return False + words = message.split() + if len(words) == 0 or words[-1] != self.config["command_suffix"]: + return False + + query = " ".join(words[:-1]) + self.process_query(query, event.target) + return True + + def process_query(self, query, target): + self.bot.log_d(f"Processing '{query}' with WolframAlpha.") + try: + response = self.client.query(query) + except: # Capture any exception as this lib is not very stable. + self.signal_failure(target) + return + + if response["@success"] != "true": + self.signal_failure(target) + return + + inputs = [] + answers = [] + num_pods = 0 + for pod in response.pods: + for subpod in pod.subpods: + self.bot.log_d(f"WolframAlpha subpod: {subpod}") + if pod["@id"] == "Input": + inputs.append(self.sanitize_text(subpod.plaintext or "")) + else: + answers.append(self.sanitize_text(subpod.plaintext or "")) + num_pods += 1 + if num_pods >= self.config["max_pods"]: + break + if num_pods >= self.config["max_pods"]: + break + + input_text = ", ".join(inputs) + answer_text = ", ".join(answers) + if input_text: + reply = input_text + " -- " + answer_text + else: + reply = answer_text + if len(reply) > self.MAX_LENGTH - len(self.CUT_MARK): + reply = reply[:self.MAX_LENGTH - len(self.CUT_MARK)] + self.CUT_MARK + self.bot.say(target, reply) + + @staticmethod + def sanitize_text(text): + return text.replace("\n", ", ")