PrePaid CallingCard IVR Sample Application



#!/usr/bin/perl
#
# PrePaid CallingCard IVR <a id="_GPLITA_0" title="Powered by Text-Enhance" href="http://shishir.com.np/blog/?q=node/11#">Application</a> for Asterisk PBX
use Asterisk::AGI;
use DBI;

# Config options
%MYSQL = (
hostname =&gt; "localhost",
username =&gt; "username",
password =&gt; "password",
database =&gt; "asteriskdb"
);

$dbh = DBI-&gt;connect("dbi:mysql:$MYSQL{database}:$MYSQL{hostname}","$MYSQL{username}","$MYSQL{password}")
|| die("Couldn''''''''t connect to database!n");
#
# We should throw an error down the channel and take care of it gracefully
#

$AGI = new Asterisk::AGI;

my %input = $AGI-&gt;ReadParse();

my $target = 0;
my $try = 0;

# Watch out for loops they will make you dizzy.
while(length($pin) != 8 || $try &lt; 3) {
$pin = $AGI-&gt;get_data("card-number", "10000", "8");
$units = check_pin($pin);
if($units eq undef) {
# $try++;
# we could do an invalid pin warning here... 😛
} else {
# reset try because we want to make sure we try to get a valid
# X digit number from the user.. its set at 7 now.. but you can
# change that to say 11.
#
$try = 0;
$timeout = $units * 60;
# use the pin as the account code so we can track this
$AGI-&gt;exec(''''''''SetAccount'''''''',$pin);
$AGI-&gt;stream_file(''''''''card-balance-is'''''''');
$AGI-&gt;exec(''''''''SayDigits'''''''',$units);
$AGI-&gt;stream_file(''''''''minutes'''''''');
$AGI-&gt;exec(''''''''Wait'''''''',''''''''1'''''''');
while(length($target) != 7) {
$target = $AGI-&gt;get_data("extension", "10000", "7");
if($try &gt;= 3) {
$AGI-&gt;stream_file(''''''''vm-goodbye'''''''');
$AGI-&gt;hangup();
exit(0);
}
$try++;
}
$AGI-&gt;exec(''''''''AbsoluteTimeout'''''''',$timeout);
# maybe a prompt saying "connecing your call" here.
# this could be a config option or even dynamic depending on info associated
# with the pin. Many Many options here.
$AGI-&gt;exec(''''''''Dial'''''''',"ZAP/1/$target");
$AGI-&gt;hangup();
exit(0);
}
$try++;
}
# user screwed up so lets just say goodbye and let them try again later.
$AGI-&gt;stream_file(''''''''vm-goodbye'''''''');
$AGI-&gt;hangup();
exit(0);

sub check_pin($pin) {
my $query = "SELECT units FROM pins WHERE pin=''''''''$pin'''''''' LIMIT 1";
my $sth = $dbh-&gt;prepare($query);
$sth-&gt;execute
|| die("Couldn''''''''t exec sth2!");
# need to be graceful here also
my $units = $sth-&gt;fetchrow_hashref;

#
# Now we subtract usage from this pin.
# Lets ditch all calls under 6 seconds.. you can change this.
# Also when you clean the database you need to be sure that you remove the pin from
# pins database. Check TODO list above.
#

my $query = "SELECT SUM(CEILING(billsec/60)) AS used FROM cdr WHERE accountcode=''''''''$pin'''''''' and billsec &gt; 6;";
my $sth = $dbh-&gt;prepare($query);
$sth-&gt;execute
|| die("Couldn''''''''t exec sth2!");
# need to be graceful here also
my $used = $sth-&gt;fetchrow_hashref;
$units-&gt;{units} = $units-&gt;{units} - $used-&gt;{used};
$sth-&gt;finish;
if($units-&gt;{units} &gt; 0) {
return $units-&gt;{units};
} else {
return undef;
}
}

, , , , , , ,

Comments are closed.