def process( self, statement: SugaroidStatement, additional_response_selection_parameters=None, ): # https://github.com/pratishrai/doraemon/blob/302a78f8ace4b4675f3cd293dce101ea448b3e13/cogs/fun.py#L1 try: response2 = requests.get( "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit" ).json() try: joke = response2["joke"] except KeyError: setup = response2["setup"] delivery = response2["delivery"] if self.chatbot.globals["rich"]: joke = f"{setup}\n\n<i>{delivery}</i>" else: joke = f"{setup}\n\n{delivery}" except Exception as e: joke = f"I think I am a joke sometimes.. {e}" selected_statement = SugaroidStatement(joke, chatbot=True) selected_statement.set_confidence(1) selected_statement.set_emotion(Emotion.lol) return selected_statement
def process( self, statement: SugaroidStatement, additional_response_selection_parameters=None, ): wolf_command = False user_requests_text = False supports_media = self.chatbot.globals["media"] rich_text = self.chatbot.globals["rich"] if "$wolf" in statement.simple: # this is a command type wolfram alpha request wolf_command = True statement.simple.remove("$wolf") if "$text" in statement.simple: user_requests_text = True statement.simple.remove("$text") url = ("https://api.wolframalpha.com/v2/query?" "input={query}" "&format={format}&output=JSON&appid={appid}") url = url.format( query="+".join(statement.simple), appid=os.getenv("WOLFRAM_ALPHA_API", "DEMO"), format="image,plaintext" if supports_media else "plaintext", ) sugaroid_logger.info(f"WolframAlpha endpoint: {url}") response = requests.get(url, headers={ "Accept": "application/json" }).json() if not response["queryresult"]["success"]: confidence = 0.3 try: text = response["queryresult"]["tips"]["text"] except KeyError: text = "Wolfram Alpha didnt send back a response" confidence = 0 if wolf_command: confidence = 1 selected_statement = SugaroidStatement(text, chatbot=True) selected_statement.confidence = confidence selected_statement.emotion = Emotion.positive return selected_statement information = [] for i in response["queryresult"]["pods"]: for j in i["subpods"]: if j["plaintext"]: plaintext_answer = j["plaintext"].split("\n") for ans in plaintext_answer: splitted_ans = ans.split("|") sugaroid_logger.info("splitted_ans") if len(splitted_ans) == 1: front = splitted_ans[0] back, rest = "", "" elif len(splitted_ans) == 2: front, back = splitted_ans rest = "" else: front, back, rest = ( splitted_ans[0], splitted_ans[1], splitted_ans[2:], ) if rich_text: if not back: information.append(f"<b>{front}</b>") else: information.append( f"<b>{front}</b>: {back} {' '.join(rest)}") else: if not back: information.append(f"{front}") else: information.append( f"{front}: {back} {' '.join(rest)}") if supports_media: information.append("<sugaroid:br>") for i in response["queryresult"]["pods"]: for j in i["subpods"]: if not j.get("plaintext") and j.get( "img") and j["img"].get("src"): information.append( f'<sugaroid:img>{j["img"]["src"]}<sugaroid:br>') information.append( "Results powered by Wolfram|Alpha (wolframalpha.com)") interpretation = "\n".join(information) selected_statement = SugaroidStatement(interpretation, chatbot=True) selected_statement.set_confidence(1) selected_statement.set_emotion(Emotion.lol) return selected_statement