Created
March 16, 2015 12:21
-
-
Save endofline/465d4e580757c0aa7970 to your computer and use it in GitHub Desktop.
Experimental Wolfram Alpha Plugin based on billius's original gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
simple "ask" function for wolfram alpha data | |
* pip3 install wolframalpha | |
* get API KEY from http://products.wolframalpha.com/developers/ | |
* put API KEY in config.json:wolframalpha-apikey | |
""" | |
import wolframalpha | |
_internal = {} | |
def _initialise(Handlers, bot): | |
apikey = bot.get_config_option("wolframalpha-apikey") | |
if apikey: | |
_internal["client"] = wolframalpha.Client(apikey) | |
Handlers.register_user_command(["ask"]) | |
else: | |
print("WOLFRAMALPHA: config.wolframalpha-apikey required") | |
return [] | |
def ask(bot, event, *args): | |
"""request data from wolfram alpha""" | |
keyword = ' '.join(args) | |
res = _internal["client"].query(keyword) | |
html = '<b>"{}"</b><br /><br />'.format(keyword) | |
has_content = False | |
for pod in res.pods: | |
if pod.title: | |
html += "<b>{}:</b> ".format(pod.title) | |
if pod.text: | |
html += pod.text.strip().replace("\n", "<br />") + "<br />" | |
has_content = True | |
else: | |
for node in pod.node.iter(): | |
if node.tag == "img": | |
html += '<a href="' + node.attrib["src"] + '">' + node.attrib["src"] + "</a><br />" | |
has_content = True | |
if not has_content: | |
html = "<i>Wolfram Alpha did not return any useful data</i>" | |
bot.send_html_to_conversation(event.conv, html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment