Added wolfram alpha module
This commit is contained in:
parent
3eeadb8956
commit
e3ad073fad
1
Pipfile
1
Pipfile
|
@ -16,6 +16,7 @@ igramscraper = "*"
|
|||
twitterscraper = "*"
|
||||
httpx = "*"
|
||||
pyyaml = "==5.3"
|
||||
wolframalpha = "*"
|
||||
|
||||
[dev-packages]
|
||||
pylint = "*"
|
||||
|
|
25
README.md
25
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
|
||||
|
|
|
@ -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 <query>')
|
||||
|
||||
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')
|
Loading…
Reference in New Issue