Thursday, September 19, 2013

Asterisk/FreePBX – Post voicemail trigger (e.g. send sms after new voicemail)

Image source: www.dictronics.com

Asterisk has a function to run a program after voicemail changes happen. The changes in voicemail could include

  • Received new voicemail
  • Read new voicemail
  • Moved voicemail to different folders
  • Cleared voicemails
  • ..etc

This is especially useful if you wish to integrate Asterisk as a voicemail system and notify another PaBX (or software/service) that a particular user has the above.

In our new Hotel System module for Asterisk, we had to “inform” frontdesk if a voicemail still exist in rooms before moving a user to another room. So that the user can choose to move it or leave it there. Of course, there could then be other uses for this.

 

How to enable post voicemail triggers on Asterisk.

Asterisk has a function in the voicemail.conf configuration file (or use FreePBX’s voicemail admin module) called “externnotify”. This option executes your app or scripts on voicemail changes. In my case above, i wrote a simple shell script in bash to call a webservice and “tell” that webservice of a user’s voicemail changes. This will give you an idea what are the possible variables and/or options and all the possibilities it can do.

Now, to enable this, if using FreePBX use the Voicemail Admin module and under settings, add a line like this in the eternnotify box:

/usr/local/bin/update_wminfo.sh

If not using FreePBX, edit the [general] section in /etc/asterisk/voicemail.conf and add a line like this

externnotify=/usr/local/bin/update_wminfo.sh

#!/bin/bash
# sanjay@astiostech.com
# invokes sending to webserver socket when there's a change in voicemail events for user
# v.1.0
# prequalify if this is a reload or not, if reload, we don't do anything
server=10.10.10.1:6050
fullstring="$1 $2 $3 $4 $5"
isreload=`echo "$fullstring" | grep -c "@"`
#
if [[ "$isreload" -gt "0" ]]; then
   
exit 1
else   
    context="$1"
    ext="$2"
    newvm="$3"
    oldvm="$4"
    #   
    myurl="vmnotify.php?ext=$ext&new=$newvm&old=$oldvm"
    sendurlnow=`/usr/bin/curl --silent --connect-timeout 15
http://$server/$myurl`
   

    if [[ "$sendurlnow" == "" ]]; then
        echo "ERR"
        exit 1
    else
        echo "SUCECSS"

        exit 0
    fi
fi
exit 0

Above is a simple script was written to contact a webserver (and it works btw) when VM events like above happen but eliminating reload notifications which also will trigger the externnotify, by design. The above script does a simple tasks to “qualify” reloads vs VM events, when reloads happen, a distinctive @ symbol always appears in the first variable parsed after your script (see below for variables parsed). So we take advantage of that to quality reloads vs real VM events. Take for example, extension 8000 has a new voicemail and a reload was done. We want to “catch” the VM events as described above, not reloads. Reload lines will contain something like this: 8000@default 1 0 0. The @ symbol is uniquely available when only reloads happen, we don’t want to trigger the script if it “just reloads”.

Whereas, VM events will contain something like this:  default 8000 1 0 0 without the @ symbol, the script will then proceed to do “something”, as written in your script or like above, send a trigger to a webserver. The variables are automatically parsed after the script you defined in externnotify. Your app would need to pickup the four variables parsed, those are;

1st variable – Extension (e.g. 8000 for VM events or 8000@default for reloads)
2nd variable – Number of new voicemails
3rd variable – Number of old voicemails, in the “old” folder
4th variable – Don’t know what this represents :S

Also see “externpass” and “externpassnotify” if you wish to integrate with another PaBX where Asterisk stores voicemails and the other PaBX doesn’t.

Do let us know if this worked or can be improved upon.

Enjoy!

Post a Comment