Email notifications for missed calls in Asterisk

A feature I have wanted for a long time is to get emails about missed calls. I have a Blackberry and knowing that people are calling and hanging up before leaving a vm is very useful when I am out in the field. I wrote a small shell script to add this functionality to Asterisk.

I place the script in the h extension of the stdexten marco. The h extension triggers once a call is hung up from that context. The first and only priority for the h extension is a System() call. It calls this shell script and passes many parameters to it. The first parameter is the email address to send the email to. To make this work for the marco, I adjust the macro to have a third parameter which is email address. I would like to pull the email address from that extensions vm but that function does not exist and I could not figure out how to add that function in the C source code. If anyone knows a better way, please let me know. The script requires you have the mailx package installed.

  • Download the script
  • Rename the file processCallEmail.sh.
    mv processCallEmail.txt processCallEmail.sh
  • Put it in the /var/lib/asterisk/agi-bin directory.
    mv processCallEmail.sh /var/lib/asterisk/agi-bin/"
  • Make sure the asterisk user has execute permissions on the script.
    chmod 755 /var/lib/asterisk/agi-bin/
    processCallEmail.sh
  • Add one line of documentation to macro-stdexten context on the line after ${ARG2}. ; ${ARG3} - email address to send emails about missed calls to
  • Add the h extension line to the macro-stdexten context. exten => h,1,System(processCallEmail.sh "${ARG3}" "${CALLERID(num)}" "${CALLERID(name) }" "${DIALSTATUS}" "${VMSTATUS}")
  • Alter the line that calls the macro to include an email address. exten => 500,1,Macro(stdexten,500,SIP/${EXTEN},abc@test.com)

Here is an example of an updated stdexten macro.
[macro-stdexten];
;
; Standard extension macro:
; ${ARG1} - Extension (we could have used ${MACRO_EXTEN} here as well
; ${ARG2} - Device(s) to ring
; ${ARG3} - email address to send emails about missed calls to
;
exten => s,1,AGI(ldaplookupphone2.agi)
exten => s,n,Dial(${ARG2},20) ; Ring the interface, 20 seconds maximum
exten => s,n,Goto(s-${DIALSTATUS},1) ; Jump based on status NOANSWER,BUSY,CHANUNAVAIL,CONGESTION,ANSWER)

exten => s-NOANSWER,1,Voicemail(${ARG1},u) ; If unavailable, send to voicemail w/ unavail announce

exten => s-NOANSWER,2,Goto(default,s,1) ; If they press #, return to start

exten => s-BUSY,1,Voicemail(${ARG1},b) ; If busy, send to voicemail w/ busy announce
exten => s-BUSY,2,Goto(default,s,1) ; If they press #, return to start

exten => _s-.,1,Goto(s-NOANSWER,1) ; Treat anything else as no answer

exten => a,1,VoicemailMain(${ARG1}) ; If they press *, send the user into VoicemailMain

exten => h,1,System(
processCallEmail.sh "${ARG3}" "${CALLERID(num)}" "${CALLERID(name) }" "${DIALSTATUS}" "${VMSTATUS}")

8 Responses to “Email notifications for missed calls in Asterisk”

  1. christendoc Says:

    Howdy,

    Can’t thank you enough for this little gem. Just started in on telephony (for fun and to feed the inner control freak, not professional). I kept looking at SIP providers to give email notification of missed calls, and no one did. So, I changed focus and sought a really lightweight PBX system (initially avoiding Asterisk) that had the feature. Alas :) this post of yours was about the only promising hit, so I spent a few days learning enough Asterisk to figure out where to plug your script in. It was a fun learn. I really fell for Asterisk in the process, and still think your script is just elegant and impressive — surpising the Digium team left this out.

    Thanks again, friend.

  2. christendoc Says:

    [LATER] Oh, and one other bit: I had to pull the backticks off this line:
    `echo $BODY | $MAILCMD $SUBJECT $EMAIL`

    It’s the payload line at the end of the script. If I’m just stupid and there’s a reason for them, let me know.

    Thanks again.

  3. jernej Says:

    Hi Matt!
    Your script looks promising, exactly what I need, but I can’t get it to work :(. I tried even giving the 777 permission on the file processCallEmail.sh, but I always get an error. Any clues?
    Thank you!

    Jernej

    – Executing [h@macro-stdexten:1] System(”SIP/100-08f5e6d8″, “processCallEmail.sh “mymail@mydomain.com” “100″ “Jernej P” “NOANSWER” “FAILED”") in new stack
    [Dec 1 14:29:19] WARNING[8162]: app_system.c:107 system_exec_helper: Unable to execute ‘processCallEmail.sh “mymail@mydomain.com” “100″ “Jernej P” “NOANSWER” “FAILED”‘
    == Spawn extension (macro-stdexten, h, 1) exited non-zero on ‘SIP/100-08f5e6d8′

  4. doughoman Says:

    jernej -
    I had same problem and traced it to my ending line characters being CRLF from pasting the shell script into Notepad. Just had to convert to UNIX LF only and it worked.

  5. sergiopichardo Says:

    i have same problem… can you tell us what command you use to convert the file…. regards,

    SP

  6. Tenner Says:

    I cannot find the stdexten macro, would someone be kind enough to point me in the direction of its location ?

  7. Matt Schmandt Says:

    It is included in the source of Asterisk as a sample. It can be found in the asterisk source code directory under configs/extensions.conf.sample.

    Here is the code copied from from Asterisk 1.4.25.1

    [macro-stdexten];
    ;
    ; Standard extension macro:
    ; ${ARG1} - Extension (we could have used ${MACRO_EXTEN} here as well
    ; ${ARG2} - Device(s) to ring
    ;
    exten => s,1,Dial(${ARG2},20) ; Ring the interface, 20 seconds maximum
    exten => s,2,Goto(s-${DIALSTATUS},1) ; Jump based on status (NOANSWER,BUSY,CHANUNAVAIL,CONGESTION,ANSWER)

    exten => s-NOANSWER,1,Voicemail(${ARG1},u) ; If unavailable, send to voicemail w/ unavail announce
    exten => s-NOANSWER,2,Goto(default,s,1) ; If they press #, return to start

    exten => s-BUSY,1,Voicemail(${ARG1},b) ; If busy, send to voicemail w/ busy announce
    exten => s-BUSY,2,Goto(default,s,1) ; If they press #, return to start

    exten => _s-.,1,Goto(s-NOANSWER,1) ; Treat anything else as no answer

    exten => a,1,VoicemailMain(${ARG1}) ; If they press *, send the user into VoicemailMain

  8. Tenner Says:

    Thanks for your help, sorry for not responding sooner. I’m still new to asterisk and Linux and currently using FreePBX which doesn’t seem to like ti when you edit too many files. :-) But thank-you for your prompt reply !!

Leave a Reply

You must be logged in to post a comment.