Asterisk Gateway Interface – Perl
May 11, 2018 | Perl, Programming and Scripting, VoIP | No Comments
Asterisk Gateway Interface

1. What is Asterisk Gateway Interface?
- Dialplan
- Asterisk Manager Interface (AMI)
- Asterisk Gateway Interface (AGI)
Dialplan
Asterisk Manager Interface (AMI)
Asterisk Gateway Interface (AGI)

- Standard AGI
- Dead AGI
- Fast AGI
- EAGI
2. Okay Now it’s time to discuss our scenario,
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
#rasterisk
> sip reload
> sip show peers
> dialplan reload
> voicemail reload
> voicemail show users
#!/usr/bin/perl use warnings; use strict;
#apt-get install build-essential
use 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();
#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.







