Monday, November 8, 2010

Getting started with AEL

AEL stands for Asterisk Extensions Language. It is intended to make writing dialplans easier than the standard syntax used in extensions.conf. The standard syntax is not very user friendly, makes it difficult to write complex dialplans, and makes it even more difficult to read those dialplans. AEL on the other hand makes writing dialplans much easier and faster, plus its syntax is like a programming language which programmers are more familiar with.


It is but strange that AEL hasn’t got well adapted by the asterisk community and most of the community members still struggle to deal with the standard syntax to write dialplans, which becomes hard to deal with as the dialplan grows. Learning AEL is very simple, and those who know the standard syntax, and any programming language, they can easily switch to AEL in a few hours.


I have learnt AEL primarily from this page of voip-info.org: http://ilovetovoip.com/UP, and still use it for my reference when needed. I would suggest you go over this page once to better understand AEL.


Here I’ll attempt to describe some basics on how to start writing in AEL.


WRITING A CONTEXT


First of all, like main dialplan goes into extensions.conf if you are writing in standard syntax, the AEL code goes into extensions.ael. If this file doesn’t exist in your /etc/asterisk folder, create it and assign necessary ownership and permissions if required. Usually as long as it is readable by all the users, it is fine.

touch /etc/asterisk/extensions.ael

In extensions.conf, we write code in sections called contexts which look like follows:

[test-context]exten => 111,1,Answer()exten => 111,n,MusicOnHold()exten => h,1,NoCDR()exten => h,n,Hangup()

The repetition of ‘exten => 111,…’ is completely unnecessary and sometimes annoying. It adds to the visual complexity of the code even if the code is very simple. The same thing in AEL will be as follows:

context test-context => { 111 => { Answer(); MusicOnHold(); }; h => { NoCDR(); Hangup(); };};

Now isn’t this AEL code much better looking and clearer than the standard version? Maybe not for this small example, but as the dialplan grows, it does.


In AEL we define a context block like we do in most familiar programming languages, i.e. inside curly braces, and then within them define blocks of extensions. Each line ends in semi-colon like in many programming languages. In this test context we have extension 111 which triggers MusicOnHold application, and extension h which hangs up the call after making sure that the call is not recorded into the CDR. Now any extension defined in sip.conf or sip_buddies tables (in case you are using real-time architecture) whose context is set to be test-context, will be able to make use of this context.


When we modify the extensions.ael file, in the asterisk CLI we give command ‘ael reload’ to make the modifications made in the file workable.All the commands used in AEL are the same as the standard context.


WRITING MACROS


Once you know how to write contexts, next logical thing which comes to mind is how to write macros. It is also very simple and straight forward. Macros are like functions or methods and we define them using keyword macro, followed by possible arguments which it will receive in round brackets, or leave the brackets empty if it’ll not receive any arguments.

macro play-moh () { MusicOnHold();};[macro-play-moh]exten => s,1,MusicOnHold()

CALLING A MACRO


To call a macro in AEL, we use an ampersand sign followed by macro name and arguments, or empty bracket if there are no arguments. For example, if we combine the above context and macro it’ll look like following:

// Comment: the test-contextcontext test-context => { 111 => { Answer(); &play-moh(); }; h => { NoCDR(); Hangup(); };};macro play-moh () { MusicOnHold();};///////////////////////////////

I M P O R T A N T: THE catch STATEMENT IN MACROS
It is a matter of common confusion amongst the new users of AEL on how to use special extensions like h, t or i in a macro. This is accomplished using the catch statement as follows:

macro play-moh () { MusicOnHold(); catch h { NoCDR(); Hangup(); };};

LABELS AND if, else, goto STATEMENTS


Labeling is also much better in AEL. You name a label followed by colons, and then use goto statement to jump to it. This is true both for contexts and macros. Consider the following example in standard syntax:


[label-example]

exten => 111,1,Set(COUNT=0)exten => 111,n(add),Set(COUNT=$[${COUNT}+1])exten => 111,n,GotoIf($["${COUNT}">"5"]?4:5)exten => 111,n,Goto(end)exten => 111,n,SayDigits(${COUNT})exten => 111,n,Goto(add)exten => 111,n(end),Hangup()exten => h,1,NoCDR()exten => h,n,Hangup() 

This is a counter, which will count up to 5 and then jump to h extension to hangup the call. It has two labels: add and end.


It looks pretty confusing, and specially repetition of  ’exten => 111,n‘ doesn’t look nice at all, it never did.


The same code in AEL will look like follows:


context label-example {

111 => { Set(COUNT=0); add: Set(COUNT=$[${COUNT}+1]); if("${COUNT}">"5") { goto end; }; SayDigits(${COUNT}); goto add; end: Hangup(); }; h => { NoCDR(); Hangup(); };};

Now it looks much better, well organized, easy to read and understand what is going on. In other words, it looks more professional. Observe carefully how the labels are used here, which I have highlighted in bold letters.


You will also notice the use of the familiar if statement, something which is used in all the programming languages, and it is pretty obvious how it is working.


CONCLUSION


You get an idea what AEL is and how it is different and better than the standard syntax of dialplan coding. Switching to AEL is very easy, and it enables you to write your dialplans much better, faster and easier.

No comments:

Post a Comment