More Dialplan

Last week we went over several commands/applications that can be used in the Asterisk Dialplan. This week I want to go over some of the more difficult or advanced features.

Predefined Channel Variables:
Under the heading “Predefined Channel Variables” this page lists all of the variables that are predefined for you. You can use these variables in your dialplan logic.

A good way to get started is to use them with the NoOp command. This will output them on the Asterisk console so you can see their values:

exten => s,n,NoOp(${CALLERID(all)})
exten => s,n,NoOp(${CALLERID(num)})
exten => s,n,NoOp(${SIPUSERAGENT})
exten => s,n,NoOp(${EXTEN})

Database Interaction:
Last week we discussed variables and so on. The problem with variables is that they go away once a call is completed. To save data you can use the AstDB.

To put a value in the AstDB you use the Set command (just like variables) but also use the DB command inside the Set command, like so:

exten => s,1,Set(the_date=${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)})) ;creates current date
exten => s,n,Set(DB(ck987/lastcalltime)=${the_date}); saves date to database

This sets a value in the “ck987” family called “lastcalltime”. The value of the variable is the current date and time (a local variable).

I can get the value of this out of the database by using the following command:

exten => s,n,Set(LASTCALLTIME=${DB(ck987/lastcalltime)})

This would set the value in a variable called LASTCALLTIME which tell me the last time someone called and the above code got executed.

Say I wanted to keep a log of how many times anyone did something on the system. I could do the following:

exten => s,n,Set(DID_SOMETHING=${DB(ck987/something)})
exten => s,n,Set(DID_SOMETHING=$[1 + ${DID_SOMETHING}])
exten => s,n,Set(DB(ck987/something)=${DID_SOMETHING})

Outbound Dialing:
Asterisk can make outbound calls as well as receive incoming calls. To make an outbound call from your dialplan you use the Dial application.

exten => s,1,Dial(SIP/flowroute/16466429990,30,r);

The above command will always dial my cell phone. The command is Dial, the first argument the connection type which for our purposes will be SIP. The second is the SIP context to use for dialing. For our purposes it will always be “flowroute”. This is followed by the number or extension to dial. The 30 means wait 30 seconds and the “r” means indicate ringing to us.

You can also call your softphone in this manner. The IAX context will be what you defined in your iax.conf file. (IAX2 is the technically the name for IAX.)

exten => s,n,Dial(IAX2/ck987_iax,30,r);

Before the call is dialed you can very easily overwrite the Caller ID Number like this:

exten => s,1,Set(CALLERID(num)=12125551212); your caller ID is now (212)555-1212

This is where we start to get into the grey-area legality of VoIP. I don’t think this is technically illegal, but please be responsible with this functionality.

Asterisk cmd Dial –

Pattern Matching:
When you define an extension in Asterisk you can either choose to put the exact digits or use a pattern to match the extension.

  • The “_” indicates that we are using a pattern.
  • “X” matches any digit from 0-9
  • “Z” matches any digit from 1-9
  • “N” matches any digit from 2-9
  • The “.” matches anything one or more times.
  • Brackets “[” enclose ranges. “[13-5]” matches 1, 3, 4, and 5.

Using patterns allows you to capture user input. The exact input will always be in the EXTEN variable so you can use that in your logic.
This can be handy for setting up outbound dialing validation. For example the pattern “_1NXXNXXXXXX” will match any valid North American phone number.

exten => _1NXXNXXXXXX,1,Dial(SIP/flowroute/${EXTEN},30,r); dials any valid North American phone.

There is a very good write-up regarding pattern matching in the Asterisk book. Check Chapter 6 around page 125.

Recording Audio:

Record() – Records what a user says for a specified period of time.

exten => s,1,Record(/home/ck987/asterisk_sounds/rec/say_something.gsm,5,30)
  • The first argument is the name of the file with the format.
  • The next argument (5) is the amount of silence detection (in seconds)
  • The last argument (30) is the max amount of time to record. If the caller hits “#” at any time the recording will stop.

If you want Asterisk to save recorded audio when a call is hung up, then you must include a “k” as the last argument.

; "k" is included so that the audio is kept even if the caller hangs up.
exten => s,1,Record(/home/ck987/asterisk_sounds/rec/say_something.gsm,,,k)

Asterisk cmd Record –

Playback with Controls:
Plays an audio file with controls.

exten => s,n,ControlPlayback(/home/ck987/asterisk_sounds/ast_intro,1000,#,*,0,1,2)
  • The first argument is the name of the file to play
  • The second argument is the number of milliseconds to move per button push. (1000 milliseconds = 1 second)
  • The last arguments are the buttons for rewind, fast forward, stop, play and pause.

Asterisk cmd ControlPlayback

Monitoring/Recording a Channel:
Asterisk can record the audio of a call. By default it saves the audio in two separate files, one for each side of the “conversation”.

exten => s,1,Monitor(wav,/home/ck987/asterisk_sounds/rec/call_monitor);

This command will create two files, both in /home/ck987/asterisk_sounds/rec/. One will be named call_monitor-in.wav, the other call_monitor-out.wav.

Asterisk cmd Monitor Also checkout MixMonitor which mixes the two channels at: Asterisk cmd MixMonitor

Executing a System Command:
Asterisk can work with other applications running on the same machine. For instance the following command will use a program called LAME (lame ain’t an mp3 encoder) to convert a recorded audio file to an MP3 file (which might be useful for those working posting these files online).

exten => s,1,Record(/home/ck987/asterisk_sounds/rec/converter.wav,5,30)
exten => s,n,System(lame /home/ck987/asterisk_sounds/rec/converter.wav /home/ck987/asterisk_sounds/rec/converter.mp3)

Asterisk cmd System