<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style id="owaParaStyle">P {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
</style>
</head>
<body fPStyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">
<p>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.&nbsp; Im truly grateful, thanks everyone.</p>
<p>&nbsp;</p>
<p>I run freeswitch at my car dealership.&nbsp; We have approximately 40 sip extensions, an&nbsp;ATT voip&nbsp;connection,&nbsp;and&nbsp;6 analog outbound&nbsp;fax lines provided by freetdm.&nbsp; Prior to freeswitch, I was running an old Lucent/Avaya Merlin &quot;key&quot; system.&nbsp; One of the features
 of the Merlin was that it was pretty easy to do &quot;directed callpick&quot;.&nbsp; In essence, you would dial *7 &#43; extension you wanted to pickup.&nbsp; Provided the call was either on hold or ringing at that extension, you would pick it up.&nbsp; When I first started using Freeswitch,
 I was very disappointed in the directed callpick.&nbsp; Before I realized that the call pickup that was built into the sample dialplan was really just an example to&nbsp;show a simple possibility for using intercept.&nbsp; I was convinced that it was a totally useful feature.&nbsp;
 I couldn't believe that was as good as it got...&nbsp; 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.</p>
<p>&nbsp;</p>
<p>Then&nbsp;I woke up, and rolled my own 8-) .&nbsp; In an effort to provide just another example, I wanted to share the piece of code I've developed.&nbsp; I will warn that this was the first perl script I have ever written, and probably is pretty ugly,&nbsp;but it seems to
 work perfectly in our environment.&nbsp; Bottom line, do what you want with it...I'm just trying to help other beginner's like myself:</p>
<p>&nbsp;</p>
<p>To all beginners:&nbsp; You will need to build and enable mod_perl for this to work...</p>
<p>&nbsp;</p>
<p>First, we need an entry in your XML dialplan to see the user's request to interecept a &quot;held&quot; or &quot;ringing&quot; extension.&nbsp; The only 4 digit extensions at my company are internal ones so I match on ** &#43; 4-digit-extension.</p>
<p>&nbsp;</p>
<p>In the xML dialplan:</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp; &lt;extension name=&quot;intercept-ext&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;condition field=&quot;destination_number&quot; expression=&quot;^\*\*(\d{4})$&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;action application=&quot;answer&quot;/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;action application=&quot;perl&quot; data=&quot;/usr/local/freeswitch/scripts/myintercept.pl $1&quot;/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;action application=&quot;sleep&quot; data=&quot;2000&quot;/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/condition&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;/extension&gt;<br>
</p>
<p>Then, we need the actual perl script:</p>
<p>/usr/local/freeswitch/scripts/myintercept.pl:</p>
<p><br>
# This is code to allow us to use intercept&nbsp;to grab&nbsp;only calls that are on hold or ringing.<br>
#<br>
# by Frank Busalacchi 2012-06-13</p>
<p>use strict;<br>
use Switch;<br>
use Data::Dumper;<br>
use XML::Simple;<br>
use POSIX qw(strftime);<br>
our $session;</p>
<p>{<br>
&nbsp;&nbsp;&nbsp; my $dest_target = $ARGV[0];</p>
<p>&nbsp;&nbsp;&nbsp; my $api = new freeswitch::API();<br>
&nbsp;&nbsp;&nbsp; my $xml = new XML::Simple;<br>
&nbsp;&nbsp;&nbsp; my $chan_xml = $xml-&gt;XMLin($api-&gt;executeString(&quot;show detailed_calls as xml&quot;),KeyAttr =&gt; 'row', ForceArray =&gt; ['row']);</p>
<p>&nbsp;&nbsp;&nbsp; my %candidates;<br>
&nbsp;&nbsp;&nbsp; my $result;</p>
<p>&nbsp;&nbsp;&nbsp; foreach my $row (@{$chan_xml-&gt;{row}}) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($row-&gt;{callstate} eq &quot;RINGING&quot; &amp;&amp; $row-&gt;{dest} eq $dest_target) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $candidates{ $row-&gt;{created_epoch} - 1000 }&nbsp; = $row-&gt;{call_uuid};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#In the case of a tie, we want&nbsp;to pickup ringing calls before held calls...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; next;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elsif ($row-&gt;{callstate} eq &quot;HELD&quot; &amp;&amp; $row-&gt;{b_sent_callee_num} eq $dest_target ) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $candidates{ $row-&gt;{created_epoch} } = $row-&gt;{b_uuid};<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; next;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elsif ($row-&gt;{b_callstate} eq &quot;HELD&quot; &amp;&amp; $row-&gt;{b_callee_num} eq $dest_target ) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $candidates{ $row-&gt;{b_created_epoch} } = $row-&gt;{uuid};<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; next;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; if( keys( %candidates ) == 0) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = &quot;[Intercept] No suitable channel to intercept on extension $dest_target.&quot;;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; freeswitch::consoleLog(&quot;INFO&quot;,&quot;$result\n&quot;);<br>
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; else {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $user = $session-&gt;getVariable('caller_id_name');<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $cid_num = $session-&gt;getVariable('caller_id_number');</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for my $winner ( sort keys %candidates ) {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $uuid= $candidates{$winner};</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = &quot;[Intercept]User:$user&quot; . &quot;[&quot; . &quot;$cid_num&quot; . &quot;] intercepting Extension:$dest_target&quot; . &quot;.&nbsp; UUID:&quot; . &quot;$uuid&quot; . &quot;\n&quot;;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; freeswitch::consoleLog(&quot;INFO&quot;,&quot;$result\n&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $session-&gt;execute(&quot;intercept&quot;,$uuid);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; last;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; 1;<br>
}</p>
<p>&nbsp;</p>
<p>Enjoy!</p>
<p>&nbsp;</p>
<p>-Frank</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
</body>
</html>