-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tools.pm
2062 lines (1422 loc) · 49.1 KB
/
Tools.pm
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Common: Shared library for voice-based applications.
#
# Copyright (C) 2010 Nokia Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Billy Odero, Jonathan Ledlie
package Nokia::Common::Tools;
use Exporter;
@ISA = ('Exporter');
@EXPORT = ('get_max_results_to_listen_to', 'walk_query_results','dtmf_dispatch_static','dtmf_dispatch_dynamic', 'write_pid','reap_old_self','stream_file', 'say_number', 'get_channel_desc','user_has_hungup','get_hash_file','get_seconds_remaining_count','db_disconnect', 'place_call', 'codec', 'get_large_number', 'get_unchecked_large_number', 'get_unchecked_small_number', 'get_yes_no_option', 'get_dtmf_input','get_small_number','mv_tmp_to_comment_dir', 'mv_tmp_to_post_dir', 'mv_tmp_to_names_dir','mv_tmp_to_status_dir', 'mv_tmp_to_dir', 'record_file', 'get_max_uint','request_attendant', 'speech_or_dtmf_input', 'init_user', 'create_user', 'get_user_id', 'get_user_name','set_user_name', 'play_random', 'dtmf_quick_jump', 'init_tools', 'get_callcount', 'rnd_alphanum', 'sms_enqueue', 'unlink_file', 'unlink_tmp_file', 'set_nickname_file', 'get_nickname_file', 'dbi_connect', 'read_config', 'set_group_name');
use strict;
use DBI;
use Data::Dumper;
use IO::Socket;
use File::Copy;
use Nokia::Common::Stamp;
use Nokia::Common::Sound;
use Nokia::Common::Phone;
use Math::Random qw(random_uniform_integer);
use LWP;
use URI;
use URI::Escape;
use LWP::UserAgent;
use UNIVERSAL::require;
=head1 NAME
Nokia::Common::Tools - Utility module
=head1 DESCRIPTION
Provides all the utility methods required for performing commonly
performed tasks
=head1 METHODS
=cut
######################################################################
my $calldir; #= $ENV{"NASI_OUTGOING"};
my $cbCount = 0;
my $HASH_FILE_LENGTH = 16;
my $MAX_DTMF_PROMPTS = 2;
my $DEFAULT_TIMEOUT = 5000;
my $MAX_RESULTS_TO_LISTEN_TO = 60;
# This can be an in-memory file system, for short-lived files
# set in init-tools
my $tmp_dir; #= $ENV{"NASI_TMP"};
my $tmp_rec_dir; #= $tmp_dir.'/record/';
my $posts_dir;# = '/data/posts/';
my $names_dir;# = '/data/names/';
my $comments_dir;# = '/data/comments/';
my $status_dir;# = '/data/status/';
my $nicknames_dir;# = '/data/names/';
my $groups_dir;
######################################################################
#
sub codec {
# record files on the file system using this codec
# TODO would it be better to use 'sln'?
# Can 'sln' files be played through a web browser easily?
return 'sln';
}
######################################################################
sub get_max_uint {
return 4294967295;
}
######################################################################
=head2 init_tools
Called on startup.
Use for any initialization of this module
=cut
sub init_tools {
my ($self) = @_;
my $prefs = $self->get_property('prefs');
$calldir = $prefs->{paths}->{NASI_OUTGOING};
$tmp_dir = $prefs->{paths}->{NASI_TMP};
$tmp_rec_dir = $tmp_dir.'/record/';
$posts_dir = $prefs->{paths}->{NASI_DATA}.'/posts/';
$names_dir = $prefs->{paths}->{NASI_DATA}.'/names/';
$comments_dir = $prefs->{paths}->{NASI_DATA}.'/comments/';
$status_dir = $prefs->{paths}->{NASI_DATA}.'/status/';
$nicknames_dir = $prefs->{paths}->{NASI_DATA}.'/names/';
$groups_dir = $prefs->{paths}->{NASI_DATA}.'/groups/';
}
######################################################################
=head2 play_random
To make the system seem more "real", you can have multiple responses that
have the same meaning and play them randomly e.g. ok, fine, cool
=over 4
=item Args:
$msg: The full filepath to the file to be streamed
$random_key: if you are randomizing "ok" as explained above, the files should be
named as okay-01, okay-02 etc. The random key in this case is the unchanging part
of the filename i.e. "okay"
=back
=cut
sub play_random {
my ($self, $msg, $random_key) = @_;
my $MAX_RANDOM_MSGS = 5;
# Thought about having a cache for the random number generator
# But this would need to be locked across threads,
# and therefore probably more expensive than just creating them
# one at a time.
my $random_msg =
sprintf ("$random_key-%02d",
Math::Random::random_uniform_integer (1, 1, $MAX_RANDOM_MSGS));
my $dtmf = $self->agi->stream_file([&msg($self,$random_msg),$msg], "*#", "0");
return $dtmf;
}
######################################################################
=head2 dtmf_quick_jump
Ask the user once if he wants to jump somewhere
Assumes press 1 to go there
=over 4
=item Args:
$jump_fn: The function to jump to/to be called when the user presses 1
$prompt: The prompt to be asking the user whether they want to jump somewhere
=back
=cut
sub dtmf_quick_jump {
my ($self, $jump_fn, $prompt) = @_;
$self->log(4, "First Jump");
my %words = ('prompt' => $prompt);
my %dispatch = (1 => $jump_fn,
'max_prompts' => 1);
my $res = &dtmf_dispatch ($self, \%words, \%dispatch, '1*');
$self->log (4, "dtmf_quick_jump res $res");
return $res;
}
######################################################################
sub dtmf_dispatch_static {
my ($self, $words, $dispatch, $digits) = @_;
while (1) {
my $ret = &dtmf_dispatch ($self, $words, $dispatch, $digits);
if ($ret eq 'hangup' || $ret eq 'cancel' || $ret eq 'timeout') { return $ret; }
}
}
######################################################################
sub dtmf_dispatch {
my ($self, $words, $dispatch, $digits) = @_;
die if (!defined($words));
die if (!defined($words->{'prompt'}));
die if (!defined($dispatch));
die if ($digits !~ /\*/);
my @prompt_list = ();
my $last_prompt = $words->{'prompt'};
if (ref($words->{'prompt'}) eq "ARRAY") {
print STDERR ("dtmf_dispatch prompt is array\n");
my @prompts = @{$words->{'prompt'}};
my $p = 0;
for (; $p < $#prompts; $p++) {
push (@prompt_list, $prompts[$p]);
#print STDERR ("prompt_list gets ".$prompt->[$p]."\n");
}
$last_prompt = $prompts[$p];
print STDERR ("last prompt is ".$prompts[$p]."\n");
}
# words->pre = say first time through
# words->prompt = tell user what to press for what action
# words->post = say if no input was given
my $max_prompts = $MAX_DTMF_PROMPTS;
if (defined($dispatch->{max_prompts})) {
$max_prompts = $dispatch->{max_prompts};
}
$self->log (4, "starting dtmf_dispatch digits $digits");
my $dtmf = 0;
if (defined($words->{'pre'})) {
$dtmf = $self->agi->stream_file
($words->{'pre'}, "$digits", "0");
}
$self->log (4, "Wilting: $max_prompts : $dtmf");
for (my $i = 0; $i < $max_prompts && defined($dtmf) && $dtmf == 0; $i++) {
if (&user_has_hungup($self)) { return 'hangup'; }
#print STDERR ("A dtmf $dtmf\n");
if ($#prompt_list >= 0) {
foreach (my $p = 0; $p <= $#prompt_list &&
defined($dtmf) && $dtmf == 0; $p++) {
$dtmf =
$self->agi->stream_file($prompt_list[$p], "$digits", "0");
}
}
if (defined($dtmf) && $dtmf == 0) {
$dtmf = $self->agi->get_option
($last_prompt,"$digits", $DEFAULT_TIMEOUT);
}
#print STDERR ("B dtmf $dtmf\n");
# say something on timeout if we have something to say
if ($dtmf == 0 && defined($words->{'post'})) {
$dtmf = $self->agi->get_option($words->{'post'},
"$digits", $DEFAULT_TIMEOUT);
#print STDERR ("C dtmf $dtmf\n");
}
#print STDERR ("dtmf loop $i dtmf $dtmf\n");
}
#print STDERR ("dtmf_dispatch ended for loop dtmf $dtmf\n");
# user did not give any input in time alloted
if (!defined($dtmf) || $dtmf < 0) {
$self->log (3, "user hung up");
return 'hangup';
}
if ($dtmf == 0) {
$self->log (3, "timed out");
return 'timeout';
}
my $input = chr($dtmf);
$self->log (3, "post-loop input $input");
if ($input ne '*') {
#$self->log (3, "jump to fn?");
die if (!defined($dispatch->{$input}));
#$self->log (3, "yes: jump to fn");
my $function = $dispatch->{$input};
my $ret = $function->($self);
if (!defined($ret)) {
$self->log (4, "func returned undef");
return 'ok';
} else {
$self->log (4, "func returned $ret");
}
return $ret;
} else {
$self->log (3, "user entered *");
return 'cancel';
}
# we will return here after running the dispatch function
return 'ok';
}
######################################################################
sub dtmf_dispatch_dynamic {
my ($self, $dynamic_prompt_fn) = @_;
while (1) {
my %word_hash = ();
my %dispatch_hash = ();
my $digits = '';
my $words = \%word_hash;
my $dispatch = \%dispatch_hash;
&$dynamic_prompt_fn ($self, $words, $dispatch, \$digits);
my $ret = &dtmf_dispatch ($self, $words, $dispatch, $digits);
$self->log (4, "dtmf_dispatch ret $ret");
if ($ret eq 'hangup') { return $ret; }
}
}
######################################################################
=head2 get_yes_no_option
Gives the user an option in the form "For yes press 1, for no press 2".
=over 4
=item Args:
$prompt: The prompt that the user needs to respond yes or not to
=back
=cut
sub get_yes_no_option {
my ($self, $prompt) = @_;
# return 'no' for no
# return 'yes' for yes
# return 'timeout' for timeout or hangup
die if (!defined($prompt));
my $dtmf = 0;
my $input = -1;
my @prompt_list = ();
my $last_prompt = $prompt;
if ($prompt eq "ARRAY") {
my @prompts = @{$prompt};
my $p = 0;
for (; $p < $#prompts; $p++) {
push (@prompt_list, $prompts[$p]);
}
$last_prompt = $prompts[$p];
}
$self->log (4, "starting get_yes_no_option");
for (my $i = 0; $i < $MAX_DTMF_PROMPTS && $dtmf == 0; $i++) {
#$self->log (4, "A dtmf loop $i dtmf $dtmf\n");
if ($#prompt_list >= 0) {
foreach (my $p = 0; $p <= $#prompt_list &&
defined($dtmf) && $dtmf == 0; $p++) {
$dtmf =
$self->agi->stream_file($prompt_list[$p], "12*", "0");
}
}
if (defined($dtmf) && $dtmf == 0) {
$dtmf = $self->agi->get_option
($prompt, "12*", 250);
}
#$self->log (4, "B dtmf loop $i dtmf $dtmf\n");
if (defined($dtmf) && $dtmf == 0) {
$dtmf = $self->agi->get_option
(&msg($self,'yes-press-one-no-press-two'),
"12*", $DEFAULT_TIMEOUT);
}
#$self->log (4, "B dtmf loop $i dtmf $dtmf\n");
}
# user did not give any input in time alloted
if (!defined($dtmf) || $dtmf < 0) {
$self->log (3, "user hung up");
return 'hangup';
}
if ($dtmf == 0) {
$self->log (3, "timed out");
return 'timeout';
}
$self->log (4, "ended for loop dtmf $dtmf\n");
$input = chr($dtmf);
$self->log (3, "post-loop input $input");
if ($input eq '1') {
return 'yes';
} elsif ($input eq '2') {
return 'no';
} elsif ($input eq '*') {
return 'cancel';
}
die ("Should not be reached");
}
######################################################################
=head2 get_large_number
Allows the user to input a large multi-digit number using the phones keypad.
but unlike L<Nokia::Tools::get_unchecked_large_number>, it plays it back to the user for confirmation.
=over 4
=item Args:
$prompt: The prompt asking the user to enter a large number
=back
=cut
sub get_large_number {
my ($self, $prompt) = @_;
my $number = &get_and_check_number ($self, $prompt, "0", 1);
$self->log (4, "get_large_number returning $number");
return $number;
}
######################################################################
=head2 get_unchecked_large_number
Allows the user to input a large multi-digit number using the phones keypad
=over 4
=item Args:
$prompt: The prompt asking a user to enter a number
$number_list: Reference to the array that will hold the large number. Each
number input is stored as a separate array element
=back
=cut
sub get_unchecked_large_number {
my ($self, $prompt, $number_list) = @_;
for (my $p = 0; $p < $MAX_DTMF_PROMPTS; $p++) {
my $loop = 0;
my $number = '';
my $timeout = 0;
while (!$timeout) {
my $dtmf = 0;
if ($loop == 0) {
my $allDigits = "0123456789*#";
if (ref($prompt) eq "ARRAY") {
# play the first prompts only the first time through
if ($p == 0) {
for (my $s = 0; defined($dtmf) && $dtmf == 0 && $s < $#$prompt; $s++) {
$dtmf = $self->agi->stream_file ($prompt->[$s],$allDigits,"0"),
}
}
# play the last prompt everytime (including after timeouts)
# unless the user has already cut us off
if (defined($dtmf) && $dtmf == 0) {
$dtmf = $self->agi->stream_file ($prompt->[$#$prompt],$allDigits,"0"),
}
} else {
# if there's only one prompt, just play it everytime
$dtmf = $self->agi->stream_file ($prompt,$allDigits,"0"),
}
}
if (defined($dtmf) && $dtmf == 0) {
# wait a little after we've played a prompt for the user to enter something
# if he hasn't entered anything already
$dtmf = $self->agi->wait_for_digit(3000);
}
if (!defined($dtmf) || $dtmf < 0) {
return 'hangup';
} elsif ($dtmf > 0) {
my $input = chr($dtmf);
#print STDERR "got input $input\n";
if ($input eq '*') {
return 'cancel';
} elsif ($input eq '#') {
if ($number eq '') {
return 'cancel';
}
return $number;
} else {
$number .= $input;
push (@$number_list, $input);
}
} else {
# timed out, did user enter any digits?
if (length($number) > 0) {
return $number;
} else {
# prompt the user again
$timeout = 1;
}
}
$loop++;
}
}
return 'timeout';
}
######################################################################
=head2 get_small_number
Allows the user to input a single-digit number using the phones keypad
but unlike L<Tools::get_unchecked_small_number>, it plays it back to the user for confirmation.
=over 4
=item Args:
$prompt: The prompt asking a user to input a number
$digits: A string of valid digits that the user is allowed to key in
=back
=cut
sub get_small_number {
my ($self, $prompt, $digits) = @_;
return &get_and_check_number ($self, $prompt, $digits, 0);
}
######################################################################
=head2 get_unchecked_large_number
Allows the user to input a single-digit number using the phones keypad.
=over 4
=item Args:
$prompt: The prompt asking a user to enter a number
$digits: A string of valid digits that the user is allowed to key in
=back
=cut
sub get_unchecked_small_number {
my ($self, $prompt, $digits) = @_;
my $dtmf = &get_dtmf_input ($self, $prompt, $digits);
if ($dtmf =~ /^\d+$/ && $dtmf > 0) {
my $number = chr ($dtmf);
return $number;
}
# This can return strings
return $dtmf;
}
######################################################################
sub get_and_check_number {
my ($self, $prompt, $valid_numbers, $is_large_number) = @_;
$self->log (4, "starting get_and_check_number");
my $number = 0;
for (my $i = 0; $i < $MAX_DTMF_PROMPTS &&
defined($number) && $number >= 0; $i++) {
my @number_list = ();
if ($is_large_number) {
# returns 0, 1, ...
# or -1 on error
$number = &get_unchecked_large_number ($self, $prompt, \@number_list);
$self->log (4, "in get_and_check_number, get_unchecked_large_number returned $number");
} else {
$number = &get_unchecked_small_number ($self, $prompt, $valid_numbers);
$self->log (4, "in get_and_check_number, get_unchecked_small_number returned $number");
$number_list[0] = $number;
}
$self->log (4, "get_dtmf_input returned number $number");
if ($number eq 'timeout' || $number eq 'hangup' || $number eq 'cancel') {
return $number;
}
if ($number >= 0) {
my $input = 0;
my $digits = '12*';
# If user enters 2, we break out of here and
# let him enter a new number.
for (my $j = 0; $j < $MAX_DTMF_PROMPTS && $input ne '2'; $j++) {
my $dtmf = &stream_file($self,'you-entered', "$digits", "0");
if (defined($dtmf)) {
$self->log (4, "after you_entered dtmf $dtmf");
} else {
$self->log (4, "after you_entered dtmf NULL");
}
if ($dtmf == 0) {
#my @number_as_array = ();
for (my $n = 0; $n <= $#number_list && $dtmf == 0; $n++) {
$dtmf = &stream_file ($self,"digits/$number_list[$n]", "$digits", "0");
$self->log (4, "saying number $n dtmf $dtmf");
}
$self->log (4, "after say_number dtmf $dtmf");
if ($dtmf == 0) {
$dtmf = $self->agi->get_option
(&msg($self,'if-correct'), "$digits", $DEFAULT_TIMEOUT);
$self->log (4, "after get_option dtmf $dtmf");
}
}
$self->log (4, "before first return dtmf $dtmf");
if (!defined($dtmf) || $dtmf < 0) {
$self->log (4, "user hungup get_and_check_number");
return 'hangup';
} elsif ($dtmf == 0) {
$self->log (4, "user timed out get_and_check_number");
return 'timeout';
}
$input = chr ($dtmf);
if ($input eq '1') {
$self->log (4, "get_and_check_number user picked $number");
return $number;
} elsif ($input eq '*') {
$self->log (4, "user quit get_and_check_number");
return 'cancel';
}
}
}
}
$self->log (4, "leaving get_and_check_number");
return 'timeout';
}
######################################################################
=head2 get_dtmf_input
Allows the user to provide input using the phones keypad. It can be numbers
or special characters like * or #
=over 4
=item Args:
$prompt: The prompt asking the user to key in a number
$digit: A string of the valid set of digits the user is allowed to key in
=back
=cut
sub get_dtmf_input {
my ($self, $prompt, $digits) = @_;
# returns number input
# prompt can be either a ref to an array of prompts or just the one prompt
# do the right thing in either case
# get_option only takes one file to stream (contrary to the documentation).
# stream_file does not take a timeout.
$self->log (4, "starting get_dtmf_number");
die ("No prompts defined") if (!defined($prompt));
my $dtmf = 0;
my $input = -1;
my @prompt_list = ();
my $last_prompt = $prompt;
if (ref($prompt) eq "ARRAY") {
#print STDERR ("prompt is array\n");
my $p = 0;
for (; $p < $#$prompt; $p++) {
push (@prompt_list, $prompt->[$p]);
#print STDERR ("prompt_list gets ".$prompt->[$p]."\n");
}
$last_prompt = $prompt->[$p];
print STDERR ("last prompt is ".$prompt->[$p]."\n");
}
# Note that there is no advantage in sending in the sound files
# in [x,y,z] format because
# the underlying command does the looping anyway.
for (my $i = 0; $i < $MAX_DTMF_PROMPTS && defined($dtmf) && $dtmf == 0; $i++) {
# If we have a series of prompts, loop through them first
if ($#prompt_list >= 0) {
foreach (my $p = 0; $p <= $#prompt_list &&
defined($dtmf) && $dtmf == 0; $p++) {
$dtmf =
$self->agi->stream_file($prompt_list[$p], "$digits", "0");
}
}
#print STDERR ("A dtmf loop $i dtmf $dtmf\n");
# With the last prompt, wait a bit for input
# before we start looping again
if (defined($dtmf) && $dtmf == 0) {
$dtmf =
$self->agi->get_option
($last_prompt,"$digits", $DEFAULT_TIMEOUT);
#print STDERR ("B dtmf loop $i dtmf $dtmf\n");
}
}
#print STDERR ("ended for loop dtmf $dtmf\n");
# user did not give any input in time alloted
if (!defined($dtmf) || $dtmf < 0) {
$self->log (3, "user hung up");
return 'hangup';
}
if ($dtmf == 0) {
$self->log (3, "timed out");
return 'timeout';
}
$input = chr($dtmf);
if ($input eq '*') {
return 'cancel';
}
return $dtmf;
}
######################################################################
sub speech_or_dtmf_input {
my ($self, $prompt, $grammar_file, $digits) = @_;
# since we are going to keep all of these utterances for safe keeping
# we'd better put them in a daily directory
my $file_prefix = '/data/utter/'.&year_month_day_dir.'/'.&rnd_alphanum ($HASH_FILE_LENGTH);
my $beep = 0;
my $res = $self->agi->record_file ($file_prefix, 'wav', "$digits", 5000,'0', $beep, 1500);
$self->log (3, "record file $res");
if ($res > 0) {
return chr($res);
} elsif ($res < 0) {
# error condition
return '-1';
}
# take the recorded file and send it to the recognizer
# TODO make more efficient with pipes, fifo, locks, etc.
my $wav_file = $file_prefix.'.wav';
my $sr_config_file = $file_prefix.'.cfg';
my $sr_in_file = $file_prefix.'.srin';
my $sr_out_file = $file_prefix.'.srout';
open CFG, ">$sr_config_file" or die ("Cannot open sr config file $sr_config_file");
print CFG "[FILES]\n";
print CFG "VocabularyFile=$grammar_file\n";
print CFG "MsgInFile=$sr_in_file\n";
print CFG "ModelsFile=$self->{language_model}\n";
print CFG "MsgOutFile=$sr_out_file\n";
# okay. a bit ugly.
print CFG "[MODE]\nDensFrameRate=1\nFeatureMode=FILE_SAMPLES\nTimeOutEnd=4000\n";
print CFG "PronunciationSize=1000\nDensTopRate=10000\nUTF8EncodedChar=1\n";
print CFG "KeyPress=-1\nLidNbest=4\nTimeOutMin=1000\nNoiseSnr=5\nNoiseSnrRange=20\n";
print CFG "NBest=5\n\n[LANG]\n";
close CFG;
my $default_in_lab = $ENV{NASI_HOME}.'/sr/in.lab';
open MSGIN, ">$sr_in_file" or die ("Cannot open sr in file $sr_in_file");
print MSGIN "msg_RECOGNITION_STATE in $wav_file lab $default_in_lab res foo usr bar\n";
close MSGIN;
my $cmd = $ENV{NASI_HOME}."/sr/sr $sr_config_file";
$self->log (3, "running $cmd");
# TODO check that this ran ok?
system ($cmd);
open MSGOUT, "$sr_out_file" or die ("Cannot open sr out file $sr_out_file");
my $score = 0;
my $keyword = '';
while (my $line = <MSGOUT>) {
# Result 1 score 877.99 [acc] alexander_cooper
if ($line =~ /Result\s+1\s+/) {
my (@res) = split (/\s+/,$line);
$score = $res[3];
$keyword = $res[5];
}
}
close MSGOUT;
$self->log (3, "score $score keyword $keyword");
return $keyword;
}
######################################################################
sub request_attendant {
my ($self) = @_;
my $number = &get_unchecked_small_number
($self,
&msg($self,'please-press-0-to-connect-to-an-operator-or-hang-up-to-end-the-call'),
'0');
if ($number == 0) {
$self->agi->set_variable("ATTENDANT","connect");
}
}
######################################################################
sub get_max_results_to_listen_to {
return $MAX_RESULTS_TO_LISTEN_TO;
}
######################################################################
sub tell_user_message {
my ($self, $msg) = @_;
my $dtmf = $self->agi->stream_file
($msg, "*#", "0");
$self->agi->wait_for_digit (1500);
}
######################################################################
=head2 record_file
Cleanly record a file to the temp filesystem, letting the user play it back
and re-record it. Message should be something like: record your X at the beep
Returns the relative pathname of the file if successful (relative to the
temp filesystem, which is kept in memory).
=over 4
=item Args:
$instructions_prompt: The prompt that
$length_in_seconds: The maximum time the user is given for the recording
$redo_prompt: The prompt asking the user whether they want to re-record
$delete_on_hangup: Delete the file if the user cancels or hangs up (default is no)
=back
=cut
sub record_file {
my ($self, $instructions_prompt, $length_in_seconds,
$redo_prompt, $delete_on_hangup) = @_;
$self->log (4, "start record_file");
if (!defined ($redo_prompt)) {
$redo_prompt = &msg($self,'to-keep-your-recording');
}
if (!defined($delete_on_hangup)) {
$delete_on_hangup = 1;
}
my $hash = &rnd_alphanum ($HASH_FILE_LENGTH);
$self->log (4, "record_file rnd alphanum $hash");
my $SECONDS_OF_SILENCE = 3;
my $tmp_record_file_no_codec = $tmp_rec_dir.$hash;
my $tmp_record_file_w_codec = $tmp_rec_dir.$hash.'.'.&codec();
#my $tmp_record_file_wav_codec = $tmp_rec_dir.$hash.'.wav';
my $tmp_record_file_sln_codec = $tmp_rec_dir.$hash.'.sln';
my $res = '';
my $silence_loop = 0;
while ($res eq '') {
my $dtmf = $self->agi->stream_file ($instructions_prompt, "*#","0");
&unlink_file ($self, $tmp_record_file_w_codec);
$self->log (4, "about to record to $tmp_record_file_w_codec");
# record the file in wav format
# but this is converted to gsm
# by the time we have cleaned up the file
if (defined($dtmf) && $dtmf >= 0 && chr($dtmf) ne '*') {