forked from apsknight/swayamsevak-fulfillment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
401 lines (340 loc) · 11.4 KB
/
fetch.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
This Module fetch data from Database client and
generate responses to be sent as Dialogflow fulfillment.
"""
# import general dependencies
import requests, json
import datetime
# import Response Generator library
import df_response_lib
# Clients for building responses
fulfillment_client = df_response_lib.fulfillment_response()
actions_client = df_response_lib.actions_on_google_response()
# Base route for Database client
base_url = 'http://2019.almafiesta.com/testing2019/html/ContactFrom_v11/api/'
# To be implemented
def handle_key_error(error):
pass
def handle_status_error():
return{
"speech": "I could not find what you were looking for."
}
# Method for returning carousel containing details of all available doctors.
def handle_all_doctors():
endpoint = base_url + 'allDoctors.php'
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
template = {
"speech": "These are all the doctors in the hospital",
"displayText": "No",
"messages": [{
"type": "carousel_card",
"items": [
]
}]
}
for item in response['data']:
data = {
"heading": item['name'],
"status": item['status'],
"title1": "Department: "+item['department'],
"title2": "Room: "+item['room']
}
template['messages'][0]['items'].append(data)
return template
# return actions_client.carousel_card(response)
# return {
# "speech": "I want to wrie this"
# "messages": [{
# "type": "carousel_card",
# "items": [
# {
# "title": "Doctor Singh",
# "id": "C01",
# "image": {
# "url": "https://user-images.githubusercontent.com/29799995/53495668-6392a000-3ac6-11e9-8f15-a66dede37a1d.png",
# },
# "optionInfo": {
# "key": "K01"
# },
# "description": "Room No: 54, Phone Number: 6789"
# },
# {
# "title": "Doctor Madhav",
# "id": "C02",
# "image": {
# "url": "https://user-images.githubusercontent.com/29799995/53495668-6392a000-3ac6-11e9-8f15-a66dede37a1d.png",
# },
# "optionInfo": {
# "key": "K02"
# },
# "description": "Room No: 44, Phone Number: 7789"
# }
# ]
# }],
# "source": "flask",
# }
def handle_all_Beds(type):
endpoint = base_url + 'allBeds.php'
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
template = {
"speech": "These are the beds satisfying your requirement",
"displayText": "No",
"messages": [{
"type": "carousel_card",
"items": [
]
}]
}
if type=="":
for item in response['data']:
data = {
"heading": item['roomtype'],
"status": item['status'],
"title1": "BedNo: "+item['bedno'],
"title2": "Charges: "+item['charges']
}
template['messages'][0]['items'].append(data)
else:
for item in response['data']:
if item['roomtype']==type:
data = {
"heading": item['roomtype'],
"status": item['status'],
"title1": "BedNo: "+item['bedno'],
"title2": "Charges: "+item['charges']
}
template['messages'][0]['items'].append(data)
return template
def handle_patient_appointments(name):
endpoint = base_url + 'appointmentByPatient.php?name='+name
response = json.loads(requests.get(endpoint,).text)
if response['status'] != 1:
return handle_status_error()
template = {
"speech": "Your id is "+response['data'][0]['id']+"and appointment is on "+response['data'][0]['date']
}
return template
def handle_doctors_contact(name):
endpoint = base_url + 'contactByDoctor.php?name='+name
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
item=response['data'][0]
template = {
"speech": "This is the contact information of the doctor. You can click on the button to compose a mail",
"displayText": "No",
"messages": [{
"type": "basic_card",
"heading": item['name'],
"title1": "Mobile: "+item['mobile'],
"title2": "Email: "+item['email'],
"button": {
"title1": "Mail Doctor",
"uri1": "mailto:"+item['email'],
"title2": "Call Doctor",
"uri2": "tel:"+item['mobile']
}
}]
}
return template
def handle_department_doctors(dept):
endpoint = base_url + 'doctorByDepartment.php?dept='+dept
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
template = {
"speech": "These are the doctors in "+dept,
"displayText": "No",
"messages": [{
"type": "carousel_card",
"items": [
]
}]
}
for item in response['data']:
data = {
"heading": item['name'],
"status": item['status'],
"title1": "Phone: "+str(item['phone']),
"title2": "Room: "+item['room']
}
template['messages'][0]['items'].append(data)
return template
def handle_fee_doctors(name):
endpoint = base_url + 'feeByDoctor.php?name='+name
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
template = {
"speech": "His fees is ₹ "+response['data'][0]['fees']
}
return template
def handle_medicines(name):
endpoint = base_url + 'medicineByName.php?name='+name
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
if response['data'][0]['status'] == 1:
template = {
"speech": "The medicine "+response['data'][0]['name']+" is available"
}
else:
template = {
"speech": "The medicine "+response['data'][0]['name']+" is unavailable"
}
return template
def handle_schedule_doctors(name):
endpoint = base_url + 'scheduleByDoctor.php?name='+name
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return handle_status_error()
template = {
"speech": response['data'][0]['name']+" is available on the following days",
"displayText": "No",
"messages": [{
"type": "carousel_card",
"items": [
]
}]
}
item = response['data'][0]['schedule']
if item['monday']!='0':
data = {
"heading": "Monday",
"title1": "Timings: "+item['monday']
}
template['messages'][0]['items'].append(data)
if item['tuesday']!='0':
data = {
"heading": "Tuesday",
"title1": "Timings: "+item['tuesday']
}
template['messages'][0]['items'].append(data)
if item['wednesday']!='0':
data = {
"heading": "Wednesday",
"title1": "Timings: "+item['wednesday']
}
template['messages'][0]['items'].append(data)
if item['thursday']!='0':
data = {
"heading": "Thursday",
"title1": "Timings: "+item['thursday']
}
template['messages'][0]['items'].append(data)
if item['friday']!='0':
data = {
"heading": "Friday",
"title1": "Timings: "+item['friday']
}
template['messages'][0]['items'].append(data)
if item['saturday']!='0':
data = {
"heading": "Saturday",
"title1": "Timings: "+item['saturday']
}
template['messages'][0]['items'].append(data)
if item['sunday']!='0':
data = {
"heading": "Sunday",
"title1": "Timings: "+item['sunday']
}
template['messages'][0]['items'].append(data)
else:
data={}
template['messages'][0]['items'].append(data)
return template
def handle_video_call():
return{
"speech": "Click on the button to start, when you are ready",
"displayText": "No",
"messages": [{
"type": "basic_card",
"heading": "Customer Support",
"title1": "Here you go",
"button": {
"title1": "Call",
"uri1": "https://appear.in/swayamsevak"
}
}]
}
def handle_help():
return{
"speech": "These are the things I can do!",
"messages": [
{
"type":"simple",
"speech":"Provide you with a list of doctors."
},
{
"type":"simple",
"speech":"Provide you with the schedule of a doctor."
},
{
"type":"simple",
"speech":"Tell the fee of a doctor."
},
{
"type":"simple",
"speech":"Give you the contact details of a doctor."
},
{
"type":"simple",
"speech":"Check availability of a medicine."
},
{
"type":"simple",
"speech":"Give you details of beds availability and charges."
},
{
"type":"simple",
"speech":"Check your next appointment."
},
{
"type":"simple",
"speech":"Connect you via video call to the support staff."
},
]
}
def handle_broadcast(map,session_id):
endpoint = base_url + 'broadcast.php?'
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return {}
template = {
"displayText": "No",
"messages": []
}
for item in response['data']:
# print (datetime.datetime.now().strftime("%H:%M:%S")+" "+item['mtime'])
if datetime.datetime.now().strftime("%H:%M:%S")>=item['mtime'] and map.get(session_id,'0')<item['id']:
map[session_id]=item['id']
# print("Got "+session_id+" for "+item['id'])
data = {
"type": "broadcast_card",
"announcement": item['mdata']
}
template['messages'].append(data)
return template
def handle_register(name, dob, sex):
endpoint = base_url + 'register.php?name='+name+'&dob='+dob+'&sex='+sex
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
handle_status_error()
return{
"speech": "Registration Successful"
}
def handle_register(name, docname, time):
endpoint = base_url + 'appointment.php?name='+name+'&docname='+docname+'&time='+time
response = json.loads(requests.get(endpoint).text)
if response['status'] != 1:
return{
"speech": "Slot Not free, try another slot"
}
return{
"speech": "Registration Successful"
}