Categories
FreePBX Knowledge Base

Post call emailing of Call Recordings in Freepbx

In freepbx there is a feature that is quite well hidden but actually does a very useful job.

In The “Advanced Settings” page if you enable both “Post Call Recording Script” As the name suggests this is a script that run after a recorded call has ended. We created a script called postrecord.sh and in the text field on the menu we have put as below. This emails both inbound and outbound calls.

For calls to and from an extension we can pull the email address from the voicemail.conf and send the email to that address.

Its also set to delete the wav file away after a defined number of days.

/usr/local/sbin/postrecord.sh ^{TIMESTR} ^{FROMEXTEN} ^{CALLFILENAME} ^{UNIQUEID} ^{ARG3}

The Script below will first convert the recording then email it to you or your customer.

A couple of prerequisites are required, these are sox and lame. sox is probably already installed, lame maybe not.

Installing Lame is simple for centos as below.

wget http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz tar -zxvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure
make
make install

The script is fairly simple as below. the main variables are passed to it but we build the directory structure on the fly and file extension is fixed as wav. you can set the file_age variable to delete the wav file messages over that many days old.

  • Be careful if cutting and pasting this scripty as wordpress may have wrapped some lines
#!/bin/bash
#This script emails the recorded call right after the call is hung up. Below are    the variables passed through asterisk
# $1 - Time String
# $2 - Source
# $3 - File
# $4 - unique id
# $5 - Destination
# $dt - Date and Time
/bin/nice /bin/sleep 3
dy=$(date '+%Y')
dm=$(date '+%m')
dd=$(date '+%d')
file_age=35

dtpath=/var/spool/asterisk/monitor/$dy/$dm/$dd/
/bin/nice /usr/local/bin/lame -b 16 -m m -q 9-resample $dtpath$3.wav  $dtpath$3.mp3
/bin/nice /bin/chown  asterisk:asterisk $dtpath$3.mp3
dt=$(date '+%m/%d/%Y %r');
id=$(mysql -uUser -pPassword -s -N -e "SELECT descr from asterisk.queues_config where extension = $5");

email=recordings@yourdomain.com

file=$dtpath$3

if [ "$id" = "" ]; then
     direction=callers 
            id=$(mysql -uUser -pPassword -s -N -e "SELECT name from asterisk.users where extension = $2");

  IN=$(/bin/grep "$2 =>" /etc/asterisk/voicemail.conf)
              echo $IN
               set -- "$IN"
               IFS=","; declare -a Array=($*)
               email=${Array[2]}

            else

            direction=customers    
            fi

echo -e "You have a new call recording to listen to\n\n
 The call date and time was: $dat \n\n 
 The call was from: $2 \n\n The call was to: $5 \n\n
 The $direction name was: $id \n\n
 And the unique call id was: $4 \n\n
 Please see the attached file \n\n" | mail -a $file.mp3 -s "New Recording at $dt" $email 

/bin/nice /usr/bin/find /var/spool/asterisk/monitor/  -type f -mtime +"$file_age" |grep wav | \
while read I; do
              /bin/rm  "$I"
done