Skip to main content

Asterisk Gateway Interface - Perl

Asterisk Gateway Interface


1. What is Asterisk Gateway Interface? 


In simple word AGI is Language Independent API to programmers to control the call flow on their Asterisk PBXs.

Asterisk provides more than its own dial-plan, to control to the call flow or lets say call logics. So which means you may use either one of

  1. Dialplan
  2. Asterisk Manager Interface (AMI)
  3. Asterisk Gateway Interface (AGI)
to manipulate your call logics.

Before we move on to AGI lets briefly discuss about each one of above,

Dialplan

Dial plan is Asterisk native call logics performer, it's fast, easy to learn and efficient. But this configuration script is more closer to assembly program (If you have any previous experience on assembly), the main drawback of the Asterisk Dialplan in it's lack of support on standard procedural  language as an example when you want create a loop. 

Any way in the following tutorials we will only discuss about the AGI, But we can't avoid the fact that we need Dialplan to call out AGI script.

Asterisk Manager Interface (AMI)

What if you have a remote controller to your Asterisk PBX, it is AMI. It is more sophisticate with regards to Dialplan. So in essence you can control your PBX by using TCP socket. 

You can use a remote control tool to control your PBX but beware there are more security concerns would be followed up with this.

Asterisk Gateway Interface (AGI)

If you consider AMI and Dialplan regards to AGI, AGI should lay between them. AGI can't be completely independent, we have to get the support of the Dialplan, also you may use your desired language on AGI script. 

In this tutorial I'll use Perl on our AGI script...


But please note that if your PBX system is pure outbound AGI is not for you, AGI is only for inbound call  handling.

There are four type of AGI's you can find,
  • Standard AGI
It is the Simplest of all, and use standard inputs(STDIN) and outputs(STDOUT) to communicate with the PBX. In this tutorial are about to use this one. 
  • Dead AGI
Dead AGI is to handle the call logics after call hangup. Some AGI commands will not be able to use under this. 
  • Fast AGI
Using Fast AGI you can transfer your AGI processing burden to another server. You may use TCP sockets for the communications. It's Provide all the features in Standard AGI. 
  • EAGI
If Developers need to access/communicate beyond the STDIN and STDOUT which if they need to access the media channel, they may use EAGI.

2. Okay Now it's time to discuss our scenario,

I have two SIP peers call, peter and bob. We need them to able call with each other, and if one of them is not available, calling party should be able to drop a voice mail.
Later the other party may listen to that voice mail by accessing their voice mail.
By Dialing an extension with '*' (eg: *101) can listen their Voice Mail Boxes 

Create two sip peers for bob and peter

  • sip.conf
          [peter]             ;Peer name
          type=friend         ;Allow both incoming and outgoing 
          secret=123          ;Password for SIP peer
          host=dynamic        ;IP address of the SIP peer 
          allow=alaw,ulaw     ;Allowed coddecs
          context=users       ;Dialplan context for the SIP peer

          [bob]
          type=friend
          secret=123
          host=dynamic
          allow=alaw,ulaw
          context=users
         
Configure Dialplan to use our AGI script 
  • extensions.conf
   [users]
          exten => _[*0-9]X.,1,NoOp("Dialiing AGI") ; This will accept dial pattern which are starting from * or from a number
          same => n,AGI(dialplan.pl,${EXTEN})            ; run Our AGI script which resides in agi-bin, and also we are providing the dialled extension number as a command line input. 

Configure voice mail boxes

  • voicemail.conf
          [sales]                                       ;VM Context
          101 => 123,Bob's Mailbox,bob@example.com ;vmbox => [password],[Description], [Mail address to use]
          102 => 321,Peter's Mailbox,peter@example.com
         

Now please reload your configuration through asterisk CLI and Verify them
                   #rasterisk
                     > sip reload
                     > sip show peers
                     > dialplan reload
                     > voicemail reload
                     > voicemail show users
              

Now lets look at our Perl script

#!/usr/bin/perl
use warnings;
use strict;
#apt-get install build-essential

use Asterisk::AGI;

Asterisk::AGI for perl : 
First you may install cpan which is use to manage perl module by using 
#apt-get install build-essential
Then install Asterisk:AGI module using cpan
#cpan
>install Asterisk::AGI

Now in here I've define a global variable and assign the extension number which has passed as a command line argument from the Dialplan.
our $DPLAN_EXTEN=$ARGV[0];

Now depends on the number that dialed, if number start with '*' it should dialed to relevant voice mail box or else if it's a number it should directly dial the relevant sip peer.
if ($DPLAN_EXTEN =~ m/^\*/){
       vm_box();
} else {
       main();
}
####Call Routes between two peers, Bob and Peter 
sub main{

#-->In here I have created a hash(a nested hash) to store relevant information to use
 my %EXTEN_CONF = (
                '101' => {
                        'CHAN'          =>'SIP',
                        'PEER'          =>'bob',
                        'MAXWAIT'       =>5,
                        'VM_CONTEXT'    =>'sales',
                },
                '102' => {
                        'CHAN'          =>'SIP',
                        'PEER'          =>'peter',
                        'MAXWAIT'       =>5,
                        'VM_CONTEXT'    =>'sales',
                },
 );

#-->Now here we create our AGI object
my $AGI = new Asterisk::AGI;


#-->Use exec to use any Dialplan Application in your AGI script. Using exec you can execute the relevant application and pass the argument to the relevant application.
#-->$AGI->exec($app, $options)
#--> As per the following example, execute dial function and pass relevant Chanel , Peer and wait time. If you execute the following on the Dialplan it would be "exten => 101,1,Dial(SIP/bob,5)"
$AGI->exec('Dial',"$EXTEN_CONF{$DPLAN_EXTEN}{'CHAN'}/$EXTEN_CONF{$DPLAN_EXTEN}{'PEER'},$EXTEN_CONF{$DPLAN_EXTEN}{'MAXWAIT'}");

#-->Now in order to put a voice mail use voice mail function
$AGI->exec('VoiceMail',"$DPLAN_EXTEN\@$EXTEN_CONF{$DPLAN_EXTEN}{'VM_CONTEXT'}");


#-->To Hangup
$AGI->hangup();
}
####Listen to the Voice Mails
sub vm_box{
#-->In order to store the informations regarding to the voice mail boxes here I've use a hash again
my %VM_CONF = (
                '*101' => {
                        'VM_BOX'        =>'101',
                        'VM_CONTEXT'    =>'sales',
                },
                '*102' => {
                        'VM_BOX'        =>'102',
                        'VM_CONTEXT'    =>'sales',
                },
);
my $AGI = new Asterisk::AGI;
$AGI->exec('VoiceMailMain',"$VM_CONF{$DPLAN_EXTEN}{VM_BOX}\@$VM_CONF{$DPLAN_EXTEN}{VM_CONTEXT}");
$AGI->hangup();


You can put your AGI file in asterisk agi-bin. in my Debian system its on 
#cd /var/lib/asterisk/agi-bin

In order to properly run the script change the ownership to your Asterisk user and grant the execute permission.

#chown asterisk:asterisk dialplan.pl
#chmod u+x dialplan.pl

Click here to download configuration file.


To get the asterisk Dialplan application information and how to use them you may refer asterisk CLI or you can search them on https://www.voip-info.org.


Installing Perl modules,



Thats it.... Enjoy...

Comments

  1. Asterisk Gateway Interface - Perl >>>>> Download Now

    >>>>> Download Full

    Asterisk Gateway Interface - Perl >>>>> Download LINK

    >>>>> Download Now

    Asterisk Gateway Interface - Perl >>>>> Download Full

    >>>>> Download LINK 1c

    ReplyDelete

Post a Comment

Popular posts from this blog

Asterisk on Docker

This Document covers asterisk basic installation on docker. Since till now there is no official asterisk image on Docker hub we will use Debian core to install the asterisk. Prerequisites Linux host to install docker, Internet connectivity and docker account to download docker images. 1. Installing Docker (Ensure your Internet connectivity) Centos 7           #wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm           #rpm -ivh epel-release-latest-7.noarch.rpm           #yum install docker  Install Docker on RHEL and CentOS 6          # yum install epel-release          # yum install docker-io 2. Start Docker Centos 7          # systemctl start docker          # systemctl status docker          # systemctl enable docker  On RHEL/CentOS 6          # service docker start          # service docker status          # chkconfig docker on Our Asterisk PBX will reside on Debain, So first we must set our Debian container

Simple Squid (Proxy server) Configuration

1.  SETUP THE INITIAL NETWORK CONNECTIVITY ON CENTOS After installing centos completely configured the network interfaces as follows, 1. Add a network interface to centos virtual machine as “Lan segment”(Centos lan) 2. And configure the ip address for that LAN segment If this centos machine has only one NIC we have to create two vlans and trunk them.             One to connect to the internet.            One to connect to the local area network interface which we created earlier 3. Using fedora(Another vm guest os) verify the network connectivity 4. Pinging to the LAN-segment that we created in order to verify the internet connectivity. Now we are connected to the private network that we created using centos earlier. Note that in this scenario centos is act as a router. So we are going to convert this centos virtual machine to router, in order to do that we need to inform it to kernel and configure it. Using sysctl command,

HAProxy

HAProxy implementation Case Study This tutorial covers HAProxy Deployment on Firewall and SELinux enabled Centos7 systems. First of all lets get an overall idea about my situation.  I've bought a domain call mycompany.com so all of my  hosted sites should be followed by this main domain. As an example, if someone look for london.mycompany.com he should reach to London server, if someone look for chicago.mycompany.com he should reach to Chicago server So, I've created a Cloudflare account and point mycompany.com to our public IP address and created two                 CNAME entries from Cloudflare by adding London and Chicago. From the Cloudflare all the request to mycompany.com will forward to our public address and HAProxy may read those requests and Process them and forward them accordingly between two IIS servers. Note that all HTTPs connection should terminated at HAProxy.   Please see my post   HTTP

FreePBX-Installation

FreePBX-Installation 1. Pre-installation In order to setup call center server first we have to confirm that our system is full filled the minimum requirements. This asterisk deployment is based on RedHat distribution aka CentOS.  To full-fill the above requirement we are going to setup asterisk 11 on CentOS 6.5 (x64). 1.2 CentOS 6.5x64 installation  It is recommended to install CentOS 6.5x64 minimum version and manually install all the other package as our requirement.  At the beginning it recommended to configure the logical disk drives aka Raid.  Note that some of the server-rigs will not compatible to centos 6.5x64, most of the time it’s because the particular server’s Raid drivers might not be found in centos 6.5x64 disk. In such scenario please follow the below instruction. First we have to download the Raid driver from relevant vendor. (If it’s HP you will find somewhat like this hpvsa-X.X.X-X.rhel6u.5x86_64.dd). Note that if the dr

Discussion : SIP vs BRI/PRI

SIP SIP standards for Session Initiation Protocol and It's Purely IP based.  BRI/PRI PRI stands for Primary Rate Interface and It contains One 64Kbps T1 or E1 Chanel for Signaling AKA Channel D and 23 T1 or 30 E1 Channels as Bearing Chanel aka Channel B.\ BRI standards for Basic Rate Interface and Contain Two Barer channel and One Signaling Chnnel AKA 2B+D. Further Both Both PRI and BRI are ISDN services and also data rate of PRI is 2.048Mbps while 128-144Kbps. ISDN : Integrated Service Digital Network / It's Some Dumb Network SIP vs BRI/PRI SIP does Best effort Delivery as same as IP traffic do, while BRI/PRI Provide QoS. If someone requires to attain QoS through SIP something like MPLS will do with a considerable amount cost. SIP is more flexible than BRI/PRI because it can be accommodated by company existing data network while BRI/PRI reuires to have a dedicated link for it self.

RHEL Recover your root Password

Root Password recovery In this tutorial I will demonstrate you how to recover you lost password. This tutorial is for RHEL 7 Password recovery. Before we go further I would like to brief Linux boot process, When pushed power button your PC/Server it will powered on and the system firmware will runs POST (Power On Self Test) which will check and initiate attached hardware. You can do modifications to these process by BIOS/UFFI configuration After POST, system firmware will look for bootable device in other words it look for Master Boor Record (MBR) Then the System will reads the boot loader from the disk and let boot loader to take control of the system Now the boot loader loads its configuration from disk, at that point you may display the boot options Depends on your selection boot loader will load the kernel and initramfs from disk to you memory(RAM). Initramfs  is some gziped archive contains kernel modules for hardware all hardware which requ

Perl - Database Transactions

Database Transactions with Perl To understand what is a database transaction lets look at a simple bank withdrawal and deposit scenario. Assume that you have two bank accounts call A and B, and you need withdraw some amount from account A and Deposit it on account B. In this scenario what happen if you couldn't withdraw money from your account A, the deposit part won't carry out. Again if you couldn't deposit the money to your account B you have to deposit them back on account A (Which means a roll back). So in the  context of Database, Transaction is refer to  a sequence of jobs which is supposed to run as a whole. So in other words, it should happen as whole or not. So as in our following example, we have 3 Database queries which should perform as a whole. Further assume that our first Database query is supposed to perform a insert if successful, second query  should update a table if successful, third query  should delete an entry from a table.  So i

Share-A-Directory-between-two-server-NFS

Share-A-Directory-between-two-server-NFS Please grant the relevant permission on the shell scripts ./client_Export.sh Takes three argument as follows 1. server ip address to be exported 2. client directory which should be exported to the particular server 3. options aka permission for the expoted folder rw: This option allows the client server to both read and write within the shared directory sync: Sync confirms requests to the shared directory only once the changes have been committed. no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger filesystem, nfs performs scans of every directory above it, in order to verify its permissions and details. Disabling the subtree check may increase the reliability of NFS, but reduce security. no_root_squash: This phrase allows root to connect to the designated directory E