Mod perl Hangup Hook

From FreeSWITCH Wiki

Revision as of 17:08, 29 August 2011 by Mcollins (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Introduction

FreeSWITCH's api_hangup_hook is a handy tool for post-processing on a call. While many use Lua for this (because it is lightweight and easy) there are valid reasons for using Perl. For example, Lua does not natively support directory operations (create file, delete file, etc.). Also, Perl has many CPAN modules that can be leveraged for post processing.

Discussion

Post-processing with a perl script is actually very simple if you are familiar with Perl and know a few tricks. Keep these thoughts in mind:

  • Enable mod_perl in src/modules.conf and add to conf/autoload_configs/modules.conf.xml
  • Put your scripts in /usr/local/freeswitch/scripts/ unless you wish to specify the full path to your scripts
  • Use the "our $env;" trick
  • You don't need session_in_hangup_hook in order to use the special $env object

Setting a hook is simple:

<action application="set" data="api_hangup_hook=perl hook.pl"/>

The above hook will run "perl hook.pl" when the channel hangs up.

A sample hook.pl:

our $env;
open(FILEOUT,'>','/tmp/hookdata.txt');
print FILEOUT $env->serialize;
close(FILEOUT);

A dump of hookdata.txt will show you what is available. Note that this particular hook ran after a successful rxfax application:

'direction: outbound
is_outbound: true
uuid: 2cef485a-d020-11e0-a4e6-474e765f4808
write_codec: L16
write_rate: 8000
channel_name: loopback/9905-a
call_uuid: 2cef3cde-d020-11e0-a4e5-474e765f4808
origination_caller_id_number: 5594372600
originate_early_media: true
other_loopback_leg_uuid: 2cef485a-d020-11e0-a4e6-474e765f4808
loopback_leg: B
open: true
RFC2822_DATE: Fri,%2026%20Aug%202011%2013%3A15%3A43%20-0700
endpoint_disposition: ANSWER
playback_seconds: 2
playback_ms: 2000
playback_samples: 16000
api_hangup_hook: perl%20hook.pl
fax_v17_disabled: 0
fax_ecm_requested: 1
fax_filename: /tmp/fax-in-2cef485a-d020-11e0-a4e6-474e765f4808.tif
jitterbuffer_msec: 0
fax_success: 1
fax_result_code: 0
fax_result_text: OK
fax_ecm_used: on
fax_local_station_id: SpanDSP%20Fax%20Ident
fax_remote_station_id: SpanDSP%20Fax%20Ident
fax_document_transferred_pages: 1
fax_document_total_pages: 1
fax_image_resolution: 8031x3850
fax_image_size: 0
fax_bad_rows: 0
fax_transfer_rate: 14400
read_codec: L16
read_rate: 8000
current_application: hangup
hangup_cause: NORMAL_CLEARING
hangup_cause_q850: 16
digits_dialed: none
start_stamp: 2011-08-26%2013%3A15%3A43
profile_start_stamp: 2011-08-26%2013%3A15%3A43
answer_stamp: 2011-08-26%2013%3A15%3A43
end_stamp: 2011-08-26%2013%3A16%3A39
start_epoch: 1314389743
start_uepoch: 1314389743241198
profile_start_epoch: 1314389743
profile_start_uepoch: 1314389743241198
answer_epoch: 1314389743
answer_uepoch: 1314389743261208
bridge_epoch: 0
bridge_uepoch: 0
last_hold_epoch: 0
last_hold_uepoch: 0
hold_accum_seconds: 0
hold_accum_usec: 0
hold_accum_ms: 0
resurrect_epoch: 0
resurrect_uepoch: 0
progress_epoch: 0
progress_uepoch: 0
progress_media_epoch: 0
progress_media_uepoch: 0
end_epoch: 1314389799
end_uepoch: 1314389799501207
last_app: hangup
caller_id: 5594372600
duration: 56
billsec: 56
progresssec: 0
answersec: 0
waitsec: 0
progress_mediasec: 0
flow_billsec: 56
mduration: 56260
billmsec: 56240
progressmsec: 0
answermsec: 20
waitmsec: 0
progress_mediamsec: 0
flow_billmsec: 56260
uduration: 56260009
billusec: 56239999
progressusec: 0
answerusec: 20010
waitusec: 0
progress_mediausec: 0
flow_billusec: 56260009
Channel-State: CS_HANGUP
Channel-Call-State: HANGUP
Channel-State-Number: 10
Channel-Name: loopback/9905-b
Unique-ID: 2cef89e6-d020-11e0-a4e7-474e765f4808
Call-Direction: inbound
Presence-Call-Direction: inbound
Channel-Call-UUID: 2cef3cde-d020-11e0-a4e5-474e765f4808
Answer-State: hangup
Channel-Read-Codec-Name: L16
Channel-Read-Codec-Rate: 8000
Channel-Read-Codec-Bit-Rate: 128000
Channel-Write-Codec-Name: L16
Channel-Write-Codec-Rate: 8000
Channel-Write-Codec-Bit-Rate: 128000
Caller-Direction: inbound
Caller-Dialplan: xml
Caller-Caller-ID-Number: 5594372600
Caller-Callee-ID-Name: Outbound%20Call
Caller-Callee-ID-Number: 9905
Caller-Destination-Number: 9905
Caller-Unique-ID: 2cef89e6-d020-11e0-a4e7-474e765f4808
Caller-Source: mod_loopback
Caller-Context: default
Caller-Channel-Name: loopback/9905-b
Caller-Profile-Index: 1
Caller-Profile-Created-Time: 1314389743241198
Caller-Channel-Created-Time: 1314389743241198
Caller-Channel-Answered-Time: 1314389743261208
Caller-Channel-Progress-Time: 0
Caller-Channel-Progress-Media-Time: 0
Caller-Channel-Hangup-Time: 1314389799501207
Caller-Channel-Transfer-Time: 0
Caller-Screen-Bit: true
Caller-Privacy-Hide-Name: false
Caller-Privacy-Hide-Number: false
variable_direction: outbound
variable_is_outbound: true
variable_uuid: 2cef485a-d020-11e0-a4e6-474e765f4808
variable_write_codec: L16
variable_write_rate: 8000
variable_channel_name: loopback/9905-a
variable_call_uuid: 2cef3cde-d020-11e0-a4e5-474e765f4808
variable_origination_caller_id_number: 5597993757
variable_originate_early_media: true
variable_other_loopback_leg_uuid: 2cef485a-d020-11e0-a4e6-474e765f4808
variable_loopback_leg: B
variable_open: true
variable_RFC2822_DATE: Fri,%2026%20Aug%202011%2013%3A15%3A43%20-0700
variable_endpoint_disposition: ANSWER
variable_playback_seconds: 2
variable_playback_ms: 2000
variable_playback_samples: 16000
variable_api_hangup_hook: perl%20hook.pl
variable_fax_v17_disabled: 0
variable_fax_ecm_requested: 1
variable_fax_filename: /tmp/fax-in-2cef485a-d020-11e0-a4e6-474e765f4808.tif
variable_jitterbuffer_msec: 0
variable_fax_success: 1
variable_fax_result_code: 0
variable_fax_result_text: OK
variable_fax_ecm_used: on
variable_fax_local_station_id: SpanDSP%20Fax%20Ident
variable_fax_remote_station_id: SpanDSP%20Fax%20Ident
variable_fax_document_transferred_pages: 1
variable_fax_document_total_pages: 1
variable_fax_image_resolution: 8031x3850
variable_fax_image_size: 0
variable_fax_bad_rows: 0
variable_fax_transfer_rate: 14400
variable_read_codec: L16
variable_read_rate: 8000
variable_current_application: hangup
variable_hangup_cause: NORMAL_CLEARING
variable_hangup_cause_q850: 16
variable_digits_dialed: none
variable_start_stamp: 2011-08-26%2013%3A15%3A43
variable_profile_start_stamp: 2011-08-26%2013%3A15%3A43
variable_answer_stamp: 2011-08-26%2013%3A15%3A43
variable_end_stamp: 2011-08-26%2013%3A16%3A39
variable_start_epoch: 1314389743
variable_start_uepoch: 1314389743241198
variable_profile_start_epoch: 1314389743
variable_profile_start_uepoch: 1314389743241198
variable_answer_epoch: 1314389743
variable_answer_uepoch: 1314389743261208
variable_bridge_epoch: 0
variable_bridge_uepoch: 0
variable_last_hold_epoch: 0
variable_last_hold_uepoch: 0
variable_hold_accum_seconds: 0
variable_hold_accum_usec: 0
variable_hold_accum_ms: 0
variable_resurrect_epoch: 0
variable_resurrect_uepoch: 0
variable_progress_epoch: 0
variable_progress_uepoch: 0
variable_progress_media_epoch: 0
variable_progress_media_uepoch: 0
variable_end_epoch: 1314389799
variable_end_uepoch: 1314389799501207
variable_last_app: hangup
variable_caller_id: 5594372600
variable_duration: 56
variable_billsec: 56
variable_progresssec: 0
variable_answersec: 0
variable_waitsec: 0
variable_progress_mediasec: 0
variable_flow_billsec: 56
variable_mduration: 56260
variable_billmsec: 56240
variable_progressmsec: 0
variable_answermsec: 20
variable_waitmsec: 0
variable_progress_mediamsec: 0
variable_flow_billmsec: 56260
variable_uduration: 56260009
variable_billusec: 56239999
variable_progressusec: 0
variable_answerusec: 20010
variable_waitusec: 0
variable_progress_mediausec: 0
variable_flow_billusec: 56260009
API-Command: perl
API-Command-Argument: hook.pl

'

Individual items can be accessed using the getHeader method:

print FILEOUT "UUID: " . $env->getHeader('uuid') . "\n";
print FILEOUT "Caller: " . $env->getHeader('caller_id') . "\n";
print FILEOUT "Fax result: " $env->getHeader('fax_result_text') . "\n";
print FILEOUT "Fax pages: " . $env->getHeader('fax_document_transferred_pages') . "\n";

Once you have the values you need you simply build a perl script and do whatever you wish to do.

Examples

Email to Fax With Gmail Account

This example requires a Gmail account and a CPAN module:

perl -MCPAN -e 'install Email::Send::SMTP::Gmail'

It also uses the tiff2pdf utility that is part of libtiff. (I'm sure you have libtiff installed, otherwise mod_spandsp would not have compiled...)

Here is the perl script:

New script coming - the one I was using has an issue with SSLeay and can cause mod_perl and FS to core dump...

Here is the dialplan extension:

  <extension name="fax in">
    <condition field="destination_number" expression="^9905$">
      <action application="answer"/>
      <action application="playback" data="silence_stream://2000"/>
      <action application="set" data="api_hangup_hook=perl hook.pl"/>
      <action application="set" data="fax_file=/tmp/fax-in-${uuid}.tif"/>
      <action application="rxfax" data="${fax_file}"/>
      <action application="info"/>
      <action application="log" data="INFO Fax recd!!"/>
      <action application="hangup"/>
    </condition>
  </extension>

See Also

Personal tools

Community
Support FreeSWITCH