Asterisk voicemail notification call out script

I ran into a issue recently as I needed to setup a call out system for my wife’s vet practice.  The system needed to call the on-call veterinarian after a message was left on the PBX.

I had originally set their office up with a FreePBX installation, but there was echo on the line and other issues that I wasn’t prepared to try to troubleshoot.  We use a SpoTel device at home so I knew that it would have the echo cancellation technology built into the device.

SpoTel ipPBX02

SpoTel ipPBX02

On the old PBX I was able to write PHP scripts to do my bidding which involved sending SMS messages when voicemail messages were left.  The Dr’s did not particularly like that implementation since they preferred the system called them to notify of messages.  A phone call does have a certain urgency to it that is not easily re-produced with an SMS ding..  :)In the end I picked up a SpoTel ipPBX02 (same device as I am using at home) but pre-configured for use with 2 Bell lines.  I believe this meant that the two PSTN ports were FXO ready.  The device sells for $275 and in my opinion (together with a good VOIP provider) is well worth the money.  Their office uses VOIP phones (Polycom 601’s)  through out, so that meant the device did not need to have any FXS ports to connect to standard analog phones.

With the SpoTel device, it runs ucLinux which is a very bare bones operating system.  I did find that it had basic shell (not bash) so at least I knew that it would have some programming ability; though likely painful.

In the end the setup went like this…

  • Added a call out to a shell script on the externnotify directive within the [general] block in voicemail.conf
  • The called script determined the extension based on the passed parameters from the externnotify, then called the appropriate Dr’s script
  • The Dr’s script
    • first checked to ensure there was a message waiting
      • no message, exit the script
    • setup a lock file to make sure that it was running in an exclusive state
    • forked a call to the Dr’s phone and bridged that call to an extension on the PBX
      • extension details added to extension_additional.conf
    • slept for 10 minutes and started on the first step above
  • Once connected to the Dr, the system told them that it was the call notify system calling then directed them to their voicemail account for validation
If the Dr did not answer the phone, or did not listen to the message the system would repeat the call out again 10 minutes later.
Here are the file chunks
voicemail.conf
[general]
externnotify=/var/lib/misc/system_notify.sh

system_notify.sh
#!/bin/sh
CONTEXT=$1
EXTENSION=$2
VM_COUNT=$3

if [ "$2" == "372" ]
then
        sh /var/lib/misc/amanda_notify.sh
elif [ "$2" == "272" ]
then
        sh /var/lib/misc/robin_notify.sh
fi

robin_notify.sh

#!/bin/sh

LOCKFILE="/tmp/272outdial.lock"
MESSAGEFILE="/var/spool/asterisk/voicemail/default/272/INBOX/msg0000.txt"

if [ -f $LOCKFILE ] && echo $LOCKFILE exists && exit 0
then
   touch $LOCKFILE
fi

if [ -f $MESSAGEFILE ]
then
   asterisk -rx "originate SIP/trunk_2/16135551212 extension 5272@"
else
   rm -f $LOCKFILE
exit
fi

sleep 600

sh /var/lib/misc/robin_notify.sh
rm -f $LOCKFILE
exit
extensions_additional.conf
[notify-callback]
exten => 5272,1,Answer
exten => 5272,n,Wait(1)
exten => 5272,n,Background(/storage/sounds/record/CallNotify)
exten => 5272,n,VoiceMailMain(272@default)
exten => 5272,n,Macro(hangupcall)

Note the line asterisk -rx “originate SIP/trunk_2/16135551212 extension 5272@” which actually does the heavy lifting for us.  cuLinux does not support the ability to fork, so this callout to asterisk directly solves the problem for us.

The call is made on the SIP trunk, and then passed to extension 5272.

Looking at  extensions_additional.conf we can see how the flow is within Asterisk.  It pushes out the CallNotify recorded message to let the Dr. know that they are being called, then directs to the Dr’s voicemail box to have them login.  Once the Dr. listens to the message (or whatever) the system is free to hangup the call.

So far there have been no issues and all has worked without issue – and at a major cost saving to their office.

Script chunks as files:
Asterisk-scripts.zip

Comments (5):

  1. Travis

    September 19, 2016 at 11:49 pm

    I am trying to get this outcall vmail notify to work. I am running it on elastix 2.4 that has freepbx 2.8
    I have made come changes for ext numbers and so but I can’t get it to call out.. Can you please maybe give some help. I am not sure if it is calling the SH codes or not.. I a see a lot of it all in PHP

    Reply
    • Robin Mulloy

      September 20, 2016 at 12:05 am

      Try the following on the command line..

      asterisk -rx “originate SIP/trunk_2/16135551212 extension 5272@”

      You will need to update your trunk and phone number appropriately (and the extension). See what message you get when you execute this. You should get a phone call from your PBX to the number listed and if you have your extensions setup then you should get a call.

      Now I should also mention that I haven’t worked with Asterisk in quite a while.

      You can also check the Asterisk logs as often the answer will be listed there when something isn’t quite working as you expect.

      Reply
  2. Travis

    September 20, 2016 at 8:53 am

    I have tried that but just I don’t even see it trying to call out. When just watching the asterisk in putty.
    What does the trunk_2 mean? Is that the trunk or a trunk route?

    Reply
    • Robin Mulloy

      September 20, 2016 at 3:24 pm

      When you call out from an extension on the PBX, you should see a similar designation. Use that format. It might be SIP/### or blahblah/external/##### All that “SIP/trunk_2/####” represents is the routing in my case to get a call out on the SIP trunk.

      The “extension 5272@” simply pushes the call to that extension where the PBX then plays the recorded message and does other processing as noted above.

      You have all the pieces, you just need to match your own setup (ie. trunk (SIP or otherwise) and extension)

      Hope this helps..

      Reply
      • Travis

        September 21, 2016 at 3:24 pm

        Ok. I happen to find out where I was wrong.. I got it working. Thanks for the help.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.