Asterisk tips callback

How to create callback voicemail

by Jpiterak

Here are a couple of scripts I use to do callback voicemail to my users. This was a request from a customer who was looking at a similar feature on an Avaya system.

How it works:

    • Instead of calling Voicemail() directly, I set up a macro 'macro-leave_voicemail' that makes the Voicemail() call, then checks to see if you want to send a call to the user.
    • If callback is set, the macro calls a bash script that creates the call file. This script:
      • Checks the voicemail.conf file for a specially formatted comment line after the mailbox definition with the telephone numbers to call for this user.
      • Creates a call file for each of these phone numbers. Note that the call file:
        • Puts the callee into a context in extensions.conf that tells them that they have a call waiting, and allows them to press 1 to go directly to voicemail.
        • Sets a variable '${mailbox}', which is used to enter the correct voicemailbox if the user chooses to listen to the voicemail.


voicemail_callback.sh <voicemailbox>

Requires that you add a line after each entry in voicemail.conf where you want the mailbox to get a callback, in the format
315 => 4513,Jim Doh,doh@megacorp.com,5555555555@messaging.nextel.com
callme = 5555555555,6666666666

  1. Allow for different contexts, as the config file does.
  2. Right now, anyone with the same mailbox # will get called :-)

if [ "x$1" != "x" ];then voicemailbox="$1"; else return 1; fi
cfg_line=$(grep -A 1 "^$voicemailbox" "$VOICEMAIL_CFG")
if ( echo "$cfg_line"|grep -E 'callme\W*=\W*[[:digit:]]+'>/dev/null);then
phnum_list=$(echo "$cfg_line"|grep -E 'callme = '|cut -d'=' -f 2|sed -e 's/,/ /g')
echo "$phnum_list"
return 1

if [ "x$1" != "x" ];then mailbox="$1"; else return 1; fi
if [ "x$2" != "x" ];then phone_number="$2"; else return 1; fi

  1. note use of '-' in '-EOF1' - Escapes tab at beginning of lines
CALLFILE=$(cat <<-EOF1
Channel: Zap/g1/$phone_number
MaxRetries: 2
  1. Retry in 5 min
RetryTime: 300
WaitTime: 45

Context: feature-voicemail_callback
Extension: s
Priority: 1
SetVar: mailbox=$mailbox


echo "$CALLFILE" >> "$CALL_SPOOL_DIR"/$(date +%Y%m%d-%I%M%S)-$phone_number



  1. CALL_SPOOL_DIR=tst/
  2. VOICEMAIL_CFG="./asterisk/voicemail.conf"

  1. *********MAIN*******************************************************

if [ "x$1" != "x" ];then voicemailbox="$1"; else exit 1; fi

if ph_numbers=$(get_phone_numbers $voicemailbox) ;then
for ph_num in $ph_numbers; do
make_callfile "$voicemailbox" "$ph_num"

In extensions.conf:

  • A macro that is used to call app_voicemail, and which calls the bash script to initiate the call out.

  ; Leave a voicemail message, then do post-processing.
  ;   o Call configured phones, with an announcement that a message
  ;      is waiting, and the option to listen to the voicemail(s)
  ;        ${ARG1} = u or b for 'unavailable' or 'busy' message
  ;        ${ARG2} = mailbox
  ;	 ${ARG3} = Call user flag
  ; USAGE:
  ;	exten => s,15,Macro(leave_voicemail,u,310,1)

  exten => s,1,ResponseTimeout(30)
  exten => s,2,Voicemail2(${ARG1}${ARG2})
  exten => s,3,GoToIf($[${ARG3} = 0]?s|5)
  exten => s,4,system(${SCRIPTS_DIR}/voicemail_callback.sh ${ARG2})
  exten => s,5,NoOp

  exten => h,1,GoToIf($[${ARG3} = 0]?h|3)
  exten => h,2,system(${SCRIPTS_DIR}/voicemail_callback.sh ${ARG2})
  exten => h,3,NoOp
  exten => t,1,GoToIf($[${ARG3} = 0]?t|3)
  exten => t,2,system(${SCRIPTS_DIR}/voicemail_callback.sh ${ARG2})
  exten => t,3,NoOp

  • Note that the h and t extensions may not be necessary... I used them for troubleshooting problems with call progress detection on FXO cards (More on that later... Down in 'Please Note!')
  • The context that is connected to by the call file:

exten => s,1,ResponseTimeout(2)
exten => s,2,Background(vm_callback-announcement)
exten => s,3,Background(vm_callback-options)
exten => t,1,Goto(s|2)

exten => 1,1,VoicemailMain2(s${mailbox})

  • Finally, the sound file contents:

vm_callback-announcement: "You have new voicemail"
vm_callback-options: "To listen to this voicemail, press 1"

Created by: oej, Last modification: Tue 26 of Jun, 2012 (04:04 UTC) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+