Dialplan

DIALPLAN

You all now have some familiarity with the Asterisk Dialplan. It is what you are using to construct the logic that occurs when a call comes to your extension.

Generally the dialplan is defined in a configuration file called: /etc/asterisk/extensions.conf. For us, I am dynamically including each of your dialplan files into this file when we “reload” asterisk through the web page that was setup. For all practical purposes, you are directly editing that file when you write your own dialplan files.

Let’s revisit some of the dialplan concepts we talked about last week:

Context

A context is a central idea to asterisk dialplans. It defines a section of the dialplan that any incoming or outgoing call is executing. There can be hundreds of contexts defined and logic within those contexts can move calls from one to another.

For instance, when a call comes in via our number, it is put into the context that our SIP service is registered to:

[inbound]
; ITP REDIAL CONTEXT
exten => 19175346464,1,Answer()
exten => 19175346464,n,Wait(1)
exten => 19175346464,n,Goto(itp-redial,s,1)

This context is defined in /etc/asterisk/extensions.conf if you want to have a look.

This context does a couple of things but most important is that it asks the caller for an extension and then uses the “Goto()” command based on the extension that was entered to send the call to another context (the ones we have defined).

exten => 10,1,Goto(ck987,s,1);

If they enter extension 10, it goes into the context that I have defined for myself with the “s” extension for start and to the command with priority 1.

Extensions

Extensions are the means that asterisk uses to match a command within a context. There are a couple of special extensions as follows:

s – The “start” or “special” extension. Where things generally start within a context. More
t – The “timeout” extension. If a Digit or Response timeout has occurred. More
i – The “invalid” extension. If the user enters an extension that isn’t matched. More
h – The “hangup” extension. Executes after a call has hungup. More

Priority

Within a context and an extension Asterisk uses priorities to determine which command to run.

Generally these go in order from 1 to 2 and so on. Last week we talked about the “n” priority which is essentially the previous priority + 1 which makes it much easier to work with dialplans.

[itp-redial]
exten => s,1,Answer();
exten => s,n,Wait(1);
exten => s,n,Background(vm-extension);
exten => s,n,WaitExten(10);
exten => t,1,Goto(s,3);
exten => i,1,Playback(invalid);
exten => i,n,Goto(s,3);

exten => 10,1,Goto(ck987,s,1);
exten => 123,1,Goto(abc123,s,1);
;and so on..

There is also the ability to label priorities which allows you to jump to priorities without knowing the number (such as when you use the “n” priority). This would makes Goto commands easier to deal with:

[itp-redial]
exten => s,1,Answer();
exten => s,n,Wait(1);
exten => s,n(PlayExtension),Background(vm-extension); Defines the PlayExtension label
exten => s,n,WaitExten(10);
exten => t,1,Goto(s,PlayExtension); Go to the PlayExtension label instead of priority 3
exten => i,1,Playback(invalid);
exten => i,n,Goto(s,PlayExtension); Go to the PlayExtension label instead of priority 3

exten => 10,1,Goto(ck987,s,1);
exten => 123,1,Goto(abc123,s,1);
;and so on..

More Information: Asterisk Priorities – voip-info.org

Variables

You can use variables in the dialplan to have greater flow control over a dialplan.

[itp-redial]
exten => s,1,NoOp,${CALLERID(NAME)}
exten => s,n,Set(TIMEOUT(digit)=5); Global TIMEOUT variable for digits
exten => s,n,Set(TIMEOUT(response)=10); Global TIMEOUT variabe for response time
exten => s,n,Set(TIMEOUT(absolute)=600); Global TIMEOUT variable for total call length
exten => s,n,Set(numtimeouts=0); Local variable, nutimeouts that keeps track of number of timeouts
exten => s,n,Answer();
exten => s,n,Wait(1);
exten => s,n(PlayExtension),Background(vm-extension);
exten => s,n,WaitExten(10);
exten => t,1,Set(numtimeouts=$[${numtimeouts} + 1]);  Update the numtimeouts variable
exten => t,n,GotoIf($[${numtimeouts} >= 4]?999,1);  If the number of timeouts is greater than 4 jump to extension 999
exten => t,1,Goto(s,PlayExtension);
exten => t,n,Goto(s,PlayExtension);
exten => i,1,Playback(invalid);
exten => i,n,Goto(s,PlayExtension);	

exten => 10,1,Goto(ck987,s,1);
exten => 123,1,Goto(abc123,s,1);
;and so on..

exten => 999,1,Hangup(); Hangup if they get here
exten => T,1,Hangup(); Absolute Timeout occurred

Variable Name: numtimeouts
Set a variable: Set(numtimeouts=0)
Retrieve a variable: ${numtimeouts}
Use a variable in an expression: $[${numtimeouts} + 1]

Lots More Information: Asterisk variables – voip-info.org

Applications and Commands

Rather than go in-depth into all of the applications available this week, I would rather you explored them on your own. Here are some suggestions for exploration: Answer, Wait, DigitTimeout, ResponseTimout, Background, Playback, Goto, GotoIf, Hangup, DateTime, Record, Monitor, NoOp, GotoIfTime

You can retrieve a handy dandy list of all dialplan applications through the core show applications command on the console: (Don’t forget Appendix B in the Asterisk book as well as the voip-info.org wiki

Next week we will get into Macros, Pattern Matching, MusicOn Hold, Database Interaction, Outbound Calling (Dial), MP3Player, Queues, Conference Rooms, Streaming, System Commands and so on.