Categories

Archives

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-context
context 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.

3 people like this post.

Other posts related to this topic

  • Proxmox and Two Subnets on the Same Network Interface, Properly Routed
    This was a little bit tricky, but thanks to the power of the freely available (though hard to find sometimes) knowledge on the Internet, and Google's searches, I just finished this task and thought to write a blog about it. Though the following is fo...
  • Setup SNMP on an Asterisk server
    SNMP - Simple Network Monitoring Protocol, as it name suggests, is a protocol used to monitor various properties of network equipment. These properties are identified by something called OIDs (Object Identifiers) which are long numeric strings and id...
  • Phone Reminders – Make your life easier
    A phone reminder is a reminder which is sent to you over the phone. It amazingly makes various things in your life more manageable which otherwise would stay mismanaged because you simply forget about them. Life is busy for everyone in this age, an...
  • The h Extension
    The h extension is a great extension, and when a call is hung up by any of the parties, i.e. either by the caller or by the callee, asterisk dialplan looks for the h extension before hanging up the call. Same is true for a conference call, when a use...
  • Keep track of number of active calls in Asterisk
    How to keep track of the total number of calls in Asterisk? There is no predefined variable yet which keeps track of this very important piece of information, which is sometimes very critical to make certain decisions in a dialplan. There are vari...
  • Scaling an Asterisk installation
    Scaling basically means that if the hardware in your system can't handle any more load, then the system should be able to take additional hardware and share extra load with it. In case of VoIP calls, if you server is capable of handling, lets say 300...
  • What to do when your asterisk is hacked
    There are various types of malicious hacks on Linux systems. When dealing with Asterisk server, most common one is that of registering a SIP extension from a hacker's machine and use it to make expensive international calls. This is financi...
  • Securing Asterisk – Fail2Ban
    Fail2Ban from www.fail2ban.org is a great tool to block unwanted IP addresses from accessing your server. It works along with iptables, and checks the log files for predefined patterns, and on finding a matching pattern blocks the IP address ...
  • SIP on Android / VoIP Client
    One of the first apps which I installed on my Milestone/Droid was sipdroid. This app is a softphone and very easy and straight forward to use. To my surprise the voice quality on it was just fine, though it uses ulaw, which consumes the most bandwidt...
  • Monitoring Asterisk using AstAssisstant
    By the time of this posting (June 2010 ) there is still a great need for a good open source monitoring solution for Asterisk. Somebody had written Flash OP many years ago but it never evolved beyong a certain point, leaving it very ugly looking and v...

6 comments to Getting started with AEL

  • RT

    Zeeshan, Thanks for such a benign intro. I too was apprehensive about dialplan and syntax. AEL is a good thing, but I do not see any conceptual documentation of AEL (the way it works – or how does it co exist with extensions.conf) The vioi-info is a good place but I wanted to know if any officially supported document was there? The Asterisk site does have it. The way you have mentioned looks better to me but would prefer more elaborated details, if possible.

    Can both co-exist?

    • You are welcome Rajeev.

      Unfortunately there is no detailed documentation on AEL. The only one page into to AEL on voip-info is all where I learnt it from, and later gained experience by practice. Actually its not that hard. All I can suggest is start writing small dialplans and soon you’ll be good at it. If you have an existing dialplan, start converting it into AEL. All you have to do is delete extensions.conf and Asterisk will automatically look for extensions.ael (unless your version of Asterisk requires some other configuration, I never moved beyond Asterisk 1.4).

  • Its a good suggestion. It has been a while since I used a high availability solution the Linux way, i.e. using heartbeat and DRBD. But sure I’ll explore my old notes and write something on it and update you on it.

  • Dear Zeeshan,
    Would you share your knowledge and experiences on asterisk High Availability solutions too?
    thanks.

  • Very Useful for me to start AEL Programming .
    Thanks

    Regards

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free