5.1. A simple IVR

The standard Asterisk sound set includes a file called marryme.gsm, containing the announcement "Will you marry me? Press 1 for yes or 2 for no."[24]To build a "marriage proposal" application, the following dialplan will suffice:[25]
exten => 30,1,Answer()
exten => 30,2,Background(marryme)
exten => 30,3,Hangup()

exten => 1,1,Playback(thank-you-cooperation)
exten => 1,2,Hangup()

exten => 2,1,Playback(sorry)
exten => 2,2,Hangup()
If the caller dials extension 30, Asterisk answers and plays the file marryme.gsm. Through use of the Background() application, the user is allowed to enter input at any time during playback. The input is interpreted as an extension and the call is passed to that extension. If the caller presses 1, he hears "Thank you for your cooperation," after which Asterisk hangs up.

Differences between Playback() and Background()

Playback() (see Section B.84, “Playback()) only plays back sound files; input is ignored. Background() (see Section B.12, “Background()) plays sound files back while listening for caller input, which is interpreted as an extension as though it had been dialed as one.

Difference between 10 and 1000

To address the challenge of extensions beginning with the same digits, let's examine the following example:
exten => 30,1,Answer()
exten => 30,2,Background(marryme)
exten => 30,3,Hangup()

exten => 1,1,Playback(thank-you-cooperation)
exten => 1,2,Hangup()

exten => 10,1,NoOp(Test mit 10)
exten => 10,2,Hangup()

exten => 100,1,NoOp(Test mit 100)
exten => 100,2,Hangup()

exten => 2,1,Playback(sorry)
exten => 2,2,Hangup()
Background() waits a set time after each digit in order to distinguish between 1, 10 and 100. Once this time (TIMEOUT) has expired, input is deemed to be complete.

Tip

TIMEOUT lets you set other timeouts. For more information, enter show function TIMEOUT in the Asterisk CLI, or see Section C.53, “TIMEOUT()
TIMEOUT is defined in seconds and may be set in the dialplan like so.
exten => 123,1,Set(TIMEOUT(digit)=3)

Intelligent discernment

In the dialplan above, Asterisk will proceed immediately if 2 is pressed, but only after the timeout has expired if 1 is pressed. Asterisk intelligently determines whether a digit entered can match multiple extensions and behaves accordingly.

Invalid input (the i extension)

An invalid entry (any entry for which no extension in the dialplan matches) can be handled by the i extension. A simple example would look like this:
exten => 30,1,Answer()
exten => 30,2,Background(marryme)
exten => 30,3,Hangup()

exten => 1,1,Playback(thank-you-cooperation)
exten => 1,2,Hangup()

exten => 2,1,Playback(sorry)
exten => 2,2,Hangup()

; Any other input is caught by the i extension.
exten => i,1,Background(sorry)
exten => i,2,Hangup()

Pauses

The easiest way to create pauses for input is to play back empty sound files. A series of silent sound files of between 1 and 9 seconds in length may be found in /var/lib/asterisk/sounds/silence/. If we need to allow five seconds following the prompt (a marriage proposal requires careful consideration, after all), here's how we can accomplish that:
exten => 30,1,Answer()
exten => 30,2,Background(marryme)
exten => 30,3,Background(silence/5)
exten => 30,4,Hangup()

exten => 1,1,Playback(thank-you-cooperation)
exten => 1,2,Hangup()

exten => 2,1,Playback(sorry)
exten => 2,2,Hangup()

exten => i,1,Background(marryme)
exten => i,2,Hangup()


[24] Allison Smith is a Canadian voice professional who is the "Voice of Asterisk". Its growing popularity has given her a considerable cult following :)

[25] Using this IVR for an actual marriage proposal is strongly discouraged.