Mod perl

From FreeSWITCH Wiki

Jump to: navigation, search

mod_perl is supported as of RC4

Configuring

The file conf/autoload_configs/perl.conf.xml is used in the default FreeSWITCH™ setup.

Here is a minimum configuration file, it will fetch a dialplan from perl script.

<configuration name="perl.conf" description="PERL Configuration">
  <settings>
    <!--<param name="xml-handler-script" value="/tmp/xml.pl"/>-->
    <!--<param name="xml-handler-bindings" value="dialplan"/>-->

    <!--
	The following options identifies a perl script that is launched	
	at startup and may live forever in the background.
	You can define multiple lines, one for each script you 
	need to run.
    -->
    <!--param name="startup-script" value="startup_script_1.pl"/-->
    <!--param name="startup-script" value="startup_script_2.pl"/-->

  </settings>
</configuration>

The xml-handler-bindings value can assume the following values:

  • configuration
  • dialplan
  • directory

(See Mod xml curl for more informations. Mod_perl behaves the same identical way.

The startup-script values represent perl scripts (located inside the scripts/ directory) that are launched when FreeSWITCH is started. The scripts live in their own thread. You can use them to run simple tasks (and then let them finish) or looping forever, watching (for example) for events, generating calls or whatever you like.


How to access request parameters and how to return data

You have two hashes that are populated for you by freeswitch. Those hashes are:

  • %XML_REQUEST
  • %XML_DATA

You also have $params that is a wrapped switch event.

You should return a valid XML dialplan in the $XML_STRING variable before exiting your code.

See the perl code example below to start experimenting. the code will dump all the data you need for your login on the console.


Perl Code Examples

  • Called from dialplan
#!/usr/bin/perl

freeswitch::console_log("info", "Perl rocks\n");

while( ($name, $value) = each(%XML_REQUEST)) {
    freeswitch::console_log("info", '[XML_REQUEST] '."$name => $value\n");
}

# %XML_DATA is only present when perl is used as a binding
while( ($name, $value) = each(%XML_DATA)) {
    freeswitch::console_log("info", '[XML_DATA] '."$name => $value\n");
}

# even $params is full of data
my $xml_dump = $params->serialize();
freeswitch::console_log("info", "[PARAMS] $xml_dump\n");


$XML_STRING = '
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="freeswitch/xml">
  <section name="dialplan" description="Perl RE Dial Plan For FreeSwitch">
    <context name="default">
      <extension name="test">
        <condition field="destination_number" expression="^991$">
          <action application="playback" data="tone_stream://path=${base_dir}/conf/tetris.ttml;loops=5"/>
        </condition>
      </extension>
    </context>
  </section>
</document>
';

  • Called from CLI or webapi
#!/usr/bin/perl
#
use strict;
use warnings;

our $stream;
$stream->write("Content-Type: text/plain\n\n");

## Get OS Type and make a decision
if ( $^O eq "linux" ) {
    my $res = `uname -a`;
    $stream->write("OS Info: $res\n");
} else {
    $stream->write("Loser! Your OS is: $^O\n");
}

# End with a "true value" as it were
1;

Note: I put this script into my /usr/local/freeswitch/scripts directory as sysinfo.pl. It can then be run thusly:

perl sysinfo.pl -OR- perlrun sysinfo.pl (from the CLI)
http://your-fs-host-name-or-ip:8080/api/perl?sysinfo.pl (URL for a browser)
Personal tools