Hack97.Automate Your Voicemail Greeting

Hack 97. Automate Your Voicemail Greeting

Use an AGI script with Asterisk to update your voicemail greeting automatically.

In the business world, people often update their voicemail greeting on a daily basis. For example, you might call a co-worker and be greeted with "You've reached the desk of Bob Smith. Today is Tuesday, August 16, and I am in the office today." You can imagine Bob's routine when he gets into the office in the morning: he verifies the current date, rehearses his new message a couple of times, and then calls into his voicemail and updates it. He probably stumbles over the words a couple of times, so he probably has to start over at least once.

This takes far more effort and time than I am willing to commit just to update the current date in my voicemail greeting. I've got more fun things to work on than that! Here's how you can use an AGI script with Asterisk and some home automation to keep your voicemail greeting up to date automatically, without lifting a finger.

7.11.1. Create the Sound Files

You can always use Asterisk's built-in text-to-speech engine to speak your message for you, but that is a little too cold. Instead, with a little work, you can have Asterisk play the appropriate sound files that you've recorded with your own voice. I used the sound recorder that comes with Windows to record several sounds: one for each day of the week (wday1.wav for Monday, wday2.wav for Tuesday, etc.); one for each month of the year (month1.wav for January, month2.wav for February, etc.); and one for each day of the month (1.wav for the first, 2.wav for the second, etc.). You'll also want to record a beginning for your greeting, and two different endings, one for when you're in the office and one for when you are out. I named these files start.wav, endnormal.wav, and endooo.wav.

When speaking these months and days, you might be surprised how difficult it is to get the words to flow together without sounding choppy. I finally started saying an entire date and then cropping the file at the appropriate place to get it to flow better when Asterisk plays it.

By default, Asterisk doesn't have a codec to play .wav files. Instead of installing a new codec, you can use the SoX sound converter [Hack #24] (http://sox.sourceforge.net/) to convert the files into the .gsm format that Asterisk can play with its default installation. Use the following command to convert a .wav file into a .gsm file:

 $ sox  foo.wav  -r 8000  foo.gsm  resample ql  

After you've converted all your sound files, create a directory called vm-sounds in /var/lib/asterisk/sounds and copy the files into it.

7.11.2. Motion Detection Code

I set up a motion detector right under my desk in my office. I use an excellent home automation package called MisterHouse (http://www.misterhouse.net) to monitor the motion detector. I know that if there is no motion in my office between 8 a.m. and 9 a.m., I am probably not going to be in the office that day. Here's the code file I give to MisterHouse to write a file if I'm in the office between those times:

 $office_movement = new X10_Item('A1'); # A1 is the X10 code my $office_presence_start = "8:00 AM"; my $office_presence_end = "9:00 AM"; my $office_presence_file = "office.presence.txt"; if (time_now($office_presence_start)) { # reset the file unlink $office_presence_file; } if (state_now $office_movement eq ON and time_greater_than($office_presence_start) and time_less_than($office_presence_end)) {  open PRESENCE, ">$office_presence_file";  print PRESENCE time();  close PRESENCE; } 

Save the previous code in a file and place it in the MisterHouse code directory. You'll need to reload MisterHouse to have it start using your code.

7.11.3. Dialing Greeting Code

Here is the code I use to control the creation of my dialing greeting. Note that it requires the Asterisk::AGI Perl module:

 #!/usr/bin/perl use strict; use Asterisk::AGI; my ($sec,$min,$hour,$mday,$mon, $year,$wday,$yday,$isdst) = localtime(time); my $yyyymmdd = sprintf ("%04d%02d%02d",$year+1900,$mon+1,$mday); my $month = $mon + 1; my $vm_sound_dir = "vm-sounds"; my $office_presence_file = "/path/to/file/office.presence.txt"; my $AGI = Asterisk::AGI->new; my %input = $AGI->ReadParse; my $greeting_start = "start"; my $greeting_end_in_office = "endnormal"; my $greeting_end_out_of_office = "endooo"; my $weekday_sound = "wday" . $wday; my $mday_sound = $mday; my $month_sound = "month" . $month; my @files_to_play = (); push @files_to_play, $greeting_start; push @files_to_play, $weekday_sound; push @files_to_play, $month_sound; push @files_to_play, $mday; if (-e $office_presence_file or $hour == 8) { push @files_to_play, $greeting_end_in_office; } else { push @files_to_play, $greeting_end_out_of_office; } foreach my $sound (@files_to_play) { $AGI->verbose("Playing sound $sound"); $AGI->stream_file("$vm_sound_dir/$sound"); } 

Save the code to a file named vmautomate.pl and place it in the /var/lib/asterisk/agi-bin directory. Add the following lines to your extensions.conf file, where 8001 is your extension and 100 is your voice mailbox number:

 exten => 8001,1,Dial(SIP/8001,20,rt) exten => 8001,2,AGI(vmautomate.pl) exten => 8001,3,Voicemail,100 

After you reload Asterisk, when you call your extension, your phone will ring for 20 seconds and then the AGI script will run. Depending on the time of the day and the presence of $office_presence_file, you should hear the appropriate greeting. You should replace your "regular" voicemail greeting with the default greeting so that it flows properly once it reaches the Voicemail directive.

It doesn't take long to imagine some cool functionality with AGI scripts. For example, you can take this a step further and have an AGI script that reads your calendar that you've published with iCal to see whether you're scheduled to be out of the office that day. You can even have Asterisk announce to a caller when you have free time during that particular day and suggest that the caller try back then.

Dave Mabe

VoIP Hacks
VoIP Hacks: Tips & Tools for Internet Telephony
ISBN: 0596101333
EAN: 2147483647
Year: 2005
Pages: 156

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net