From e3ad073fadc701d9e551dbc28526d45dd55fd827 Mon Sep 17 00:00:00 2001 From: Ville Ranki Date: Sat, 28 Mar 2020 23:08:21 +0200 Subject: [PATCH] Added wolfram alpha module --- Pipfile | 1 + README.md | 25 ++++++++++++++++++++++++ modules/wa.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 modules/wa.py diff --git a/Pipfile b/Pipfile index 011fa96..665571c 100644 --- a/Pipfile +++ b/Pipfile @@ -16,6 +16,7 @@ igramscraper = "*" twitterscraper = "*" httpx = "*" pyyaml = "==5.3" +wolframalpha = "*" [dev-packages] pylint = "*" diff --git a/README.md b/README.md index 0c0f972..c30f493 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,15 @@ If you don't want some modules, just delete the files from modules directory. Support room: #hemppa:hacklab.fi - https://matrix.to/#/#hemppa:hacklab.fi +## Hint: RSS Bridge + +RSS Bridge is awesome project that creates RSS feeds for sites that don't have them: +https://github.com/RSS-Bridge/rss-bridge + +If you want bot to notify on new posts on a service, check out if RSS Bridge +supports it! You can use the stock Matrix RSS bot to subscribe to feeds created +by RSS bridge. + ## Module list ### Bot @@ -243,6 +252,22 @@ The module uses a demo API Key which can be replaced by your own api key by sett You can create one at https://api.nasa.gov/#signUp +### Wolfram Alpha + +Make queries to Wolfram Alpha + +You'll need to get an appid from https://products.wolframalpha.com/simple-api/documentation/ + +Examples: + +* !wa 1+1 +* !wa airspeed of unladen swallow + +Commands: + +* !wa [query] - Query wolfram alpha +* !wa appid [appid] - Set appid (must be done as bot owner) + ## Bot setup * Create a Matrix user diff --git a/modules/wa.py b/modules/wa.py new file mode 100644 index 0000000..dc3b09a --- /dev/null +++ b/modules/wa.py @@ -0,0 +1,53 @@ +import urllib.request +import wolframalpha + +from modules.common.module import BotModule + + +class MatrixModule(BotModule): + app_id = '' + + 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') + print('Appid', self.app_id) + 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) + answer = query + ': ' + try: + res = client.query(query) + for pod in res.results: + for sub in pod.subpods: + print(sub) + answer += str(sub.plaintext) + "\n" + except Exception as exc: + answer = str(exc) + + await bot.send_text(room, answer) + else: + await bot.send_text(room, 'Usage: !wa ') + + def get_settings(self): + data = super().get_settings() + data['app_id'] = self.app_id + return data + + def set_settings(self, data): + super().set_settings(data) + if data.get("app_id"): + self.app_id = data["app_id"] + + def help(self): + return ('Wolfram Alpha search')