-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
38 lines (29 loc) · 1.15 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
from flask import Flask, jsonify, redirect, request
import wolframalpha
API_KEY = os.getenv('API_KEY')
TOKEN = os.getenv('TOKEN')
DEBUG = os.getenv('DEBUG', False)
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def main():
if request.method == 'POST':
if API_KEY is None or TOKEN is None:
return 'Missing configuration for API_KEY and/or TOKEN', 500
if request.form['token'] != TOKEN:
return 'Unauthorized', 401
# https://docs.python.org/3/library/stdtypes.html#str.split
query = request.form['text'].split(' ', 1)[1]
client = wolframalpha.Client(API_KEY)
res = client.query(query)
if len(res.pods) == 0:
return jsonify({'text': "Sorry, I couldn't find any relevant information for you."})
try:
response = next(res.results).text
except StopIteration:
response = "Sorry, I couldn't get a result for that query."
return jsonify({'text': response})
if request.method == 'GET':
return 'See https://github.com/chdsbd/wolframalpha-bot'
if __name__ == "__main__":
app.run(debug=DEBUG)