-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·122 lines (93 loc) · 3.01 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()
import http.client, base64
import json
import os
from flask import Flask
from flask import request
from flask import make_response
# Flask app should start in global layout
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
#print("Request:")
#print(json.dumps(req, indent=4))
res = processRequest(req)
res = json.dumps(res, indent=4)
print("Response:")
print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def processRequest(req):
action = req.get("result").get("action")
print("action")
print(action)
if action == "find-status":
conn = http.client.HTTPSConnection("my316075.sapbydesign.com")
baseurl = "/sap/byd/odata/cust/v1/purchasing/PurchaseOrderCollection/?"
#baseurl = "https://services.odata.org/Northwind/Northwind.svc/Products?"
yql_query = makeYqlQuery(req)
yql_url = baseurl + yql_query + "&$format=json"
print(yql_url)
base64string = base64.encodestring(('%s:%s' % ("odata_demo", "Welcome01")).encode()).decode().replace('\n', '')
headers = {
'authorization': "Basic " + base64string
}
conn.request("GET", yql_url, headers=headers)
res = conn.getresponse()
result = res.read()
print("result")
print(result)
data = json.loads(result)
print("data")
print(data)
res = makeWebhookResult(data)
return res
else:
print("action not found")
return {}
def makeYqlQuery(req):
result = req.get("result")
parameters = result.get("parameters")
poid = parameters.get("id")
if poid is None:
return None
#return "$filter=PurchaseOrderID eq " + "'" + str(poid) + "'"
return "$top=1"
def makeWebhookResult(data):
d = data.get('d')
print("json.d: ")
print(json.dumps(d, indent=4))
value = d.get('results')
print("json.results: ")
print(json.dumps(value, indent=4))
"""value = data.get('value')
print("json.value: ")
print(json.dumps(value, indent=4))
prodID = value[0].get('ProductID')
print("json.ID: ")
print(json.dumps(prodID, indent=4))
prodName = value[0].get('ProductName')
print("json.name: ")
print(json.dumps(prodName, indent=4))
speech = "Product ID is " + str(prodID) + \
"The description of product is " + prodName"""
speech = "Purchase Order ID is " + str(value[0].get('PurchaseOrderID')) + \
" and the status of this PO is " + value[0].get('PurchaseOrderLifeCycleStatusCodeText')
print("Response:")
print(speech)
return {
"speech": speech,
"displayText": speech,
# "data": data,
#"contextOut": "",
"source": "byd-assistant"
}
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000))
print("Starting app on port %d" % port)
app.run(debug=False, port=port, host='0.0.0.0')