[Freeswitch-users] Sharing: Call Intercept

Frank Busalacchi Jr frankjr at mcpeekdodge.com
Sat Jun 23 18:29:49 MSD 2012


Over the last 6 months, I have received all kinds of support and help from the freeswitch community (both IRC and this list) in learning to control this incredible piece of software.  Im truly grateful, thanks everyone.



I run freeswitch at my car dealership.  We have approximately 40 sip extensions, an ATT voip connection, and 6 analog outbound fax lines provided by freetdm.  Prior to freeswitch, I was running an old Lucent/Avaya Merlin "key" system.  One of the features of the Merlin was that it was pretty easy to do "directed callpick".  In essence, you would dial *7 + extension you wanted to pickup.  Provided the call was either on hold or ringing at that extension, you would pick it up.  When I first started using Freeswitch, I was very disappointed in the directed callpick.  Before I realized that the call pickup that was built into the sample dialplan was really just an example to show a simple possibility for using intercept.  I was convinced that it was a totally useful feature.  I couldn't believe that was as good as it got...  My problem with the directed pickup that is in the sample dialplan is that it can steal a live call right from another extension, as well as it really only works with the most recent call on that extension.



Then I woke up, and rolled my own 8-) .  In an effort to provide just another example, I wanted to share the piece of code I've developed.  I will warn that this was the first perl script I have ever written, and probably is pretty ugly, but it seems to work perfectly in our environment.  Bottom line, do what you want with it...I'm just trying to help other beginner's like myself:



To all beginners:  You will need to build and enable mod_perl for this to work...



First, we need an entry in your XML dialplan to see the user's request to interecept a "held" or "ringing" extension.  The only 4 digit extensions at my company are internal ones so I match on ** + 4-digit-extension.



In the xML dialplan:



    <extension name="intercept-ext">
      <condition field="destination_number" expression="^\*\*(\d{4})$">
        <action application="answer"/>
        <action application="perl" data="/usr/local/freeswitch/scripts/myintercept.pl $1"/>
        <action application="sleep" data="2000"/>
      </condition>
    </extension>

Then, we need the actual perl script:

/usr/local/freeswitch/scripts/myintercept.pl:

# This is code to allow us to use intercept to grab only calls that are on hold or ringing.
#
# by Frank Busalacchi 2012-06-13

use strict;
use Switch;
use Data::Dumper;
use XML::Simple;
use POSIX qw(strftime);
our $session;

{
    my $dest_target = $ARGV[0];

    my $api = new freeswitch::API();
    my $xml = new XML::Simple;
    my $chan_xml = $xml->XMLin($api->executeString("show detailed_calls as xml"),KeyAttr => 'row', ForceArray => ['row']);

    my %candidates;
    my $result;

    foreach my $row (@{$chan_xml->{row}}) {
        if ($row->{callstate} eq "RINGING" && $row->{dest} eq $dest_target) {
            $candidates{ $row->{created_epoch} - 1000 }  = $row->{call_uuid};     #In the case of a tie, we want to pickup ringing calls before held calls...
            next;
        }

        elsif ($row->{callstate} eq "HELD" && $row->{b_sent_callee_num} eq $dest_target ) {
            $candidates{ $row->{created_epoch} } = $row->{b_uuid};
            next;
        }

        elsif ($row->{b_callstate} eq "HELD" && $row->{b_callee_num} eq $dest_target ) {
            $candidates{ $row->{b_created_epoch} } = $row->{uuid};
            next;
        }
    }

    if( keys( %candidates ) == 0) {
        $result = "[Intercept] No suitable channel to intercept on extension $dest_target.";
        freeswitch::consoleLog("INFO","$result\n");
    }

    else {
        my $user = $session->getVariable('caller_id_name');
        my $cid_num = $session->getVariable('caller_id_number');

        for my $winner ( sort keys %candidates ) {

            my $uuid= $candidates{$winner};

            $result = "[Intercept]User:$user" . "[" . "$cid_num" . "] intercepting Extension:$dest_target" . ".  UUID:" . "$uuid" . "\n";
            freeswitch::consoleLog("INFO","$result\n");
            $session->execute("intercept",$uuid);
            last;
        }
    }
    1;
}



Enjoy!



-Frank




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20120623/c18bd28d/attachment.html 


Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users mailing list