Call recording in Asterisk

I´ve been using asterisk for a while and it performs like a charm. Using a basic dialplan with the IAX protocal its time to add some call recording. There are at least two way to go about it:

  • Plain extentions.conf ’scripting’. This file is being pulled through some kind of parser -everytime- a call is being requested. Furthermore all kind of system variables are available. If your familiar with PHP you can compare them to $_GLOBALS.
  • Using macro´s. Usually I’ am not a fan of macros, but this is the exception to the rule. They greatly enhance readability of your dialplans and are similar to PHP functions.


Cut-out extentions.conf
exten => _0034[9]XXXXXXXX,1,Verbose(1|vsp-out)
exten => _0034[9]XXXXXXXX,n,NoOp()
exten => _0034[9]XXXXXXXX,n,MixMonitor(${SCREEN_FILE},b)
exten => _0034[9]XXXXXXXX,n,Dial(SIP/vsp/${EXTEN})
exten => _0034[9]XXXXXXXX,n,StopMonitor()
exten => _0034[9]XXXXXXXX,n,Hangup()

For every outbound request you start with defining a filename for the recording eg. SCREEN_FILE. In this example I used a combination of system variables to make up a filename. By default they are put into a wav format but with some extras realtime convertion to mp3 is possible.
Second you have to start the recording; this is done with MixMonitor() which takes, among others, the filename as an parameter. By default all files are stored in /var/spool/asterisk/monitor, which is defined in asterisk.conf. Finally the recording is stopped with StopMonitor().


There comes a time, especially when you´re getting the hang of asterisk scripting ;), and feel the power of a fully fledged PABX at the tip of your fingers, rewriting of your extentsion.conf is needed. I mean really needed! After building all kind of nifty things like demo accounts to test the quality of speach/connections through Echo(), or rotating huntgroups, playing back welcome messages before actually any call is taken with Play(ing)back() the weasels, IVR´s and making your server crazy doing all kind of silly stuff controlled by DTMF… just to name a few..
Ok, uh macros… right! ;) Here it comes:

exten => s,n,NoOp()
exten => s,n,Verbose(1|VR ${ARG1}/${ARG2} ${SCREEN_FILE})
exten => s,n,MixMonitor(${SCREEN_FILE})
exten => s,n,Dial(SIP/${ARG2}/${ARG1},20)
exten => s,n,StopMonitor()
exten => s,n,Hangup()

In order to get a macro called you’ll have to give it a proper name. Although they both appear in extentions.conf, written between [], asterisk distinguishes between a ‘context’ and a macro by its name prefix. So in this example the macro ‘record’ is defined as [macro_record]. It´s basically a rewrite, with some extra tweaks but more importantly it uses a variable ARGS2 for the extention being dialed.

The first dialplan can now be rewritten in just two lines:

exten => _0034[9]XXXXXXXX,1,Verbose(1|vsp, CV)
exten => _0034[9]XXXXXXXX,n,Macro(record,${EXTEN},vsp)

So if your need to add another country with a different VSP you could use:

exten => _0044[6]XXXXXXXX,1,Verbose(1|vsp2, CV)
exten => _0044[6]XXXXXXXX,n,Macro(record,${EXTEN},vsp2)

You probably have noticed all the Verbose() lines. I use them to ‘debug’ while testing in the asterisk console. I you feel confident about your creations you can erase them. Effectively reducing a channel selection with call recording to one single line! Cool hè

Asterisk dialplan
Asterisk macros

July 21st, 2008 - Posted in asterisk | | 0 Comments

Installing phpagi 2.x

I’am using Debian/4, Apache/2.2.3, PHP 5.2.5-0.dotdeb.2 and Asterisk/1.4.13

Download agi-php 2.x files from

Adjust agi script files location set in asterisk.conf (a simple reload won’t do the job, so restart Asterisk afterwards)

astagidir => /var/lib/asterisk/agi-bin

Create a context in the dialplan for your agi scripts in extention.conf
exten => 800,1,Verbose(1|AGI)
exten => 800,n,agi(dtmf.php)
exten => 800,n,Hangup()

and include it in your incoming context with
include => agi

Before you can use the examples eg. dtmf.php a few adjustments are needed:

1. Scripts must be executable: chmod 755 dtmf.php

2. All php files are run by Asterisk, as if started from the console, so test that by actually running it from the console.

I had to edit dtmf.php to make a few changes:
Change the interpreter location from
#!/usr/local/bin/php -q
#!/usr/bin/php5 -q

Change the php include path to let the script find the file phpagi.php with
ini_set( "include_path", "/your_folder_to_phpagi.php/" );

Install speech engine
I followed the installation guide available at

apt-get install festival
Comment out ;exit 0

Possible errors and solutions

Error message:
Failed to execute ‘/var/lib/asterisk/agi-bin/dtmf.php’: No such file or directory

Either check your astagidir in asterisk.conf
or make the dtmf.php executable with chmod 755
or check the line “#!/usr/bin/php5 -q”

Error message:
WARNING[26320]: file.c:563 ast_openstream_full: File /var/spool/asterisk/tmp//text2wav_ace75969fc9b3a79aef4da4291ca0646 does not exist in any format.
Festival is not started or installed (Remember the exit 0; ?)


Update 07/12/2011:

The Festival installation guide at has been moved to

June 26th, 2008 - Posted in asterisk, voIP | | 2 Comments

Installing spanish voices for festival speech synthesis system

On this blog I found the packages to install spanish voices for Festival. No apt-getting but *.deb files, learn how to install these…

June 26th, 2008 - Posted in asterisk, voIP | | 0 Comments