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

In reply to Asterisk and res_perl by outcast

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.