• Aucun résultat trouvé

The Background(), WaitExten(), and Goto() Applications

Dans le document Asterisk : The Future of Telephony ™ (Page 155-158)

One of the most important keys to building interactive Asterisk dialplans is the Back ground()# 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 or phone trees). Many companies use voice menus to direct call-ers to the proper extensions, thus relieving their receptionists from having to answer every single call.

In fact, if you don’t have any channels configured, now is the time to do so. There is a real satisfaction that comes from passing your first call into an Asterisk system that you built from scratch. People get this funny grin on their face as they realize that they have just created a telephone system. This pleasure can be yours as well, so please, don’t go any further until you have made this little dialplan work.

#It should be noted that some people expect that Background(), due to its name, would continue in the dialplan while the sound is being played, but its name refers to the fact that it is playing a sound in the background, while waiting for DTMF in the foreground.

Building an Interactive Dialplan | 127

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 our example:

exten => 123,1,Answer()

exten => 123,n,Background(main-menu) exten => 123,n,WaitExten()

exten => 2,1,Playback(digits/2) exten => 3,1,Playback(digits/3) exten => 4,1,Playback(digits/4)

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 2, 3, or 4. 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 5), it won’t give you what you expected.

It is also possible that Asterisk will find an ambiguous match. This can be easily ex-plained if we add an extension named 1 to the previous 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) exten => 4,1,Playback(digits/4)

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. (We’ll learn how to choose our own timeout values in Chapter 6.) 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:

[incoming]

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.

Building an Interactive Dialplan | 129

Dans le document Asterisk : The Future of Telephony ™ (Page 155-158)