#!/usr/bin/perl use Asterisk::AGI; my $AGI = new Asterisk::AGI; %input = $AGI->ReadParse(); sub asr { use IO::Socket; use FileHandle; use IPC::Open2; my $file = shift or return undef; my $host = shift || 'localhost'; my $port = shift || '1069'; my $fh; my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "$host", PeerPort => "$port", ) or return undef; #Idea here being that you can pass a reference to an existing file handle... not yet implemented, just pass a filename. if (ref $file) { my $fh = $file; } else { open (FH, $file) || return undef; $fh = *FH; } $file =~ /(gsm|wav)$/; my $type = $1; if ($type !~ /gsm|wav/) { warn "Unknown file type ($file)"; return undef; } #print "FTYPE: $type\n"; $pid = open2(*SOXIN, *SOXOUT, "sox -t $type - -s -r 8000 -w -t wav - 2>/dev/null") || warn ("Could not open2.\n"); binmode $fh; binmode SOXIN; binmode SOXOUT; binmode $remote; while (defined(my $b = read $fh, my($buf), 4096)) { last if $b == 0; $count += $b; print SOXOUT $buf; } close SOXOUT; $count = 0; my $sox = undef; while (defined(my $b = read SOXIN, my($buf), 4096)) { last if $b == 0; $count += $b; $sox .= $buf; } print $remote length($sox) . "\n"; print $remote "$sox"; close SOXIN; #print "DEBUG: Waiting for result.\n"; $count=0; while (defined(my $b = read $remote, my($buf), 4096)) { last if $b == 0; $count += $b; $result .= $buf; } close $fh; close $remote; return "$result"; } sub confirm { while (my $tries <= 3) { $tries++; $AGI->stream_file("vr/say_yes_no",'""'); $AGI->stream_file("beep",'""'); $AGI->record_file("/tmp/$$", 'gsm', '0',3000); $AGI->stream_file("beep",'""'); #Here is where the magic happens, this is calling the asr sub from sphinx-netclient.pl #Again, this sub needs to be in this same script. $vresponse will contain the #transcription of the what the caler said. my $vresponse = asr("/tmp/$$.gsm"); $AGI->verbose("CONFIRM: $vresponse"); next if $vresponse !~ /YES|NO|ACCEPT|CANCEL/; $gotresp = 1; if ($vresponse =~ /NO|CANCEL/i) { sleep 1; $AGI->stream_file("cancelled",'""'); return undef; } else { $AGI->set_variable('RESPONSE', 'YES'); return 1; } } if (! $gotresp) { sleep 1; $AGI->stream_file("invalid_selection",'""'); return undef; } } $AGI->stream_file("vr/green_eggs_ham",'""'); unless ( confirm() ) { #They said no $AGI->set_variable('RESPONSE', 'NO'); exit; }