Have dragon bot fall back to wikipedia if wolfram is unable to answer the question

This commit is contained in:
luke 2018-03-09 11:18:31 -08:00
parent 4fec8ea6df
commit 776d34cca1
4 changed files with 30 additions and 21 deletions

View File

@ -20,11 +20,11 @@ import excuse
import get_from_reddit
import help_methods
import lewds
import questions
import role_check
import stock
import wallpaper
import weather
import wolfram
import stock
# Client object
client = discord.Client()
@ -71,7 +71,7 @@ async def on_message(message):
message.channel,
"{} ```{}```".format(
message.author.mention,
wolfram.answer_question(message.content)
questions.answer_question(message.content)
)
)

26
app/questions.py Normal file
View File

@ -0,0 +1,26 @@
import wikipedia
import wolframalpha
import help_methods
def answer_question(message):
"""
answer_question(question)
Submits a request to the wolfram API and returns the response
If no answer is found, tries wikipedia. If that fails, apologizes
"""
if len(message.split()) > 1:
client = wolframalpha.Client('2LU2Y7-YJQTA7TL8E')
question = ' '.join(message.split()[1:])
try:
res = client.query(question)
return next(res.results).text
except Exception:
try:
return wikipedia.summary(question, sentences=5)
except Exception:
return "Sorry, I\'m unable to answer that"
return help_methods.get_help_message('message')

View File

@ -4,5 +4,6 @@ docker
pylint
pyowm
requests
wikipedia
wolframalpha
yahoo-finance

View File

@ -1,18 +0,0 @@
import wolframalpha
import help_methods
def answer_question(message):
"""
answer_question(question)
submits a request to the wolfram API and returns the response
"""
if len(message.split()) > 1:
client = wolframalpha.Client('2LU2Y7-YJQTA7TL8E')
question = ' '.join(message.split()[1:])
res = client.query(question)
return next(res.results).text
return help_methods.get_help_message('message')