-
Notifications
You must be signed in to change notification settings - Fork 2
/
mydoctor.py
359 lines (340 loc) · 18.8 KB
/
mydoctor.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python
import requests
import json
import urllib
from flask import Flask, jsonify
from flask import request
from flask import abort
from flask import Response
from datetime import datetime
from datetime import time as newtime
from datetime import date
from flask_cors import CORS, cross_origin
import calendar
import os.path
import re
import random
from pymongo import MongoClient
import sys
import time
from datetime import datetime
from pytz import timezone
from time import sleep
import pytz
AUTHBot = 'Njk1YTMyNWUtNzg5Zi00ZmNmLWE4ZGYtMzkwYjNkMGViNWM4NjVmOWQzODMtYjgw'
HEADERSBot = {'Authorization': 'Bearer ' + AUTHBot}
headersCliniko= {'Content-Type': 'application/json','Accept': 'application/json','User-Agent': 'mydoctor([email protected])'}
BASE_URL = "https://api.ciscospark.com/v1/"
ROOMS_URL = BASE_URL + "rooms"
MESSAGES_URL = BASE_URL + "messages"
WEBHOOKS_URL = BASE_URL + "webhooks"
MEMBERSHIP_URL = BASE_URL + "memberships"
PEOPLE_URL = BASE_URL + "people"
clinikoBase_URL= "https://api.cliniko.com/v1/"
clinkoPatients_URL=clinikoBase_URL+"patients"
clinkoPractitioner_URL=clinikoBase_URL+"practitioners"
clinkoAppointment_URL=clinikoBase_URL+"appointments"
myID = '[email protected]'
BotId = 'Njk1YTMyNWUtNzg5Zi00ZmNmLWE4ZGYtMzkwYjNkMGViNWM4NjVmOWQzODMtYjgw'
uri= 'mongodb://mydoc:[email protected]:61262/mydoctor'
app = Flask(__name__)
cors=CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
client = MongoClient(uri,
connectTimeoutMS=30000,
socketTimeoutMS=None,
socketKeepAlive=True)
db = client.get_default_database()
def postMessageBotOneToOne(PersonId, message):
dict = {"toPersonId": PersonId, "markdown": message}
resp = requests.post(MESSAGES_URL, json=dict, headers=HEADERSBot)
roomId = json.loads(resp.text)['roomId']
return roomId
def createPatient(practiceId,firstName, lastName,dob,userAddress):
if db.practices.find({"practiceId": practiceId}).count() > 0:
cursor = db.practices.find_one({"practiceId": practiceId}, {"vendorName": 1,"apiKey": 1, "_id": 0})
vendorName=cursor["vendorName"]
apiKey=cursor["apiKey"]
if vendorName == "cliniko":
dict = {"first_name": firstName, "last_name": lastName,"date_of_birth":dob,"address_1":userAddress}
resp = requests.post(clinkoPatients_URL, json=dict, headers=headersCliniko,auth=(apiKey,''))
patientId = json.loads(resp.text)['id']
return patientId
def getPractitionerDetails(practiceId,first_name):
if db.practices.find({"practiceId": int(practiceId)}).count() > 0:
cursor = db.practices.find_one({"practiceId": int(practiceId)}, {"vendorName": 1,"apiKey": 1, "_id": 0})
vendorName=cursor["vendorName"]
apiKey=cursor["apiKey"]
print (apiKey)
print (vendorName)
if vendorName == "cliniko":
resp = requests.get(clinkoPractitioner_URL, headers=headersCliniko,auth=(apiKey,''))
practitioners = json.loads(resp.text)['practitioners']
print (practitioners)
for p in practitioners:
if p["first_name"]== first_name:
practitionerId=p["id"]
practitionerFirstname=p["first_name"]
practitionerLastname = p["last_name"]
practitionerTitle = p["title"]
practitionerDesignation = p["designation"]
return practitionerId,practitionerFirstname,practitionerLastname,practitionerTitle,practitionerDesignation
def getPractitionerDetailID(practiceId,practitionerId):
if db.practices.find({"practiceId": int(practiceId)}).count() > 0:
cursor = db.practices.find_one({"practiceId": int(practiceId)}, {"vendorName": 1,"apiKey": 1, "_id": 0})
vendorName=cursor["vendorName"]
apiKey=cursor["apiKey"]
if vendorName == "cliniko":
clinkoPractitionerone_URL=clinkoPractitioner_URL+"/"+practitionerId
resp = requests.get(clinkoPractitionerone_URL, headers=headersCliniko,auth=(apiKey,''))
practitionerFirstname=json.loads(resp.text)["first_name"]
practitionerLastname = json.loads(resp.text)["last_name"]
practitionerTitle = json.loads(resp.text)["title"]
practitionerDesignation = json.loads(resp.text)["designation"]
return practitionerFirstname,practitionerLastname,practitionerTitle,practitionerDesignation
def getPracticeIdDrName(first_name):
cursor = db.practices.find({}, {"practiceId":1,"vendorName": 1,"apiKey": 1, "_id": 0})
for doc in cursor:
vendorName=doc["vendorName"]
apiKey=doc["apiKey"]
practiceId = doc["practiceId"]
if vendorName == "cliniko":
resp = requests.get(clinkoPractitioner_URL, headers=headersCliniko,auth=(apiKey,''))
practitioners = json.loads(resp.text)['practitioners']
for p in practitioners:
if p["first_name"]== first_name:
return practiceId
def getAvailabletime(practiceId,practitionerId,vendorId,appointmentDate,appointment_type):
if db.practices.find({"practiceId": practiceId}).count() > 0:
cursor = db.practices.find_one({"practiceId": practiceId}, {"vendorName": 1,"apiKey": 1, "_id": 0})
vendorName=cursor["vendorName"]
apiKey=cursor["apiKey"]
if vendorName == "cliniko":
getavailabletimeUrl=clinikoBase_URL+'businesses/'+str(vendorId)+'/practitioners/'+str(practitionerId)+'/appointment_types/'+str(appointment_type)+'/available_times?from='+appointmentDate +'&to='+appointmentDate
print (getavailabletimeUrl)
resp = requests.get(getavailabletimeUrl, headers=headersCliniko,auth=(apiKey,''))
print (resp.text)
available_times = json.loads(resp.text)["available_times"]
return available_times
def createAppointment(practiceId,date,patientId, practitioerId,appointment_type_id,vendorId):
if db.practices.find({"practiceId": practiceId}).count() > 0:
cursor = db.practices.find_one({"practiceId": practiceId}, {"vendorName": 1,"apiKey": 1, "_id": 0})
vendorName=cursor["vendorName"]
apiKey=cursor["apiKey"]
if vendorName == "cliniko":
dict = {"appointment_start": date, "patient_id": patientId,"practitioner_id":practitioerId,"appointment_type_id":appointment_type_id,"business_id":vendorId}
resp = requests.post(clinkoAppointment_URL, json=dict, headers=headersCliniko,auth=(apiKey,''))
appointmentId = json.loads(resp.text)['id']
return patientId
def convertAvailabletime(strdatetime):
fmt = "%Y-%m-%dT%H:%M:%SZ"
timeonlyfmt="%H:%M:%S"
sourcetime = datetime.strptime(strdatetime, fmt)
utcsourcetime = sourcetime.replace(tzinfo=timezone('UTC'))
convertedlocaltime = utcsourcetime.astimezone(timezone('Australia/Adelaide'))
return convertedlocaltime.strftime(timeonlyfmt)
def convertTimezone(strdatetime,srctz,dsttz):
fmt = "%Y-%m-%dT%H:%M:%SZ"
sourcetime = datetime.strptime(strdatetime, fmt)
tzsourcetime = sourcetime.replace(tzinfo=timezone(srctz))
convertedtime = tzsourcetime.astimezone(timezone(dsttz))
return convertedtime
def toUTC(strdatetime,tz):
fmt = "%Y-%m-%dT%H:%M:%SZ"
datetimeinutc= tz.normalize(tz.localize(datetime.strptime(strdatetime, fmt))).astimezone(pytz.utc)
return datetimeinutc.strftime(fmt)
@app.route('/')
def index():
return "Hello, World!"
@app.route('/fullfillment', methods=['POST'])
def firehosehook():
if not request.json:
abort(400)
else:
personId = request.json["originalRequest"]["data"]["data"]["personId"]
try:
action = request.json["result"]["action"]
except:
action =""
pass
personidUrl = PEOPLE_URL + '/' + personId
resp = requests.get(personidUrl, headers=HEADERSBot)
try:
firstName = json.loads(resp.text)['firstName']
except:
firstName = ""
pass
try:
lastName = json.loads(resp.text)['lastName']
except:
lastName = ""
pass
if action == "input.welcome" and db.users.find({"personId": personId}).count() == 0:
postMessageBotOneToOne(personId, "Hello **"+firstName+"**! "+'I am Mydoctor Bot and I can be your personal assistant to help you to:\n\n- Find a GP near to you and book appointments for you \n\n- Remind you about your medical appointments \n\n- Alert you about your appointment recalls\n\n- Book a ride for appointment (not yet but soon..) **')
data = {
'speech': 'It looks like I did not serve you before. I know little about yourself, but to become your assistant, I would like to know more about yourself, would you like to proceed?',
'displayText': 'It looks like I did not serve you before. I know little about yourself, but to become your assistant, I would like to know more about yourself, would you like to proceed?',
'data':{},
'contextOut':[{"name":"Welcome-followup", "lifespan":1, "parameters":{}}],
'source':""
}
elif action == "input.welcome" and db.users.find({"personId": personId}).count() > 0:
cursor = db.users.find_one({"personId": personId},{"firstName": 1, "_id": 0})
firstName=cursor["firstName"]
data = {
'speech': 'Hey '+firstName+'! how can I help you today?',
'displayText': 'Hey '+firstName+'! how can I help you today?',
'data': {},
'contextOut': [{"name": "appointment_booking", "lifespan": 1, "parameters": {}}],
'source': ""
}
elif action == "useronboarding.yes":
postMessageBotOneToOne(personId, "Thats Awesome!, let me ask few questions now.")
data = {
'speech': "",
'displayText': "",
'data':{},
"followupEvent": {"name": "user-data-collection-event", "data": {"firstName":firstName,"lastName":lastName,"dob":"","userAddress":""}},
'contextOut':[],
'source':""
}
elif action == "userdatacollection.done":
userData=request.json["result"]["parameters"]
firstName=userData["firstName"]
userData.update({"personId":personId,"practices":[]})
result = db.users.insert_one(userData)
if result.inserted_id:
data = {
'speech': "Thanks "+firstName+"! I have saved your details, How can I help you today?",
'displayText': "Thanks "+firstName+"! I have saved your details, How can I help you today?",
'data': {},
"followupEvent": {},
'contextOut': [{"name": "appointment_booking", "lifespan": 1, "parameters": {}}],
'source': ""
}
else:
data = {
'speech': "Sorry we are experiencing some technical problems",
'displayText': "Sorry we are experiencing some technical problems",
'data': {},
"followupEvent": {},
'contextOut': [],
'source': ""
}
elif action == "appointment.book":
#postMessageBotOneToOne(personId, "No problems, I can help you with that.")
appointmentData = request.json["result"]["parameters"]
appointmentDate=appointmentData["appointmentDate"]
practitionerName = appointmentData["practitionerName"]
time = appointmentData["time"]
appointment_type=0
practiceId = getPracticeIdDrName(practitionerName)
patientId=None
cursor = db.users.find_one({"personId": personId},{"firstName": 1, "lastName": 1, "dob": 1, "userAddress": 1,"practices":1, "_id": 0})
praccursor = db.practices.find_one({"practiceId": practiceId}, {"practiceName": 1, "Address": 1, "vendorId":1,"services":1,"_id": 0})
for prac in praccursor["services"]:
if prac["name"] == "Generic":
appointment_type = prac["appointment_type"]
for pracs in cursor["practices"]:
if pracs["practiceId"] ==practiceId:
patientId = pracs["patientId"]
if not patientId:
#postMessageBotOneToOne(personId, "it seems like you haven't been to any of our registered practices, let me add your details first")
patientId=createPatient(practiceId, cursor["firstName"], cursor["lastName"], cursor["dob"], cursor["userAddress"])
result = db.users.update(
{"personId": personId},
{"$push":{'practices': {"practiceId": practiceId,"patientId":patientId}}}
)
#if patientId and result['nModified']>0:
#postMessageBotOneToOne(personId, "All good. I have added your details into "+praccursor["practiceName"])
practitionerId, practitionerFirstname, practitionerLastname, practitionerTitle, practitionerDesignation=getPractitionerDetails(practiceId, practitionerName)
#postMessageBotOneToOne(personId,"\n\nLet me check **" +practitionerTitle+"."+practitionerFirstname+"**'s availability. Please hang on")
available_times=getAvailabletime(practiceId, practitionerId, praccursor["vendorId"], appointmentDate,appointment_type)
convertedAvailabletimes=[]
message_slot=""
if len(available_times)>0:
i=1
for a_t in available_times:
available_time=convertAvailabletime(a_t["appointment_start"])
message_slot= message_slot+'\n\n- Slot '+str(i)+' :' +available_time
i=i+1
convertedAvailabletimes.append(available_time)
combineddate = appointmentDate + "T" + time + "Z"
#print ('booking local time is ' + combineddate)
booking_utc = toUTC(combineddate, timezone('Australia/Adelaide'))
if time in convertedAvailabletimes:
postMessageBotOneToOne(personId, "Yes, **Dr. "+practitionerFirstname+ "** will be able to see you at "+time)
print ('booking converted time in UTC is '+booking_utc)
data = {
'speech': "",
'displayText': "",
'data': {},
"followupEvent": {"name": "booking-confirmation-event","lifespan": 5,"data": {"practiceId": str(practiceId), "appointmentTime": booking_utc, "patientId": str(patientId),"practitionerId": str(practitionerId),"appointmentType": str(appointment_type),"vendorId":str(praccursor["vendorId"])}},
'contextOut': [],
'source': ""
}
else:
postMessageBotOneToOne(personId,
"Sorry, **Dr." + practitionerFirstname + "** is not available at the time you have requested, below are the available timeslots you can book"+message_slot)
data = {
'speech': '',
'displayText': '',
'data': {},
"followupEvent": {"name": "appointment-selection_event", "lifespan": 5,
"data": {"practitionerName": practitionerName, "appointmentDate": appointmentDate}},
'contextOut': [],
'source': ""
}
else:
data = {
'speech': "Sorry, there are no free slots available to book. please try again with new dates",
'displayText': "Sorry, there are no free slots available to book. please try again with new dates",
'data': {},
"followupEvent": {},
'contextOut': [{"name": "appointment_booking", "lifespan": 1, "parameters": {}}],
'source': ""
}
elif action == "booking.confirm.yes":
postMessageBotOneToOne(personId, "Thats Awesome!, let me proceed with your booking now")
appointmentType = request.json["result"]["parameters"]["appointmentType"]
appointmentTime = request.json["result"]["parameters"]["appointmentTime"]
patientId = request.json["result"]["parameters"]["patientId"]
practiceId = request.json["result"]["parameters"]["practiceId"]
vendorId = request.json["result"]["parameters"]["vendorId"]
practitionerId = request.json["result"]["parameters"]["practitionerId"]
praccursor = db.practices.find_one({"practiceId": int(practiceId)},
{"practiceName": 1, "Address": 1, "vendorId": 1, "services": 1,
"_id": 0})
practitionerFirstname, practitionerLastname, practitionerTitle, practitionerDesignation=getPractitionerDetailID(practiceId,practitionerId)
appointmentId = createAppointment(int(practiceId), appointmentTime, int(patientId), int(practitionerId),appointmentType,int(vendorId))
if appointmentId:
postMessageBotOneToOne(personId,
"\n\nThank you for your patience. Your booking is confirmed. Your booking details are:\n\n" +"**Booking ID:"+str(appointmentId)+"**\n\n"+"**Practitioner Details :"+practitionerTitle + "." + practitionerFirstname+ " "+practitionerLastname+ "**\n\n**Appointment Time:"+appointmentTime+"**\n\n**Practice Name:"+praccursor["practiceName"]+"**\n\n**Practice address:"+praccursor["Address"]+"**")
data = {
'speech': '',
'displayText': '',
'data': {},
'contextOut': [{"name": "appointment_booking", "lifespan": 1, "parameters": {}}],
'source': ""
}
else:
data = {
'speech': 'Sorry, I could not book this appointment,Can you please ping me little later?',
'displayText': 'Sorry, I could not book this appointment,Can you please ping me little later?',
'data': {},
'contextOut': [{"name": "appointment_booking", "lifespan": 1, "parameters": {}}],
'source': ""
}
else:
data={}
js = json.dumps(data)
resp = Response(js, status=200, mimetype='application/json')
resp.headers["Content-Type"] = "application/json"
return resp
if __name__ == '__main__':
try:
app.run(host='0.0.0.0', port=80, debug=True)
except KeyboardInterrupt:
pass
finally:
print('closing connection....')