I found that there are great many of us who are using VOIP and Asterisk. Of those of us who are using Asterisk many have not heard of res_perl.

res_perl is module for asterisk that will allow you to write applications for the dial plan in perl. The great thing about it is that it will only run one copy of multi-threaded perl, instead of calling multiple copies use AGI scripting. The even better part of this it has module that give direct access to the Asterisk API. So you can do things bridging calls and recording of audio right in the perl script. Now here is sudo example of how to build a dynamic dial plan with Asterisk and res_perl.

****This has not been tested to work****
extensions.conf: [incoming} exten => _4XXX,1,Perl(dynamicexten:${EXTEN}) exten => _4XXX,2,SoftHangup exten => _4XXX,102,Playback(didnotgothrough) exten => _4XXX,103,SoftHangup exten => h,1,Hangup exten => i,1,Playback(invalid) exten => i,2,SoftHangup exten => t,1,Playback(didnotgothrough) exten => t,2,SoftHangup [dynamic] exten => s,1,Dial(${TYPE}/${USER},${TIMEOUT},${OPTIONS}) exten => s,2,Voicemail(${VMSUPPRES}u${VMEXTEN}) exten => s,3,SoftHangup exten => s,102,Voicmail(${VMSUPPRES}b${VMEXTEN}) exten => s,103,SoftHangup exten => h,1,Hangup exten => i,1,Playback(invalid) exten => i,2,SoftHangup exten => t,1,Playback(didnotgothrough) exten => t,2,SoftHangup exten => o,1,Goto(operator,s,1) exten => *,1,VoicemailMain() /etc/asterisk/perl/asterisk_init.pm snippit: use DBI; sub dynamicexten { ($chan_name,$exten) = shift; $chan = asterisk_get_channel_by_name($chan_name); $dbh = DBI->connect('dbi:mysql:dynamicdail','asterisk','astdbi'); $cmd = $dbh->prepare(qq!select extenuser.*,sip.* from extenuser,si +p where extenuser.exten = "$exten" and sip.name = "$exten"!); $cmd->execute || return -1; # if cmd cannot execute s +end return -1 to dialplan to change priority by 101 $data = $cmd->fetchrow_hashref; # get data from DB asterisk_setcontext($chan,"dynamic"); asterisk_setpriority($chan,"1"); asterisk_setextension($chan,"s"); foreach(keys %$data) { # load up variables for +dialplan asterisk_setvariable($chan,$_,$_,$data->{$_}); } return 0; }

If you had done this using AGI it would have call a new instance of perl and load all of the modules every time some calls. With res_perl it stores perl and modules in memory.

There is another way of loading perl scripts using res_perl. You store them in /etc/asterisk/perl/apps and run like this
exten => _4XXX,1,Perl(LoadFile:dynamicexten.pl,${EXTEN}) sample /etc/asterisk/perl/apps/dyanmicexten.pl: package Asterisk::Embed; use DBI; sub { ($chan_name,$exten) = shift; $chan = asterisk_get_channel_by_name($chan_name); $dbh = DBI->connect('dbi:mysql:dynamicdail','asterisk','astdbi'); $cmd = $dbh->prepare(qq!select extenuser.*,sip.* from extenuser,si +p where extenuser.exten = "$exten" and sip.name = "$exten"!); $cmd->execute || return -1; # if cmd cannot execute s +end return -1 to dialplan to change priority by 101 $data = $cmd->fetchrow_hashref; # get data from DB asterisk_setcontext($chan,"dynamic"); asterisk_setpriority($chan,"1"); asterisk_setextension($chan,"s"); foreach(keys %$data) { # load up variables for +dialplan asterisk_setvariable($chan,$_,$_,$data->{$_}); } return 0; }
No I did not forget to add the 1; at the end.

***Note: There is typo in the res_perl README. It says to load a perl file it should be Perl(Loadfile:filename,arg_1). It really should be Perl(LoadFile:filename,arg_1). Notice File is captialized.

There is one down side, documentation. There not alot of it. So if you really want to understand be prepared to read some c code and read alittle bit about SWIG.

Please let me know if you have questions.
This only a virtual reality

Replies are listed 'Best First'.
Re: Asterisk and res_perl
by outcast (Monk) on Apr 17, 2005 at 14:03 UTC
    Let me know if you guys need the database schemea.

    This only a virtual reality
Re: Asterisk and res_perl
by outcast (Monk) on Apr 25, 2005 at 13:43 UTC
    Just a quick note. Make sure when you install DBI or your DBD driver you do it manually, DO NOT USE THE CPAN SHELL!!!! It will cause res_perl to break. I am working on a solution to fix this.
    This only a virtual reality