Rewrite wa:
- Work around upstream bug when result has only one pod - Add !wafull alias to output all result
This commit is contained in:
parent
dd95c7f0ad
commit
b6e10928f0
|
@ -7,6 +7,10 @@ from modules.common.module import BotModule
|
||||||
class MatrixModule(BotModule):
|
class MatrixModule(BotModule):
|
||||||
app_id = ''
|
app_id = ''
|
||||||
|
|
||||||
|
def matrix_start(self, bot):
|
||||||
|
super().matrix_start(bot)
|
||||||
|
self.add_module_aliases(bot, ['wafull'])
|
||||||
|
|
||||||
async def matrix_message(self, bot, room, event):
|
async def matrix_message(self, bot, room, event):
|
||||||
args = event.body.split()
|
args = event.body.split()
|
||||||
if len(args) == 3:
|
if len(args) == 3:
|
||||||
|
@ -25,18 +29,21 @@ class MatrixModule(BotModule):
|
||||||
|
|
||||||
query = event.body[len(args[0])+1:]
|
query = event.body[len(args[0])+1:]
|
||||||
client = wolframalpha.Client(self.app_id)
|
client = wolframalpha.Client(self.app_id)
|
||||||
answer = query + ': '
|
|
||||||
try:
|
|
||||||
res = client.query(query)
|
res = client.query(query)
|
||||||
result = "?SYNTAX ERROR"
|
result = "?SYNTAX ERROR"
|
||||||
if res['@success']=='true':
|
if res['@success']:
|
||||||
pod0=res['pod'][0]['subpod']['plaintext']
|
self.logger.debug(f"room: {room.name} sender: {event.sender} sent a valid query to wa")
|
||||||
pod1=res['pod'][1]
|
else:
|
||||||
if (('definition' in pod1['@title'].lower()) or ('result' in pod1['@title'].lower()) or (pod1.get('@primary','false') == 'true')):
|
self.logger.info(f"wa error: {res['@error']}")
|
||||||
result = pod1['subpod']['plaintext']
|
primary, items, fallback = self.parse_api_response(res)
|
||||||
answer += result + "\n"
|
if len(items) and 'full' in args[0]:
|
||||||
except Exception as exc:
|
answer = '\n'.join(items)
|
||||||
answer = "Wolfram Alpha has technical difficulty: " + str(exc)
|
elif primary:
|
||||||
|
answer = query + ': ' + primary
|
||||||
|
elif fallback:
|
||||||
|
answer = query + ': ' + fallback
|
||||||
|
else:
|
||||||
|
answer = 'Could not find response for ' + query
|
||||||
|
|
||||||
await bot.send_text(room, answer)
|
await bot.send_text(room, answer)
|
||||||
else:
|
else:
|
||||||
|
@ -52,5 +59,27 @@ class MatrixModule(BotModule):
|
||||||
if data.get("app_id"):
|
if data.get("app_id"):
|
||||||
self.app_id = data["app_id"]
|
self.app_id = data["app_id"]
|
||||||
|
|
||||||
|
def parse_api_response(self, res, key='plaintext'):
|
||||||
|
fallback = None
|
||||||
|
primary = None
|
||||||
|
items = list()
|
||||||
|
# workaround for bug in upstream wa package
|
||||||
|
if hasattr(res['pod'], 'get'):
|
||||||
|
res['pod'] = [res['pod']]
|
||||||
|
for pod in res['pod']:
|
||||||
|
title = pod['@title'].lower()
|
||||||
|
if 'input' in title:
|
||||||
|
continue
|
||||||
|
for sub in pod.subpods:
|
||||||
|
print(sub)
|
||||||
|
item = sub.get(key)
|
||||||
|
if not item:
|
||||||
|
continue
|
||||||
|
items.append(item)
|
||||||
|
fallback = fallback or item
|
||||||
|
if ('definition' in title) or ('result' in title) or pod.get('@primary'):
|
||||||
|
primary = primary or item
|
||||||
|
return (primary, items, fallback)
|
||||||
|
|
||||||
def help(self):
|
def help(self):
|
||||||
return ('Wolfram Alpha search')
|
return ('Wolfram Alpha search')
|
||||||
|
|
Loading…
Reference in New Issue