[SOLVED] How To Email Call Recordings Post Call Recording Script


#12

+1
This should be included in the wiki pages.


(Ian Plain) #13

In case anyone comes across this post I have extened teh script to convert the recording to mp3 before emailing it.

  #!/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');

echo $dt

echo -e "You have a new call recording to listen to \n\n
 The call date and time was $dt \n\n
 The call was from $2 \n\n
 The call was to $5 \n\n
 And the unique call id was $4\n\n

Please see the attached file \n\n" | mail -a $dtpath$3.mp3 -s "New Call Recording" mail@domain.com

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

The post record string in freepbx is as below

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

Finally remember to set the masquerade in sendmail so that the email correctly addressed

Enjoy


Converting recorded wav calls to mp3
(Aaron Gustafson) #14

Sorry I’m a bit of a noob, can you explain this?

Also is there any chance someone could explain or add the code so that this would then DELETE the recording? I have a limited storage space…

Thx


(Ian Plain) #15

Hi the last but one deletes file sover a certain age $file_age which is defined up the top in this case as 35 days but could change that to say 2

Ian


(Vlad) #16

For some reason my ^{TIMESTR} ^{FROMEXTEN} ^{CALLFILENAME} ^{UNIQUEID} ^{ARG3}
are blank for the script, which runs fine and sends test email etc


#17

You would have to state better your problem, that is the the interpreted ( by asterisk/Comedian mail) string that would be passed to your script as the first argument, be that a bash, perl, php whatever script.


(Vlad) #18

turns out it only happens with on demand recording (*1)
if whole session is recorded it transfers parameters to .sh fine
it is clear in logs

[2016-09-30 00:59:20] VERBOSE[2265] app_mixmonitor.c: Executing [/var/lib/asterisk/bin/emailrecording.sh]

[2016-09-30 00:50:50] VERBOSE[27016][C-00000017] app_mixmonitor.c: Executing [/var/lib/asterisk/bin/emailrecording.sh 20160930-005041 007 internal-008-007-20160930-005041-1475211041.43 1475211041.43 008]

might be a bug
can someone check if on demand recording sends?


Post Call Recording Script not sent correctly to MixMonitor
(Vlad) #19

had another minute for this
It does not because MixMonitor call is happening in /var/lib/asterisk/bin/one_touch_record.php which has no idea and doesn’t read ^ well. With regular recording it calls MixMonitor in extensions_additional.conf

in extensions_additional.conf:
[macro-one-touch-record]
include => macro-one-touch-record-custom
exten => s,1,Set(ONETOUCH_REC_SCRIPT_STATUS=)
exten => s,n,System(/var/lib/asterisk/bin/one_touch_record.php “${CHANNEL(name)}”)

in /var/lib/asterisk/bin/one_touch_record.php:
$astman->mixmonitor($masterChannel, “{$mixMonDir}{$year}/{$month}/{$day}/{$callFileName}.{$mixMonFormat}”, “ai(LOCAL_MIXMON_ID)$beep”, $mixMonPost, rand());

guess we need to fix one_touch_record.php to forward those ^ correctly as it seems they are processed by MixMonitor itself


(Vlad) #20

moved it out of this topic to FreePBX as php one is it


(Renato Fiuza) #21

Very good, it works great.
Thank you guys.


(Anton Dragushin) #22

Hello, guys, thanks a lot for this solution.

Could someone help me with adding call duration into the e-mail body?
I’ve added # $6 - Call duration as a comment and Call duration: $6\n into e-mail body, but I still have no idea what variable to use in Post Call Recording Script. I’ve tried ^{DIALEDTIME} (which gives nothing) and ^{CDR(duration)} (which gives 0 as the result).

Thanks in advance.


(Alireza M) #23

Hi, Thanks,
Is there any documentation for variables that i can use ?


(Otto) #24

If someone who has gotten this to work could post the last few resultant log file lines so others can compare with theirs it would be much appreciated.

Here is mine. The app_mixmonitor.c seems to be ending the recording after executing the email script. Is this normal?

NOTE: I know there are no FREEPBX passed variables; I was testing with a hard-coded email to see what was broken and why my emails aren’t being sent by the script.

UPDATE: The script works as intended when called directly from command line. This leaves only the possibility that the server is preventing the complete execution somehow for some reason. As noted above, file-dependent variables are not factors.


(Otto) #25

A list from 2016…
https://www.voip-info.org/wiki/view/Asterisk+variables

Not sure if this is what FREEPBX also uses because {TIMESTR} isn’t mentioned.
Still, if you look at the Asterisk log file you’ll see that variable works.


(Otto) #26

UPDATE:
Finally got this working. Make certain that any email configuration file (.muttrc for instance) is where it is expected to be by the user (in this case asterisk) running the post-call script. In mutt’s case, the file needs to be in ~/ of the asterisk user.

Also, remember variables need to be “$var”, not ‘$var’ in order to pass their values in the echo.

After the above was fixed, call recordings went to my inbox without a hitch!


(Arun Shah) #27

Great thread guys, thanks very much for the script.

I have been struggling to send out HTML emails using this script, can anyone show me how?

I want the HTML to have variables just like this script.

thanks.


(Saul Berenson) #28

Hi!
Thanks a lot for sharing this information! - it is eactly what I was looking for.

I tried the first script (just wav, no mp3) and I receive the mail.
It contains all information properly (from, to, time etc.)
but unfortunately no wav attachment.

The mail I receive in my mailclient looks as follows…:


/var/spool/asterisk/monitor/2018/03/18/in-#deleted#-#calling-no-deleted#-20180318-194803-1521402483.1160.wav

You have a new call recording to listen to

The call date and time was 03/18/2018 07:48:25 PM

The call was from #deleted#

The call was to #deleted#

So no attachment but the path to the wav file in text-form…
What did I miss?

thanks a lot!
saul


#29

Ok i know this is a little old but this request came to me from a customer.

They want to be able to do one touch call record and then for the system to email the recording to them. I was like hmm maybe i can do it but not right now. Sales guy was like “Sure we can do that. Righto Chip Chip Charoo.” That’s how i think my sales guy acts towards customers with no regard to what the system can do just to please the potential customer.

So i found this post and worked on it a bit.

I went through the Asterisk Variables website posted above. and found that what i want to do us use the following in Post Call Recording Script.

/var/lib/asterisk/bin/emailrecording.sh ^{TIMESTR} ^{CALLFILENAME} ^{UNIQUEID} ^{CHANNEL(name)}

Now the following variables are available in my script
$1 is the time of the call
$2 is the filename of the recording
$3 is the unique id of the call
$4 is who the call was with

Now that i had the extension I took advantage of the asterisk db userman_users filed where my username is the extension.

I did a simple sql query:
mysql -u root -e “SELECT email FROM asterisk.userman_users where username = $4”

And as long as you are creating usermanagement users of your extensions and using the extension as the username this will all work for you.

#!/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 - File
# $3 - unique id
# $4 - Who is the call with
# $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$2.wav  $dtpath$2.mp3
/bin/nice /bin/chown  asterisk:asterisk $dtpath/$2.mp3

dt=$(date '+%m/%d/%Y %r')

echo $dt

str=$4
exten=${str:4:4}
# the above exten takes the string of $4 which is the Channel name
# this is key in finding who recorded the call and who the call with 
# Example: SIP/2212-00000100
# the exten =${str:4:4} takes the string str which is the example above
# then looks for the 4th position in string and it starts the extension number after that
# Then it takes the second number 4 and says to grab that number of characters and associate with the 
# variable exten. leaving me with 2212 if your extensions are different then 4 characters only change the 
# second number 4 above to the extension length you have
filename=$2
IFS='-' read -a extensions <<< "${filename}"

from=""
if [ ${extensions[1]} == $exten ]
then
       from=${extensions[2]}
else
       from=${extensions[1]}
fi

email=$(mysql -u root -e "SELECT email FROM asterisk.userman_users where username = $exten")

       echo -e "You have a new call recording to listen to \n\n
       The call date and time was $dt \n
       The call was with extension: $from \n

       Please see the attached file \n\n" | mail -a $dtpath$2.mp3 -s "New Call Recording" $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

Now here is what the email looks like in the end.

image


#30

I updated the code and output above.

I ran into an issue using $FROMEXTEN in the post call recording script it wouldn’t detect the call recording based off if the call direction was external.

So with a little thinking i decided to try and use the $FILENAME and parse that filename for a 4 digit length(which my extensions) are if yours are different then 4 change that value above.

This works better it detects call recording internal-internal, internal-external, and external-internal. My only trouble now is internal-internal. How can i determine who is the party that wanted the call recorded.

I was looking at asterisk output of mixmon it shows the channel id which is the calling party but i’m not sure if thats the receive or starting party or if it’s who called mixmon. If anyone has any input to that it would be greatly appreciated.


#31

Ok that is all resolved.

I updated the code above in my last update I think this irons out my last issues.

Sorry for updating a ticket from Jan 2015 but i think my contributions to this is awesome. You can dynamically choose who gets the email and it will tell you who the calling party is and only send the vm to the user that called the mixmon recorder.