-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.py
715 lines (665 loc) · 23.9 KB
/
voice.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
import json
from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse, Gather
from twilio import twiml
from twilio.rest import Client
# from gmail import send_mail
import pandas as pd
print("call initiated")
n = 0
q_repeat = 0
repeat = False
recheck_repeat = 0
payable = True
texts = [ "Excuse me? i didn't get you. Could you say one more time please?",
"Beg your pardon?, Would you mind repeating that?",
"Sorry?, Could you say that again, please?",
"I can’t hear your voice… Can you say it in another way, please?"
]
# Importing json data
try:
with open("conversation.json", "r") as f:
use_case = json.load(f)
bot_resp = use_case['intro']
with open("creds.json", "r") as f:
json_data = json.load(f)
data = json_data[0]
from_ = data["From"]
to_ = data["To"]
ngrok_url = data["ngrok_url"]
auth_token = data["auth_token"]
sid = data["Account SID"]
except Exception as e:
print(e)
print("A problem occured in exporting json data")
import sys
sys.exit()
app = Flask(__name__)
@app.route("/hello", methods=['GET', 'POST'])
def hello():
# Start a TwiML response
try:
resp = VoiceResponse()
except Exception as e:
print(e)
try:
gather = Gather(input='speech', timeout=1 , action='/voice')
resp.redirect('/voice')
resp.append(gather)
except Exception as e:
print(e)
return str(resp)
@app.route("/voice", methods=['GET', 'POST'])
def voice():
global bot_resp
global n
global names
try:
with open("count.txt", 'r') as f:
count = int(f.read(f.read()).strip())
print(count)
except Exception as e:
print(e)
try:
resp = VoiceResponse()
except Exception as e:
print(e)
try:
name = names[count]
except:
resp.hangup()
try:
gather = Gather(input='speech', speech_timeout=2, action='/check_usecase')
except Exception as e:
print(e)
if n==0:
bot_resp = bot_resp[n]+"{}".format(name)
elif n == 1:
bot_resp = bot_resp[n] + "Dated {}, For your personal load from us. Yes?".format(due)
b_resp = bot_resp[n]
gather.say(b_resp, voice="man", language="en")
resp.append(gather)
resp.redirect('/recheck')
return str(resp)
@app.route('/check_usecase', methods=['GET', 'POST'])
def check_usecase():
"""To check which use case is going to be initiated. Processes results from the <Gather> prompt in /voice"""
global n
global data
global bot_resp
global q_repeat
global conv_1_1
global conv_1_2
global conv_1_3
global conv_1_4
global conv_2_1
global conv_2_2
global conv_3_1
global texts
global recheck_repeat
# To check whether 'no' is replied twice
global repeat
recheck_repeat = 0
resp = VoiceResponse()
resp.hangup()
# print(request.values)
if 'SpeechResult' in request.values:
choice = request.values['SpeechResult']
choice = choice.lower()
print(choice)
if choice:
if n==0:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/voice')
return str(resp)
for i in conv_1_1:
if str(i) in choice:
for i in conv_1_6:
if str(i) in choice:
n+=1
text = "I am doing great! Thank you for asking."
print(text)
resp.say(text, voice='woman', language='en')
resp.redirect("/voice")
return str(resp)
else:
continue
else:
continue
for i in conv_1_2:
if str(i) in choice:
text = "My name is Neha from No Cost"
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)
else:
continue
for i in conv_1_3:
if str(i) in choice:
text = "I am doing great! Thank you for asking."
resp.say(text, voice='woman', language='en')
resp.redirect("/voice")
return str(resp)
else:
continue
for i in conv_1_4:
if str(i) in choice:
bot_resp = use_case["use_case_5"]
resp.redirect("/use_case_5")
return str(resp)
else:
continue
# If no match found
text = texts[q_repeat]
q_repeat += 1
if q_repeat == 4:
q_repeat = 0
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)
elif n==1:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/voice')
return str(resp)
for i in conv_2_1:
if str(i) in choice:
n+=1
text = "Thank you"
resp.say(text, voice='woman', language='en')
resp.redirect('/voice')
return str(resp)
else:
continue
for i in conv_2_2:
if str(i) in choice:
n = 0
bot_resp = use_case["use_case_3"]
resp.redirect('/use_case_3')
return str(resp)
else:
continue
for i in conv_2_3:
if str(i) in choice:
n = 0
bot_resp = use_case["use_case_4"]
resp.redirect('/use_case_4')
return str(resp)
else:
continue
text = texts[q_repeat]
q_repeat += 1
if q_repeat == 4:
q_repeat = 0
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)
elif n==2:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/voice')
return str(resp)
for i in conv_3_1:
if str(i) in choice:
n = 0
bot_resp = use_case["use_case_1"]
resp.redirect('/use_case_1')
return str(resp)
else:
continue
for i in conv_3_2:
if str(i) in choice:
n = 0
bot_resp = use_case["use_case_2"]
resp.redirect('/use_case_2')
return str(resp)
else:
continue
text = texts[q_repeat]
q_repeat += 1
if q_repeat == 4:
q_repeat = 0
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)
@app.route("/use_case", methods=['GET', 'POST'])
def use_case():
""" prompted right after check_usecase function
bot_resp will be chunked as per use_case from conversation.json
"""
# Start a TwiML response
global bot_resp
global due_charges
global bounce_charges
global dues
global emis
global n
global payable
bounce = bounce_charges[n]
due_charge = due_charges[n]
due = dues[n]
emi = emis[n]
if payable:
text = "You total payable is {} rupees. E.M.I. {} with Bounce charge {} with over due charge of {}. The amount \
you pay, you will be getting an e-receipt.".format(due, emi, bounce, due_charge)
payable = False
else:
text = bot_resp[n]
try:
resp = VoiceResponse()
except Exception as e:
print(e)
text = bot_resp[n]
try:
gather = Gather(input='speech', timeout=2 , action='/check_usecase')
gather.say(text, voice="man", language="en")
resp.append(gather)
except Exception as e:
print(e)
return str(resp)
@app.route('/use_case_5', methods=['GET', 'POST'])
def use_case_5():
"""Processes results from the <Gather> prompt in /voice"""
global n
global data
global bot_resp
global q_repeat
global conv_8_1
global conv_8_2
global conv_8_3
global conv_8_4
global texts
global recheck_repeat
global payable
# To check whether 'no' is replied twice
global repeat
recheck_repeat = 0
resp = VoiceResponse()
resp.hangup()
# print(request.values)
if 'SpeechResult' in request.values:
choice = request.values['SpeechResult']
choice = choice.lower()
print(choice)
if choice:
if n == 0:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_8_1:
if str(i) in choice:
n +=1
resp.redirect("/use_case")
return str(resp)
else:
continue
elif n ==1:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_8_2:
if str(i) in choice:
n +=1
resp.redirect("/use_case")
return str(resp)
else:
continue
elif n ==2:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_8_3:
if str(i) in choice:
n = 0
text = "ok, i will call back. Thank you for your time. Have a nice day"
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
else:
continue
for i in conv_8_4:
if str(i) in choice:
n +=1
resp.redirect("/use_case")
return str(resp)
else:
continue
elif n ==3:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_8_3:
if str(i) in choice:
n = 0
text = "ok, i will call back. Thank you for your time. Have a nice day"
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
else:
continue
# to be saved in database
issue = choice
print(issue)
text = "Okay. Let me inform bank again on this so that, someone from our end gets this addressed."
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)@app.route('/use_case_4', methods=['GET', 'POST'])
def use_case_4():
"""Processes results from the <Gather> prompt in /voice"""
global n
global data
global bot_resp
global q_repeat
global conv_7_1
global texts
global recheck_repeat
global payable
# To check whether 'no' is replied twice
global repeat
recheck_repeat = 0
resp = VoiceResponse()
resp.hangup()
# print(request.values)
if 'SpeechResult' in request.values:
choice = request.values['SpeechResult']
choice = choice.lower()
print(choice)
if choice:
if n == 0:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_7_1:
if str(i) in choice:
n = 0
text = "Okay. Let me inform bank again on this, so that someone from our end gets this addressed."
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
else:
continue
# to be saved in database
issue = choice
print(issue)
text = "Okay. Let me inform bank again on this, so that someone from our end gets this addressed."
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
@app.route('/use_case_3', methods=['GET', 'POST'])
def use_case_3():
"""Processes results from the <Gather> prompt in /voice"""
global n
global data
global bot_resp
global q_repeat
global conv_6_1
global conv_6_2
global texts
global recheck_repeat
global payable
# To check whether 'no' is replied twice
global repeat
recheck_repeat = 0
resp = VoiceResponse()
resp.hangup()
# print(request.values)
if 'SpeechResult' in request.values:
choice = request.values['SpeechResult']
choice = choice.lower()
print(choice)
if choice:
if n == 0:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_6_1:
if str(i) in choice:
n+=1
text = "Ok, let me send the details to our officers."
resp.say(text, voice='woman', language='en')
resp.redirect("/use_case")
return str(resp)
else:
continue
elif n == 1:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_6_2:
if str(i) in choice:
n = 0
text = "Okay.I’m sending this for further scrutiny, Thank you for your time.Have a nice day"
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
text = texts[q_repeat]
q_repeat += 1
if q_repeat == 4:
q_repeat = 0
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)@app.route('/use_case_2', methods=['GET', 'POST'])
def use_case_2():
"""Processes results from the <Gather> prompt in /voice"""
global n
global data
global bot_resp
global q_repeat
global conv_5_1
global conv_5_2
global texts
global recheck_repeat
global payable
# To check whether 'no' is replied twice
global repeat
recheck_repeat = 0
resp = VoiceResponse()
resp.hangup()
# print(request.values)
if 'SpeechResult' in request.values:
choice = request.values['SpeechResult']
choice = choice.lower()
print(choice)
if choice:
if n == 0:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_5_1:
if str(i) in choice:
n+=1
text = "Ok, let me send the details to our officers."
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
else:
continue
elif n == 1:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
else:
n=0
text = "Request You to kindly try to clear the due as soon as possible and before month-end. \
Thank you for your time. Have a nice day."
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
text = texts[q_repeat]
q_repeat += 1
if q_repeat == 4:
q_repeat = 0
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)
@app.route('/use_case_1', methods=['GET', 'POST'])
def use_case_1():
"""Processes results from the <Gather> prompt in /voice"""
global n
global data
global bot_resp
global q_repeat
global conv_4_1
global texts
global recheck_repeat
# To check whether 'no' is replied twice
global repeat
recheck_repeat = 0
resp = VoiceResponse()
resp.hangup()
# print(request.values)
if 'SpeechResult' in request.values:
choice = request.values['SpeechResult']
choice = choice.lower()
print(choice)
if choice:
if n == 0:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
for i in conv_4_1:
if str(i) in choice:
n+=1
text = "Let me confirm your amount details."
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
else:
continue
elif n == 1:
if "repeat" in choice or "once more" in choice or "again" in choice:
text = "well"
resp.say(text, voice='woman', language='en')
resp.redirect('/use_case')
return str(resp)
else:
n=0
text = "Thank you for your time. Have a nice day."
resp.say(text, voice='woman', language='en')
resp.hangup()
return str(resp)
text = texts[q_repeat]
q_repeat += 1
if q_repeat == 4:
q_repeat = 0
resp.say(text, voice='woman', language='en')
resp.redirect("/test")
return str(resp)
@app.route("/recheck", methods=['GET', 'POST'])
def recheck():
""" To recheck input from client in voice """
global recheck_repeat
try:
resp = VoiceResponse()
except Exception as e:
print(e)
try:
gather = Gather(input='speech', timeout=2 , action='/gather')
if recheck_repeat == 2:
resp.hangup()
return str(resp)
text = "It seems voice is not clear to me. can you please repeat it once more?"
recheck_repeat += 1
gather.say(text, voice="man", language="en")
resp.append(gather)
except Exception as e:
print(e)
resp.redirect("/recheck")
return str(resp)
@app.route("/test", methods=['GET', 'POST'])
def test():
# Start a TwiML response
try:
resp = VoiceResponse()
except Exception as e:
print(e)
try:
gather = Gather(input='speech', speech_timeout='auto', action='/gather')
resp.append(gather)
except Exception as e:
print(e)
return str(resp)
# resp.redirect('/')
with open("keywords.json", "r") as f:
data = json.load(f)
conv_1_1 = data["conv_1"]["conv_1_1"]
conv_1_2 = data["conv_1"]["conv_1_2"]
conv_1_3 = data["conv_1"]["conv_1_3"]
conv_1_4 = data["conv_1"]["conv_1_4"]
conv_2_1 = data["conv_2"]["conv_2_1"]
conv_2_2 = data["conv_2"]["conv_2_2"]
conv_3_1 = data["conv_3"]["conv_3_1"]
conv_3_2 = data["conv_3"]["conv_3_2"]
conv_4_1 = data["conv_4"]["conv_4_1"]
conv_4_2 = data["conv_4"]["conv_4_2"]
conv_5_1 = data["conv_5"]["conv_5_1"]
conv_5_2 = data["conv_5"]["conv_5_2"]
conv_6_1 = data["conv_6"]["conv_6_1"]
conv_6_2 = data["conv_6"]["conv_6_2"]
conv_7_1 = data["conv_7"]["conv_7_1"]
conv_8_1 = data["conv_8"]["conv_8_1"]
conv_8_2 = data["conv_8"]["conv_8_2"]
conv_8_3 = data["conv_8"]["conv_8_3"]
conv_8_4 = data["conv_8"]["conv_8_4"]
def outgoing_call(to_, from_,ngrok_url):
# try:
# number = numbers[count]
# to_ = "+"+str(number)
# print(to_)
# except:
# print("No number left to call")
# print(to_, ngrok_url, from_)
try:
client = Client(sid, auth_token)
try:
call = client.calls.create(
url = ngrok_url+"/hello",
to = to_.strip(),
from_ = from_
)
except Exception as e:
print(e)
except Exception as e:
print(e)
except KeyboardInterrupt as e:
print(e)
if __name__ == '__main__':
try:
read_csv = pd.read_csv("numbers.csv")
names = read_csv["Name"]
numbers = read_csv["Numbers"]
dues = read_csv["Due"]
bounce_charges = read_csv["Bounce Charge"]
due_charges = read_csv["Due Charge"]
emis = read_csv["EMI"]
except Exception as e:
print(e)
for number in numbers:
to_ = "+"+str(number)
print(to_)
# outgoing_call(to_=to_, from_=from_,ngrok_url=ngrok_url)
app.run(debug=True)