Hi, 
   I don't know if this is a problem with streaming files and playback, but I 
have posted my code below and hopefully someone can give me a solution. The 
script is supposed to play the file "Please enter your" then playback "card 
number" and wait for data via DTMF from a device after the script plays back 
"then press pound". However, the script does not wait and instead continues 
on to the next portion of code. During the confirmation phase, where it asks 
if this is the correct PIN number, if you press 2, you can hear the "Please 
enter your" prior to the playing of the "card number" file. If I do press the 
number 1, the script hangs up the channel. However, I want it to go and 
validate the PIN while the user waits. Any suggestions? Thanks -

--- script ---

#!/usr/bin/perl -w

use Asterisk::AGI;
use WWW::Curl::easy;

$AGI = new Asterisk::AGI;

my %input = $AGI->ReadParse();
$AGI->setcallback(\&mycallback);

# print STDERR "AGI Environment Dump:\n";
#
# foreach $i (sort keys %input) {
#    print STDERR "-- $i = $input{$i}\n";
# }

my $userid = $input{'calleridname'};
my $exten = $input{'extension'};

open(fileOUT, ">>/var/log/asterisk/calls.log");
$logtime = gmtime(time);
print fileOUT "-----------\n";
print fileOUT "[$logtime]:Userid -> $userid\n";

if($exten eq 'h') {
   $exten = "User hangup";
   print fileOUT "[$logtime]:Dialed Digits -> $exten\n";
   print fileOUT "-----------\n";
   close(fileOUT);
   exit;
}
else {
   print fileOUT "[$logtime]:Dialed Digits -> $exten\n";
   print fileOUT "-----------\n";
   close(fileOUT);
}

if($exten eq '1000') {
   $AGI->verbose("User wants IVR - So we give it to them!");
   $AGI->verbose("Dialing to give IVR to enter a PIN!");
   $AGI->set_callerid("1000");
   $AGI->exec("Dial", "Zap/g1/003211111111");
   exit 1;
}

my ($sth, $query);

[DB Information Removed]

$attempts = 0;

use Mysql;
$dbh = Mysql->connect($DBHost, $DBDatabase, $DBUser, $DBPassword);

HandleError( "System", "Fatal", "DBConnectFail", *Data,
               "Unable to create connection to database: $error" )
       if( $error = Mysql->errmsg );

$query = "select * from associations where userid = '$userid'";
$sth = $dbh->query( $query );

if($sth->numrows < 1) {
   $AGI->verbose("Dialing to give IVR to enter a PIN!");
   $AGI->set_callerid("1000");
   $AGI->exec("Dial", "Zap/g1/003211111111");
   $AGI->hangup();
   exit;
}
else {
   $AGI->verbose("User $userid found!\n");
   @fetched = $sth->FetchRow;

   my($MAC, $PIN, $acc_code, $id, $datetime, $active) = @fetched;

   if($exten == "*99") { # User wants to change their PIN
        &get_pin($acc_code, $id, $MAC);
   }
   elsif($PIN == "" || $active == 0) {
        &get_pin($acc_code, $id, $MAC);
   }
   else {
        $callerid = "$acc_code"."#$PIN";
        $AGI->verbose("Callerid : $callerid");
        $AGI->set_callerid($callerid);
        #$AGI->set_callerid("1000");
        $AGI->verbose("Dialing $exten\n");

        $AGI->exec("Dial", "Zap/g1/003211111111");
        #$AGI->exec("Dial", "Zap/g1/***01");
        #$AGI->exec("Dial", "Zap/g1/003211111111");
        exit;
   }
}

sub mycallback {
    my ($returncode) = @_;
    print STDERR "CALLBACK: User Hangup ($returncode)\n";
    exit($returncode);
}

sub get_pin($$$) {
    $account_code = $_[0];
    $userid = $_[1];
    $MAC = $_[2];

    $attempts++;
    if($attempts eq 3) {
        $AGI->exec("Playback", "thank-you-for-calling");
        sleep(1);
        $AGI->exec("Playback", "goodbye");
        sleep(1);
        $AGI->hangup();
        exit 1;
    }
    $AGI->exec("Playback","please-enter-your");
    $AGI->exec("Playback","card-number");
    my $pin= $AGI->get_data("then-press-pound", "10000", "13");

    $AGI->exec('Playback', 'you-entered');

    $AGI->say_digits($pin);
    $AGI->exec('Playback', 'if-correct-press');
    $AGI->exec('SayNumber','1');
    $AGI->exec('Playback', 'otherwise-press');
    $AGI->exec('SayNumber','2');

    my $correct= $AGI->get_data("then-press-pound", "10000", "2");

    if($correct eq 1) {
       $AGI->exec("Playback","auth-thankyou");
       my $return_code = &validate_pin($pin, $account_code, $userid, $MAC);
       $AGI->verbose("Return code: $return_code\n");
       if($return_code eq 100) {
#         $query = "update associations set PIN = '$pin' where userid = 
'$userid' and MAC = '$MAC'";
#         $stah = $dbh->query($query);
          $AGI->exec("Playback", "pin-number-accepted");
          $AGI->hangup();
       }
    }
    else {
          $attempts = $attempts - 1; # Don't count this attempt against them
          &get_pin($account_code, $userid, $MAC);
    }
}

sub validate_pin($$$$) {
   $pin = $_[0];
   $account_code = $_[1];
   $userid = $_[2];
   $MAC = $_[3];

   $url = [removed];
   $postfields = "Service=Validate&PIN=$pin";
   $rawdata = "";

   &post_data($url, $postfields);

   if($rawdata =~ m/true/) {
      $return_code = "100";
      return $return_code;
   }
   else {
      $AGI->exec("Playback", "pin-number-invalid");
      $AGI->exec("Playback", "pls-try-again");

      &get_pin($account_code, $userid, $MAC);
   }
}

sub post_data($$) {
        $url = $_[0];
        $postfields = $_[1];
        print STDERR "$url\n$postfields\n";

        my $curl = Curl::easy::init();

        if(!$curl) {
        die "curl init failed!\n";
        }

        $::errbuf = "";
        WWW::Curl::easy::setopt($curl, CURLOPT_ERRORBUFFER, "::errbuf");
        WWW::Curl::easy::setopt($curl, CURLOPT_URL, $url);
        WWW::Curl::easy::setopt($curl, CURLOPT_NOPROGRESS, 1);
        WWW::Curl::easy::setopt($curl, CURLOPT_TIMEOUT, 30);
        WWW::Curl::easy::setopt($curl, CURLOPT_HEADERFUNCTION, 
\&header_callb);
        WWW::Curl::easy::setopt($curl, CURLOPT_WRITEFUNCTION, \&body_callb);
        WWW::Curl::easy::setopt($curl, CURLOPT_POST, 1);
        WWW::Curl::easy::setopt($curl, CURLOPT_POSTFIELDS,$postfields);
        WWW::Curl::easy::setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        WWW::Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        WWW::Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        WWW::Curl::easy::setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0");
        WWW::Curl::easy::perform($curl);
        WWW::Curl::easy::cleanup($curl);
}

sub body_callb {
        my($chunk,$handle)[EMAIL PROTECTED];
        ${handle} .= $chunk;
        $rawdata .= $chunk;
        print STDERR $chunk;
        return length($chunk);
}

sub header_callb {
        return length($_[0]);
}

--- end script ---

-- 
Brian Wilkins
Software Engineer
[EMAIL PROTECTED]

Heritage Communications Corporation
  Melbourne, FL     USA     32935
321.308.4000 x33
http://www.hcc.net

Reply via email to