Saturday, January 1, 2011

Asterisk 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 user exits, first asterisk looks for the h extension for the hung up leg of the call.


When you start writing some serious dialplans, you can’t get away by not knowing and using the h extension.


h refers to hangup. If you don’t have it defined in your dialplan, the call will still hang up, but with h extension you can do a lot more than just hanging up a call, e.g. the most important and most commonly use of h extension is to update the CDR before completing a hangup.


For an example, take a context name [my-calls] which is used to send and receive calls on an asterisk system for a local office with extension pattern 2XX.  Here the h extension will be used like this:

[my-calls]exten => _2XX,1,Verbose( - - - New call - - - )exten => _2XX,n,Dial(SIP/${EXTEN},60)exten => h,1,Verbose( - - - I am the h extension of the context [my-calls] - - - )exten => h,2,ResetCDR()exten => h,n,Hangup()

Personally I program only in AEL, and the above syntax is only for the sake of this blog entry, because most of the asterisk users still don’t know AEL. I can’t urge less to move to AEL, as it makes dialplan programming so much easier, simpler and cleaner.


The above in AEL will be as follows:

context my-calls { _2XX => { Verbose( - - - New call - - - ); Dial(SIP/${EXTEN},60); }; h => { Verbose( - - - I am the h extension of the context [my-calls] - - - ); ResetCDR(); Hangup(); };};

You can see that here the context [my-calls] has its own h extension, and as soon as a call in this context will hangup, or in technical terms a call will tear down, asterisk will display message on its screen ” – - – I am the h extension of the context [my-calls] – - – ” followed by updating the CDR record for this call, followed by hangup of the call.


You can also use the h extension in macros. The syntax for non-AEL syntax is the same as for the contexts, but for AEL, it is a little different and not very well documented. It took me a while to find out how to use it in a macro in AEL, and don’t want you to waste your time figuring it out, so here it is:

macro hangup-calls() {  Verbose( - - - This is how you hangup call in a macro when using AEL - - - ); catch h { ResetCDR(); Hangup(); };};

Basically you use the ‘catch’ statement, followed by the h block.


There are certain commands which you can’t use in the h extension. The ones I know from my experience include Wait, Playback, Backgroup, WaitExten. If you want to delay the hangup due to any reason, I have a trick for it in a separate blog, link to which you can see below.

No comments:

Post a Comment