-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathserver.py
35 lines (28 loc) · 917 Bytes
/
server.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
from flask import Flask, request, jsonify
import json
import requests
import os
app = Flask(__name__)
port = int(os.environ["PORT"])
print(port)
@app.route('/', methods=['POST'])
def index():
print(port)
data = json.loads(request.get_data().decode('utf-8'))
# FETCH THE CRYPTO NAME
crypto_name = data["nlp"]["entities"]["crypto_name"][0]["raw"]
crypto_ticker = crypto_name.upper()
# FETCH BTC/USD/EUR PRICES
r = requests.get("https://min-api.cryptocompare.com/data/price?fsym="+crypto_ticker+"&tsyms=BTC,USD,EUR")
return jsonify(
status=200,
replies=[{
'type': 'text',
'content': 'The price of %s is :\n%f BTC, \n%f USD, and \n%f EUR.' % (crypto_ticker, r.json()['BTC'], r.json()['USD'], r.json()['EUR'])
}]
)
@app.route('/errors', methods=['POST'])
def errors():
print(json.loads(request.get_data()))
return jsonify(status=200)
app.run(port=port, host="0.0.0.0")