Friday, April 2, 2010

Asterisk and Voicemail Broadcast/Blasting

This is a method I developed this morning for sending out a voicemail message in Asterisk to all users that have a mailbox. I created an extension (in extensions.conf) that allows a user to record a message and then the 'System' application is used to execute a Python script that generates a .call file for all of the users.

I've tested this using Asterisk 1.6.1.1 with about 30 users. This will soon be moving into production on a machine that currently has about 500 mailboxes which will eventually have close to 2,000 mailboxes. I'm not sure what kind of impact this will have on the system load since when using the .call files, it seems like Asterisk tries to push them through as quickly as possible. We'll be sure to try it late at night. =)


/etc/asterisk/extensions.conf
; Context for voicemail blasting system. -- 20100402 MAS
[vm_blast]
exten => _2XXXX,1,Voicemail(${EXTEN},s)


/etc/asterisk/extensions.conf
[default]
; Enter the voicemail blasting system. -- 20100402 MAS
exten => _29999,1,Answer
exten => _29999,2,Wait(1)
exten => _29999,3,Authenticate(1234,j)
exten => _29999,4,Set(RECID=${FILTER(0-9,${UNIQUEID})})
exten => _29999,5,Wait(1)
exten => _29999,6,Playback(dictate/record)
exten => _29999,7,Record(record/vm_blast-${RECID}:gsm)
exten => _29999,8,Wait(1)
exten => _29999,9,Read(CHOICE,vm-review,1)
exten => _29999,10,GotoIf($[${CHOICE} = 1]?13:11)
exten => _29999,11,GotoIf($[${CHOICE} = 2]?16:12)
exten => _29999,12,GotoIf($[${CHOICE} = 3]?5:8)
exten => _29999,13,System(nohup /var/lib/asterisk/vm_blast.py record/vm_blast-${RECID} &)
exten => _29999,14,Playback(auth-thankyou)
exten => _29999,15,Hangup()
exten => _29999,16,Playback(record/vm_blast-${RECID})
exten => _29999,17,Goto(8)


/var/lib/asterisk/vm_blast.py
#! /usr/bin/python

import sys
import os
import tempfile
import time

def main():
# check our arguments
if len(sys.argv) != 2:
print "Must be used with a recording in Asterisk's sound directory!"
sys.exit(1)
# wait a few
time.sleep(10)
# create temp directory
temp_dir = tempfile.mkdtemp(prefix='vm_blast.')
recording = sys.argv[1]
id = recording.split('/')[1]
# get a list of user's that have voicemail boxes
vm_users = os.popen('/usr/sbin/asterisk -rx "voicemail show users for default"')
for line in vm_users.readlines():
extension = line[11:16]
# make sure its a real extension number
if extension.isdigit():
# open our call file
ext_call_file = temp_dir + '/' + id + '-' + extension + '.call'
ext_call_file_handle = open(ext_call_file, 'w')
# write the call file
ext_call_file_handle.write('Channel: Local/' + extension + '@vm_blast\n')
ext_call_file_handle.write('Application: Playback\n')
ext_call_file_handle.write('Data: ' + recording + '\n')
ext_call_file_handle.write('AlwaysDelete: yes\n')
# close our call file
ext_call_file_handle.close()
# finally move our files into the outgoing directory
os.system('mv ' + temp_dir + '/* /var/spool/asterisk/outgoing/')
# remove temp directory
os.system('rm -rf ' + temp_dir)

if __name__ == '__main__':
main()


I've found this tool to be quite useful for formatting code type items: http://formatmysourcecode.blogspot.com/