-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.txt
235 lines (191 loc) · 8.2 KB
/
backup.txt
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python
from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()
from urllib.parse import urlparse, urlencode
from urllib.request import urlopen, Request
from urllib.error import HTTPError
import json
import os
import requests
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('/chat', methods=['GET'])
def web():
return "WELCOME :)"
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
#req = json.loads(string , strict=False)
#print("Request:")
#print(json.dumps(req, indent=4))
# print(type(req))
res = processRequest(req)
# print(type(res))
res = json.dumps(res, indent=4)
# print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
def processRequest(req):
if req.get("result").get("action") != "search_doctors":
return {}
else :
if req['result']['parameters']['loc_city'] == "" and req['result']['parameters']['loc_code'] == "" and req['result']['parameters']['loc_state'] == "" and req['result']['parameters']['loc_address'] == "":
return {"messages" : [ {"type" : 0, "speech" : "please enter your location, try entering city/zipcode or state"}]}
else :
if req['result']['parameters']['loc_address'] != "":
where = req['result']['parameters']['loc_address']
elif req['result']['parameters']['loc_code'] != "":
where = req['result']['parameters']['loc_code']
elif req['result']['parameters']['loc_city'] != "":
where = req['result']['parameters']['loc_city']
else : #req['result']['parameters']['loc_state'] != ""
where = req['result']['parameters']['loc_state']
what = req['result']['parameters']['specialist']
# where = req['result']['parameters']['geo-city']
baseurl = "https://pbh-uat.healthgrades.com/api/v4_0/providersearch/v4_0/pbh/search?cID=PBHTEST_007&providerType=None&what="+what+"&where="+where+"&sortBy=BestMatch"
#baseurl = "https://pbh-uat.healthgrades.com/api/v4_0/providersearch/v4_0/pbh/search?cID=PBHTEST_007&providerType=None&what="+what+"&pt="+str(points[0])+"%2C%20"+str(points[1])+"&sortBy=BestMatch"
data = get_data(baseurl)
if data[0] == 0:
# l = []
# for i in range(0, len(data[1])):
# l.append({
# "content_type": "text",
# "title": data[1][i],
# "payload": data[1][i]
# })
return { "messages": [{'title' : "your search matched multiple locations, please select one", "type" : 2, "replies" : data[1]}]}
# return { "type":2 , "replies": data[1]}
# return {"facebook" :{"text": "These are the multiple results that matched your search, please select one","quick_replies": l}}
elif data[0] == 1:
# print(data[1])
res = json.loads(data[1])
res = create_messages(res)
return res
elif data[0] == 2:
return data[1]
# 'messages':
# [
# {'title': 'when',
# 'replies': ['12:00',
# '13:00',
# '17:00',
# '18:00'],
# 'type': 2}],
# data = create_namelist(data)
# res = create_messages(data)
# "quick_replies":[
# {
# "content_type":"text",
# "title":"Red",
# "payload":"DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED"
# }
# ]
def create_messages(js):
if len(js['Results']) == 0:
a = {"messages": [
{
"type": 0,
"speech": "sorry your search didn't match any results :("
}
]}
return a
elif len(js['Results']) >= 5:
l1 = [{'type':0, 'speech':'These are the results that matched your search'}]
for i in range(0, 5):
a = {
"type" : 1,
"title" : js['Results'][i]['DemographicInfo']['DisplayName'],
"subtitle" : js['Results'][i]['DemographicInfo']['DisplayLastName'],
"imageUrl" : js['Results'][i]['DemographicInfo']['ImagePaths'][2]['Url'],
"buttons" : [{
"text" : "visit page" ,
"postback" : js['Results'][i]['DemographicInfo']['ProviderUrl']
}]
}
l1.append(a)
print({"messages" : l1})
return {"messages" : l1}
elif len(js['Results']) < 5:
l1 = [{'type':0, 'speech':'These are the results that matched your search'}]
for i in range(0,len(js['Results'])):
# print(js['Results'][i]['DemographicInfo']['DisplayName'])
# print(js['Results'][i]['DemographicInfo']['ProviderUrl'])
# print(js['Results'][i]['DemographicInfo']['ImagePaths'][0]['Url'])
# print(js['Results'][i]['DemographicInfo']['DisplayLastName'])
a = {
"type" : 1,
"title" : js['Results'][i]['DemographicInfo']['DisplayName'],
"subtitle" : js['Results'][i]['DemographicInfo']['DisplayLastName'],
"imageUrl" : js['Results'][i]['DemographicInfo']['ImagePaths'][2]['Url'],
"buttons" : [{
"text" : "visit page" ,
"postback" : js['Results'][i]['DemographicInfo']['ProviderUrl']
}]
}
l1.append(a)
print({"messages" : l1})
return {"messages" : l1}
else :
a = {"messages": [
{
"type": 0,
"speech": "sorry your search didn't match any results :("
}
]}
return a
def get_data(baseurl):
headers = {
"Accept": "application/json",
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjZCdFdLZ1g5RDd1ZGowYTJyLWkyZGFiN3dKRSIsImtpZCI6IjZCdFdLZ1g5RDd1ZGowYTJyLWkyZGFiN3dKRSJ9.eyJpc3MiOiJodHRwczovL3BiaC11YXQuaGVhbHRoZ3JhZGVzLmNvbS9hcGkvdjFfMC9zdHMvaWRlbnQiLCJhdWQiOiJodHRwczovL3BiaC11YXQuaGVhbHRoZ3JhZGVzLmNvbS9hcGkvdjFfMC9zdHMvaWRlbnQvcmVzb3VyY2VzIiwiZXhwIjoxNTAzNDkzMjU0LCJuYmYiOjE1MDM0ODk2NTQsImNsaWVudF9pZCI6InBiaC1kZXZlbG9wZXJwb3J0YWwtc3dhZ2dlcmhhcm5lc3MtaW1wbGljaXQtY2xpZW50Iiwic2NvcGUiOiJwYmgucHJvdmlkZXJzZWFyY2gudjRfMCIsInN1YiI6IjgzYmVhOTQ1LWZmZGUtNGYzZC04YzU0LWUwZTBjYWJmNjZjMCIsImF1dGhfdGltZSI6MTUwMjY5MDE1OCwiaWRwIjoiaWRzcnYiLCJQcm92aWRlclNlYXJjaF92NF8wIjoiUGJoX1NlYXJjaF9HZXQiLCJhbXIiOlsicGFzc3dvcmQiXX0.DxebugR8YmGCsJrwiU9lgeVGd2Q5Vmx9GJDs6iV-ZBMAX2DTN50nu68moVG9Np6zwYYBqIjbHpGcMjxUa7YjhTloW1Cv5baqxs3HMYA8Cgs9KB7ILHapTjsqN74CxRC1tI3imFzSxR7Hm5H7deiXCwJg12wbR0BTUE2AzfOT4h5kjhl7D_2w-jf0Qm3ucfpyjrUI3hZL2smbg-D8VteIjNbkkcyenWf3HIzGbZAWqqlSztIH0qWk6MwiG1lxBollZBTrXJ-74C_T08OoJHOLmJ3egihiNJK3Oo9GPmKUzz8G6SmAEoiSOiHOy9UlsQfX2weHHYOeoSHZ4Szeeypkvw'
}
response = requests.get(baseurl, headers=headers)
if response.status_code == 400:
js = json.loads(response.text , strict = False)
if js['Message'].find('matched multiple locations') != -1 :
return (0, js['Message'].split(':', 1)[-1].split(';'))
else:
return (2, {"messages": [
{
"type": 0,
"speech": "sorry your search didn't match any results :("
}
]})
else :
text = response.text
return (1, text)
# def create_namelist(data):
# l = []
# for i in range(0, 5):
# l.append(data['Results'][i]['DemographicInfo']['DisplayName'])
# return l
# def create_messages(l):
# l1 = [{'type':0,'speech':'these are the top 5 results that matched your search...'}]
# for i in l:
# a = {"type":0,
# "speech":i}
# l1.append(a)
# return {"messages":l1}
# def processRequest(req):
# names = create_namelist(req)
# return create_messages(names)
# def create_namelist(data):
# l = []
# for i in range(0, 5):
# l.append(data['Results'][i]['DemographicInfo']['DisplayName'])
# return l
# def create_messages(l):
# l1 = [{'type':0,'speech':'these are the top 5 results that matched your search...'}]
# for i in l:
# a = {"type":0,
# "speech":i}
# l1.append(a)
# return {"messages":l1}
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')