Asterisk IP PBX Learning Tutorials - Part 6

Here is video number 5: Using Contexts and Dial Plans - Part 2

Contexts and dialplan Part 2

Ok, moving to next context which is called “incoming-calls-wildcard”. Lets imagine situation when you have, say, hundreds of extensions, and writing rules for each one of them can take really much time. In this case wildcards are coming to save us.

Lets take a look at single rule with 2 steps in “incoming-calls-wildcard” context, as you can see I have changed last digit of dialled extension to capital “X”, which is any number from 1 to 9 and 0. But you also have to tell asterisk that this rule contains wildcard, you do it with putting subscribe sign in the beginning. But how can we tell asterisk what to dial when we have wildcards in our rules? We use built-in variables, one of them is ${EXTEN}. This is the way all variables are referenced in asterisk – dollar sign, opening curly bracket, variable name and closing curly bracket.

This particular variable contains number that is being dialled in the rule, so if you are dialling 205, then the value of EXTEN variable will be 205. Using this expression we can address all the extensions from 200, 201 and to 209 and they all be processed by the same rule, so we can save up to 10 times in the means of our space and time.

On the other hand what if you do not want to address all 10 devices in range between 200 and 209? The you can use another expression to exactly point to number you want to be accessed. Putting desired numbers in square brackets in dial rule will make asterisk serve only numbers you have specified. Here you can use 2 ways to address extensions, either you number them one by one, like 2 and 3 here, or you put a range using minus sign, here 6-8 means that all the numbers from 6 to 8 will be included in this dialling rule, those are 6, 7 and 8. And as you can see you can mix both of the ways in a single dialling rule definition. So, this particular rule will address extensions 203, 204, 206, 207 and 208. And remember that in order to tell asterisk that you use wildcards or expressions in your rule you have to prepend rule with underscore.

The next context shows us another wildcard in asterisk. Here dot (.) represents any number of any digits will follow. So overall extension will be starting with 1 and having at least 4 digits. Also, as you can see here we use another way to dial SIP peer. In most cases when you use VoIP provider you will have to address it as an extension but will pass it a number to dial out. Here we assume that you have FWD as your VoIP provider defined in sip.conf configuration file and here is a way to address it. As usual you tell asterisk that we are using SIP technology, going through FWD provider and dialling extension that has been dialled on phone.

Another important point I would like to talk about is context inclusion. As we previously saw context [dial-out] was included in first context [incoming-calls], that means that any rule defined in “dial-out” context will be present in [incoming-calls]. This feature also saves you a lot of time and makes your dialplan easily readable and understandable. For example you can have 2 up providers for dialing out, one is a cheap one and another is not so cheap but with business quality and you want everybody except you only use cheapest provider, what you do is you define 2 contexts for dial-out, say “dial-out1” and “dial-out2”, putting each provider in separated context, and you define a context to reach your local phones. Then you define 2 contexts to put there calls from your extensions and from yourself. And then comes inclusion, in context for your other extensions  you include rules to dial local phones and cheap outside provider, but in context for yourself you include business quality provider and  rules to dial local extensions. As you can see contexts and inclusion  are a very powerful tool for assuring access control, too.

But lets move on and take a look at other actions that can be taken with placed call in dialplan. To be honest there are a huge number of options on what you can do with your call and covering them all would take days or weeks, in the end of these videos I will provide you with some good sources of information on this topic so you can dig it by yourself.

But for now there is an interesting thing I want to tell you about, it is called AGI or Asterisk Gateway Interface, much like CGI or Common Gateway Interface, which is used to serve dynamic content on the web, AGI is used to execute any action you would like via extending your asterisk’s capabilities with custom scripts. You can do absolutely anything with those scripts, like contacting your database, checking with some webpage to get information and every other thing you would like to. But for sure this assumes that you have some script writing skills. The good news is that AGI scripts can be written in any programming language you like, as asterisk communicates with them using standard input and output, so as long as your system is able to compile and execute your script, asterisk will be able to use it in dialplan.

AGI is pretty simple to use, as you can see, you just put AGI command in your dialplan and that’s all, entering this step while processing the call will make asterisk to call this script and to start communicating with it.

The Asterisk Gateway Interface is most powerful tool in asterisk as it allows you to handle the call in that particular way you want it. I will provide more info on where to look for information on AGI programming at the end of the videos.

Related posts:

  1. Asterisk IP PBX Learning Tutorials - Part 5
  2. How to Use Asterisk Software IP PBX - Part 4
  3. Asterisk IP PBX Learning Tutorials - Part 4
  4. How to Use Asterisk Software IP PBX - Part 5
  5. Asterisk IP PBX Learning Tutorials - Part 10

Discussion Area - Leave a Comment

You must be logged in to post a comment.