import urllib.request
import wolframalpha
from html import escape
import json
from modules.common.module import BotModule
class MatrixModule(BotModule):
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):
args = event.body.split()
if len(args) == 3:
if args[1] == "appid":
bot.must_be_owner(event)
self.app_id = args[2]
bot.save_settings()
await bot.send_text(room, 'App id set')
return
if len(args) > 1:
if self.app_id == '':
await bot.send_text(room, 'Please get and set a appid: https://products.wolframalpha.com/simple-api/documentation/')
return
query = event.body[len(args[0])+1:]
client = wolframalpha.Client(self.app_id)
res = client.query(query)
result = "?SYNTAX ERROR"
if res['@success']:
self.logger.debug(f"room: {room.name} sender: {event.sender} sent a valid query to wa")
else:
self.logger.info(f"wa error: {res['@error']}")
short, full = self.parse_api_response(res)
if full[0] and 'full' in args[0]:
html, plain = full
elif short[0]:
html, plain = short
else:
plain = 'Could not find response for ' + query
html = plain
await bot.send_html(room, html, plain)
else:
await bot.send_text(room, 'Usage: !wa {escape(title)}\n']
+ [f'