The dialplan you just built was static; it will always perform the same actions on every call. We are going to start adding some logic to our dialplan so that it will perform different actions based on input from the user. To do this, we’re going to need to introduce a few more applications.
The Background(), WaitExten(), and Goto() Applications
One of the most important keys to building interactive Asterisk dialplans is the Background()# application.
Like Playback(), it plays a recorded sound file. Unlike Playback(), however, when the caller presses a key (or series of keys) on her telephone keypad, it interrupts the playback and goes to the extension that corresponds with the pressed digit(s).
If a caller presses 5, for example, Asterisk will stop playing the sound prompt and send control of the call to the first priority of extension 5.
The most common use of the Background() application is to create voice menus (often called auto-attendants orphone trees). Many companies use voice menus to direct callers to the proper extensions, thus relieving their receptionists from having to answer every single call.
Background() has the same syntax as Playback():
exten => 123,1,Answer()
exten => 123,n,Background(main-menu)
In earlier versions of Asterisk, if the Background() application finished playing the sound prompt and there were no more priorities in the current extension, Asterisk would sit and wait for input from the caller. Asterisk no longer does this by default.
If you want Asterisk to wait for input from the caller after the sound prompt has finished playing, you can call the WaitExten() application. The WaitExten() application waits for the caller to enter DTMF digits, and is frequently called directly after the Background() application, like this:
exten => 123,1,Answer()
exten => 123,n,Background(main-menu)
exten => 123,n,WaitExten()
If you’d like the WaitExten() application to wait a specific number of seconds for a response (instead of using the default timeout), simply pass the number of seconds as the first argument to WaitExten(), like this:
exten => 123,n,WaitExten(5)
Both Background() and WaitExten() allow the caller to enter DTMF digits. Asterisk then attempts to find an extension in the current context that matches the digits that the caller entered. If Asterisk finds an unambiguous match, it will send the call to that extension. Let’s demonstrate by adding a few lines to your example:
exten => 123,1,Answer()
exten => 123,n,Background(main-menu)
exten => 123,n,WaitExten()
exten => 1,1,Playback(digits/1)
exten => 2,1,Playback(digits/2)
exten => 3,1,Playback(digits/3)
If you call into extension 123 in the example above, it will play a sound prompt that says “main menu.” It will then wait for you to enter either 1, 2, or 3. If you press one of those digits, Asterisk will read that digit back to you. You’ll also find that if you enter a different digit (such as 4), it won’t give you what you expected.
Dial extension 123, and then at the main menu prompt dial 1. Why doesn’t Asterisk immediately read back the number one to you? It’s because the digit 1 is ambiguous;
Asterisk doesn’t know whether you’re trying to go to extension 1 or extension 123. It waits a few seconds to see if you’re going to dial another digit (such as the 2 in extension 123). If you don’t dial any more digits, Asterisk will eventually time out and send the call to extension 1.
Before going on, let’s review what we’ve done so far. When users call into our dialplan, they will hear a greeting. If they press 1, they will hear the number one, and if they press 2, they will hear the number two, and so on. While that’s a good start, let’s embellish it a little. We’ll use the Goto() application to make the dialplan repeat the greeting after playing back the number.
As its name implies, the Goto() application is used to send the call to another part of the dialplan. The syntax for the Goto() application requires us to pass the destination context, extension, and priority on as arguments to the application, like this:
exten => 123,n,Goto(context,extension,priority)
Now, let’s use the Goto() application in our dialplan:
exten => 123,1,Answer()
exten => 123,n,Background(main-menu)
exten => 1,1,Playback(digits/1)
exten => 1,n,Goto(incoming,123,1)
exten => 2,1,Playback(digits/2)
exten => 2,n,Goto(incoming,123,1)
These two new lines (highlighted in bold) will send control of the call back to the 123 extension after playing back the selected number
If you look up the details of the Goto() application, you’ll find that you can actually pass either one, two, or three arguments to the application.
If you pass a single argument, Asterisk will assume it’s the destination priority in the current extension. If you pass two arguments, Asterisk will treat them as the extension and priority to go to in the current context.
In this example, we’ve passed all three arguments for the sake of clarity, but passing just the extension and priority would have had the same effect.
Comments
Post a Comment