Wednesday, May 19, 2010

Asterisk Perl AGI

Below is the sample AGI scrip written in per perl.
In This Script You will learn the following
1. How to write AGI concept.
2. Some Perl Programming.(Variables, Arithmetic Operator, IF/ELSE/ELSIF , comparision operators, Labels etc.
3. How to use mysql queries in AGI using perl.
4. Asterisk Application Used( NoOp, SayDigits, SayNumber, Channel Variables, SayUnixTimeStamp etc)

#!/usr/bin/perl
# MODULE
use Time::Local;
use Asterisk::AGI;
use Mysql;

# CONFIG VARIABLES
$host = "localhost";
$database = "testdb";
$user = "root";
$pw = "JustForFun";

# MYSQL CONNECT
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT A DATABASE
$connect->selectdb($database);

# create agi instance
$AGI = new Asterisk::AGI;

$AGI->exec("NoOP","\----OnlineInquirySystem----\"");
$AGI->exec("Playback","custom/inquiry-thankyou");
Repeapt1:
$AGI->exec("Read","\option,custom/inquiry-tocheck,1,,,\"");
$option = $AGI->get_variable(option);

if ($option == 1 or $option == 2)
{
    EnterCheque:
    $AGI->exec("Read","cheque,custom/inquiry-pleaseenter,15,,,");
    $cheque = $AGI->get_variable(cheque);
    AfterCheque:
    $AGI->exec("NoOP","**$cheque**");
    if (length($cheque) == 10)
    {
    $AGI->exec("Playback","custom/inquiry-waitforinformation");
    $query="Select evttype_fk, issue_dtt,amount,inst_pk from lpsinst where check_no='$cheque'";
    $execute= $connect->query($query);
    @results=$execute->fetchrow();
    $AGI->exec("NoOP","=====================================");
    $AGI->exec("NoOP","**$results[0]**");
    $AGI->exec("NoOP","=====================================");
    if ($results[0] > 0)
    {
    $AGI->exec("NoOP","**CheckFound**");
    $evttype=$results[0];
    $issuedtt=$results[1];
    $amount=$results[2];
    $inst_pk=$results[3];
    $issueyear=substr $issuedtt,0,4;
    $issuemonth=substr $issuedtt,5,2;
    $issueday=substr $issuedtt,8,2;
    $issuehour=substr $issuedtt,11,2;
    $issueminute=substr $issuedtt,14,2;
    $issuesecond=substr $issuedtt,17,2;
    $AGI->exec("NoOP","$issuedtt");
    $AGI->exec("NoOP","$issueyear-$issuemonth-$issueday-$issuehour:$issueminute:$issuesecond");
    $issueutc=timelocal($issuesecond,$issueminute,$issuehour,$issueday,$issuemonth,$issueyear);
    $AGI->exec("NoOP","$issueutc");
    $bfd=length($amount) - 3;
        $afd= $bfd + 1;
        $dollars=substr $amount,0,$bfd;
        $cents=substr $amount,$afd,2;
    $AGI->exec("NoOP","D-$dollars");
    $AGI->exec("NoOP","C-$cents");
  
        if ($evttype == 3)
        {
        $AGI->exec("Playback","custom/inquiry-chequenumber");
        $AGI->exec("SayDigits","$cheque");
        $AGI->exec("Playback","custom/inquiry-wasissuedon");
        $AGI->exec("SayUnixTime","$issueutc,CET,BdY");
        $AGI->exec("Playback","custom/inquiry-amountof");
        $AGI->exec("SayNumber","$dollars");
        $AGI->exec("Playback","dollars");
        $AGI->exec("SayNumber","$cents");
        $AGI->exec("Playback","cents");
        $AGI->exec("PlayBack","custom/inquiry-nofurtherinfo");
        SecondOption:
                $AGI->exec("Read","\option,custom/inquiry-tocheck-2,1,,,\"");
                $option = $AGI->get_variable(option);
            if ($option == 1 or $option == 2)
                {
                goto Repeapt1;
                } elsif ($option == 3)
                    {
                    goto TalkToSupport;
                    } elsif($option == 4)
                        {
                        goto SecondOption;  
                        } elsif($option == 5)
                                                    {
                            EndCall:
                            $AGI->exec("Playback","custom/inquiry-thankforcalling");
                                                    goto END;
                                                    } else
                                {
                                $AGI->exec("Playback","custom/inquiry-invalidoption");
                                goto SecondOption;
                                }
      
      
        } elsif ($evttype == 4)
        {
        $query="select evt_dtt from lpshistory where inst_fk='$inst_pk' and evttype_fk=4";
            $execute= $connect->query($query);
            @results=$execute->fetchrow();
            $evtdtt=$results[0];
        $evtyear=substr $evtdtt,0,4;
            $evtmonth=substr $evtdtt,5,2;
            $evtday=substr $evtdtt,8,2;
            $evthour=substr $evtdtt,11,2;
            $evtminute=substr $evtdtt,14,2;
            $evtsecond=substr $evtdtt,17,2;
            $AGI->exec("NoOP","$issuedtt");
            $AGI->exec("NoOP","$evtsecond-$evtminute-$evthour-$evtday-$evtmonth-$evtyear");
            $evtutc=timelocal($evtsecond,$evtminute,$evthour,$evtday,$evtmonth,$evtyear);
        $AGI->exec("Playback","custom/inquiry-chequenumber");
                $AGI->exec("SayDigits","$cheque");
                $AGI->exec("Playback","custom/inquiry-wasissuedon");
                $AGI->exec("SayUnixTime","$issueutc,CET,BdY");
                $AGI->exec("Playback","custom/inquiry-amountof");
                $AGI->exec("SayNumber","$dollars");
                $AGI->exec("Playback","dollars");
                $AGI->exec("SayNumber","$cents");
                $AGI->exec("Playback","cents");
        $AGI->exec("Playback","custom/inquiry-wasvoidedon");
        $AGI->exec("SayUnixTime","$evtutc,CET,BdY");
        goto SecondOption;
        } elsif ($evttype == 5)
                {
                $query="select evt_dtt from lpshistory where inst_fk='$inst_pk' and evttype_fk=5";
                $execute= $connect->query($query);
                @results=$execute->fetchrow();
                $evtdtt=$results[0];
                $evtyear=substr $evtdtt,0,4;
                $evtmonth=substr $evtdtt,5,2;
                $evtday=substr $evtdtt,8,2;
                $evthour=substr $evtdtt,11,2;
                $evtminute=substr $evtdtt,14,2;
                $evtsecond=substr $evtdtt,17,2;
                $AGI->exec("NoOP","$issuedtt");
                $AGI->exec("NoOP","$evtsecond-$evtminute-$evthour-$evtday-$evtmonth-$evtyear");
                $evtutc=timelocal($evtsecond,$evtminute,$evthour,$evtday,$evtmonth,$evtyear);
                $AGI->exec("Playback","custom/inquiry-chequenumber");
                $AGI->exec("SayDigits","$cheque");
                $AGI->exec("Playback","custom/inquiry-wasissuedon");
                $AGI->exec("SayUnixTime","$issueutc,CET,BdY");
                $AGI->exec("Playback","custom/inquiry-amountof");
                $AGI->exec("SayNumber","$dollars");
                $AGI->exec("Playback","dollars");
                $AGI->exec("SayNumber","$cents");
                $AGI->exec("Playback","cents");
                $AGI->exec("Playback","custom/inquiry-wasredeemedon");
                $AGI->exec("SayUnixTime","$evtutc,CET,BdY");
        goto SecondOption;
                } elsif ($evttype == 6)
        {
        $query="select evt_dtt from lpshistory where inst_fk='$inst_pk' and evttype_fk=6";
                $execute= $connect->query($query);
                @results=$execute->fetchrow();
                $evtdtt=$results[0];
                $evtyear=substr $evtdtt,0,4;
                $evtmonth=substr $evtdtt,5,2;
                $evtday=substr $evtdtt,8,2;
                $evthour=substr $evtdtt,11,2;
                $evtminute=substr $evtdtt,14,2;
                $evtsecond=substr $evtdtt,17,2;
                $AGI->exec("NoOP","$issuedtt");
                $AGI->exec("NoOP","$evtsecond-$evtminute-$evthour-$evtday-$evtmonth-$evtyear");
                $evtutc=timelocal($evtsecond,$evtminute,$evthour,$evtday,$evtmonth,$evtyear);
                $AGI->exec("Playback","custom/inquiry-chequenumber");
                $AGI->exec("SayDigits","$cheque");
                $AGI->exec("Playback","custom/inquiry-wasissuedon");
                $AGI->exec("SayUnixTime","$issueutc,CET,BdY");
                $AGI->exec("Playback","custom/inquiry-amountof");
                $AGI->exec("SayNumber","$dollars");
                $AGI->exec("Playback","dollars");
                $AGI->exec("SayNumber","$cents");
                $AGI->exec("Playback","cents");
                $AGI->exec("Playback","custom/inquiry-wasstopedon");
                $AGI->exec("SayUnixTime","$evtutc,CET,BdY");
        goto SecondOption;
                } elsif ($evttype == 7)
                {
        $query="select evt_dtt from lpshistory where inst_fk='$inst_pk' and evttype_fk=7";
                $execute= $connect->query($query);
                @results=$execute->fetchrow();
                $evtdtt=$results[0];
                $evtyear=substr $evtdtt,0,4;
                $evtmonth=substr $evtdtt,5,2;
                $evtday=substr $evtdtt,8,2;
                $evthour=substr $evtdtt,11,2;
                $evtminute=substr $evtdtt,14,2;
                $evtsecond=substr $evtdtt,17,2;
                $AGI->exec("NoOP","$issuedtt");
                $AGI->exec("NoOP","$evtsecond-$evtminute-$evthour-$evtday-$evtmonth-$evtyear");
                $evtutc=timelocal($evtsecond,$evtminute,$evthour,$evtday,$evtmonth,$evtyear);
                $AGI->exec("Playback","custom/inquiry-chequenumber");
                $AGI->exec("SayDigits","$cheque");
                $AGI->exec("Playback","custom/inquiry-wasissuedon");
                $AGI->exec("SayUnixTime","$issueutc,CET,BdY");
                $AGI->exec("Playback","custom/inquiry-amountof");
                $AGI->exec("SayNumber","$dollars");
                $AGI->exec("Playback","dollars");
                $AGI->exec("SayNumber","$cents");
                $AGI->exec("Playback","cents");
                $AGI->exec("Playback","custom/inquiry-wasstopedandvoidedon");
                $AGI->exec("SayUnixTime","$evtutc,CET,BdY");
        goto SecondOption;
                } elsif ($evttype == 11 or $evttype == 12)
                {
        $query="select evt_dtt from lpshistory where inst_fk='$inst_pk' and evttype_fk=11 or evttype_fk=12";
                $execute= $connect->query($query);
                @results=$execute->fetchrow();
                $evtdtt=$results[0];
                $evtyear=substr $evtdtt,0,4;
                $evtmonth=substr $evtdtt,5,2;
                $evtday=substr $evtdtt,8,2;
                $evthour=substr $evtdtt,11,2;
                $evtminute=substr $evtdtt,14,2;
                $evtsecond=substr $evtdtt,17,2;
                $AGI->exec("NoOP","$issuedtt");
                $AGI->exec("NoOP","$evtsecond-$evtminute-$evthour-$evtday-$evtmonth-$evtyear");
                $evtutc=timelocal($evtsecond,$evtminute,$evthour,$evtday,$evtmonth,$evtyear);
                $AGI->exec("Playback","custom/inquiry-chequenumber");
                $AGI->exec("SayDigits","$cheque");
                $AGI->exec("Playback","custom/inquiry-wasissuedon");
                $AGI->exec("SayUnixTime","$issueutc,CET,BdY");
                $AGI->exec("Playback","custom/inquiry-amountof");
                $AGI->exec("SayNumber","$dollars");
                $AGI->exec("Playback","dollars");
                $AGI->exec("SayNumber","$cents");
                $AGI->exec("Playback","cents");
                $AGI->exec("Playback","custom/inquiry-wasescheatedon");
                $AGI->exec("SayUnixTime","$evtutc,CET,BdY");
        goto SecondOption;
                } else
        {
        $AGI->exec("Playback","custom/inquiry-chequenumber");
        $AGI->exec("SayDigits","$cheque");
        $AGI->exec("NoOP","**$cheque**");
        NoValidStatus:
        $AGI->exec("Read","\option,custom/inquiry-wasnotfoundtry,1,,,\"");
        $option = $AGI->get_variable(option);
        if ($option == 1)
            {
            goto EnterCheque;
            } elsif ($option == 2)
            {
            goto TalkToSupport;
            }  elsif ($option == 3)
                        {
                        goto EndCall;
                        } else
            {
            $AGI->exec("Playback","custom/inquiry-invalidoption");
            goto  NoValidStatus;
            }
      
        }

    } else
            {
        $AGI->exec("NoOP","**====CheckNotFound====**");
        $AGI->exec("Playback","custom/inquiry-chequenumber");
            $AGI->exec("SayDigits","$cheque");
            $AGI->exec("NoOP","**$cheque**");
            RepeatNotFound:
            $AGI->exec("Read","option,custom/inquiry-wasnotfoundtry,1,,,");
            $option = $AGI->get_variable(option);
            if ($option == 1)
            {
                goto EnterCheque;
                } elsif ($option == 2)
                    {
                        goto TalkToSupport;
                        }  elsif ($option == 3)
                            {
                            goto EndCall;
                            } else
                                {
                            $AGI->exec("Playback","custom/inquiry-invalidoption");
                            goto  RepeatNotFound;
                                }

  
        }

    } else
    {
    $AGI->exec("Read","cheque,custom/inquiry-rentercheque,15,,,");
    $cheque = $AGI->get_variable(cheque);
    goto AfterCheque;
    }

} elsif ($option == 3)
    {
    TalkToSupport:
    $AGI->exec("Playback","custom/inquiry-pleasehold");
    goto END;
    } elsif ($option == 4)
        {
        goto Repeapt1;
        } else
            {
            $AGI->exec("Playback","custom/inquiry-invalidoption");
            goto Repeapt1;
            }
      
END:

No comments:

Post a Comment