Playing sounds from a script

An IRC user wanted to be able to periodically send sounds to a paging device.

Having never done it, I decided I’d like to give it a shot, so here’s what I did.

First, create an extension that you can dial that plays the sound you’d like to hear. I used extension 26669 because it spells “annoy.” Put it in extensions_custom.conf in the [from-internal-custom] context:

exten => 26669,1,Answer
exten => 26669,n,Wait(2)
exten => 26669,n,Playback(tt-monkeys)
exten => 26669,n,Wait(2)
exten => 26669,n,Hangup

Now, reload asterisk and dial 26669 to test. If it doesn’t play the file then something is wrong. Fix that before you go on.

Next, we need to write some code. I’m using perl with the Asterisk::Manager module. You will probably have to install this module. It isn’t hard, and if you need help just ask.

#!/usr/bin/perl
use Asterisk::Manager;

my $mgrusr = 'admin';
my $mgrpass = 'pass';
my $mgraddr = 'localhost';
my $channel='Local/26669@from-internal-custom';
my $ext='555';
my $context='from-internal';

my $astman = new Asterisk::Manager;
$astman->user($mgrusr);
$astman->secret($mgrpass);
$astman->host($mgraddr);
$astman->connect or die "Could not connect to " . $astman->host .  "!\n";
print STDERR $astman->sendcommand( Action => 'Originate',
                                        Channel => $channel,
                                        Exten => $ext,
                                        Context => $context',
                                        Priority => '1' );
$astman->disconnect;

I called this “callme” and put it in /usr/local/bin. Now it can be called from cron.

The Exten => line is where you tell it to call. This can be a single extension, a page group, or whatever you want. You can modify the code slightly so that you can pass options to it, you can define multiple announcement extensions in extensions_custom.conf or whatever. This is just the basic idea.

HTH somebody.