Emailing Call Recordings

From FusionPBX
Revision as of 18:53, 2 November 2013 by Hcoin (talk | contribs) (Sending call recordings as email messages.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Part of the freeswitch 'standard' dialplan is the ability to record calls. Notice in the 'call local extensions' dialplan the line:

action bind_meta_app 2 ab s record_session::$${recordings_dir}/archive/${strftime(%Y)}/${strftime(%b)}/${strftime(%d)}/${uuid}.wav

The effect of this is when a local extension places a call, either the caller or the callee can press *2 which causes a file containing a recording of the call to be made from that point to the end. The file with the recording sits on the freeswitch system. For most, it's more useful to have the recording be an attachment to an email message. These steps will accomplish that. Note that these steps send all the recordings to one email address, it my setup I use email filtering to share and direct the recordings from there. A next fusionpbx step might be to look up the email address of the local user as stored in the voicemail setup and direct the recording there.

There are two major projects involved in doing the emailing. The first is the mechanism needed to send the email, the second is making sure that mechanism gets called when the call is complete no matter whether the caller or callee hangs up first.

Step 1: create and test the 'send the email' script.
Create a script file runnable by anyone, editable only by root. My system uses the mailx package and the msmtp sendmail replacement package. Install what you need on your system. I use the script file stored here: /home/administrator/scripts/send_recording. It's contents are:

#!/bin/bash
subj="Recorded call from $2-$3 to $4"
if [ -f $1 ] ; then
echo Recording attached. | mailx -v -s "$subj" -a "$1" destinanation_address@youremail.com
if [ $? -eq 0 ] ; then rm $1;
else
logger failed to email recording file $1
fi
fi

The first argument is the recording file name, the second the caller id name, then the caller id number, then the called number. Use of the " is important to prevent the caller-id to be expanded as a malicious script. Notice also this deletes the recording from the freeswitch system if the mailing program reports no error. This reduces clutter, but may not be appropriate for everyone. Run the script as the same user that freeswitch runs under, usually www-data to made sure it works. I had to add www-data to the mail group.

Whatever you decide to use, get it working and test it running as the www-data user before going on.

Step 2: Having freeswitch call the script when the recording is complete.

This turns out to be quite a project. First:

Change the above 'bind_meta_app 2... to these three lines:


 action 	  set 	  recfile_name=$${recordings_dir}/${domain_name}/archive/${strftime(%Y)}/${strftime(%b)}/${strftime(%d)}/${uuid}.wav


 action 	  set 	  record_post_process_exec_app=system:/home/administrator/scripts/send_recording '${recfile_name}' '${caller_id_name}' '${caller_id_number}' '${destination_number}'


 action 	  bind_meta_app 	  2 ab s record_session::${recfile_name}


The good news is the above will call the email script above whenever a recording is requested but, owing to quirks in how freeswitch works, only when the 'a' leg of the call (the caller) hangs up first. If the called party hangs up first, while the recording will indeed be created, the script to do the emailing won't be called, despite the 'record_post_process_exec_app' variable above. To cause the emailing to happen when the called person hangs up first, consider these changes:

Change the line 'hangup_after_bridge' in the local extensions dialplan part to false from true:

 action 	  set 	  hangup_after_bridge=false


Then, delete the dialplan segment from the line beginning 'bridge...' to the end. Replace it with this:


 action 	  export 	  voicemail_action=save


 action 	  export 	  voicemail_id=${user_data(${dialed_extension}@${domain_name} attr number-alias)}


 action 	  export 	  voicemail_profile=${domain_name}


 action 	  bridge 	  user/${user_data(${destination_number}@${domain_name} attr id)}@${domain_name}|loopback/*99${voicemail_id}


 action 	  system 	  /home/administrator/scripts/send_recording '${recfile_name}' '${caller_id_name}' '${caller_id_number}' '${dialed_extension}'


 action 	  hangup



The above changes first try to dial the user, and if that succeeds will attempt to email the recorded call if the callee hangs up first. The script relies on the 'standard' or 'default' feature of the dialplan to 'send to voicemail' *99... extension. Be sure to edit that dial plan script to allow for the however many digits your voicemail extensions require.

I suppose in order to be 'totally complete' the lua voicemail script should be edited to send the call recording as well, but I didn't bother with it as it seems a little redundant to want to record a voicemail to another local user.