2020-11-06 00:42:38 +01:00
|
|
|
try:
|
|
|
|
import wolframalpha
|
2022-08-09 23:47:28 +02:00
|
|
|
|
2020-11-06 00:42:38 +01:00
|
|
|
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)
|
2021-06-06 18:28:43 +02:00
|
|
|
self.priority = -6
|
2020-11-06 00:42:38 +01:00
|
|
|
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"]:
|
2020-11-09 15:50:26 +01:00
|
|
|
return False
|
2020-11-06 00:42:38 +01:00
|
|
|
|
|
|
|
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)
|
2021-12-02 12:22:43 +01:00
|
|
|
except Exception as exc: # unstable lib
|
|
|
|
self.bot.log_w(f"wolframalpha exception: {exc}")
|
2020-11-06 00:42:38 +01:00
|
|
|
self.signal_failure(target)
|
|
|
|
return
|
|
|
|
|
2021-12-02 11:57:06 +01:00
|
|
|
if not response["@success"]:
|
2021-12-02 12:22:43 +01:00
|
|
|
self.bot.log_d("Call to WA succeeded but response is an error.")
|
2020-11-06 00:42:38 +01:00
|
|
|
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):
|
2022-08-09 23:47:28 +02:00
|
|
|
reply = (
|
|
|
|
reply[: self.MAX_LENGTH - len(self.CUT_MARK)] + self.CUT_MARK
|
|
|
|
)
|
2020-11-06 00:42:38 +01:00
|
|
|
self.bot.say(target, reply)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def sanitize_text(text):
|
|
|
|
return text.replace("\n", ", ")
|