Skip to content

Asterisk IVR Dialplan Example: BackGround, WaitExten, Read

Asterisk IVR dialplan example in extensions.conf with BackGround, WaitExten, Read and Playback, plus i and t extensions, TIMEOUT settings and a retry limit.

By · Updated · Published

Photo of programming, html, css
Photo by Boskampi · Pixabay Content License · via Pixabay

What does a basic Asterisk IVR dialplan look like?

Photo of laptop, notebook, cellphone
Photo by Pexels · Pixabay Content License · via Pixabay

A menu context answers the call, plays a prompt, waits for a key, and handles wrong or missing input. The example below does that in extensions.conf. It is a starting point, and the later sections add the limits this first version lacks.

In extensions.conf:

INI config
[ivr-main]
exten => s,1,Answer()
 same => n(menu),BackGround(main-menu)
 same => n,WaitExten()

exten => 1,1,Goto(sales,s,1)
exten => 2,1,Goto(support,s,1)
exten => i,1,Playback(invalid-option)
 same => n,Goto(ivr-main,s,menu)
exten => t,1,Playback(no-input)
 same => n,Goto(ivr-main,s,menu)

The first line opens the context. The s extension is where a call starts. Answer() answers the channel before the prompt plays. Its optional delay is in milliseconds, and the minimum is five hundred milliseconds.

The lines that begin with same => share the extension name of the line above. Asterisk added that prefix in the 1.6.2 branch, so you do not repeat the name on every line. The text n(menu) gives that priority a label, so a Goto() can return to it later. Priority n means the previous priority plus one. The first priority of each extension still has to be written out, as the contexts and priorities page explains.

BackGround(main-menu) plays a file named main-menu and listens for digits while it plays. The docs say WaitExten() is usually called right after it, because BackGround() ends when its files finish. WaitExten() then keeps waiting for the caller to enter an extension.

The menu key lines send the call to other contexts. Goto() takes a context, an extension and a priority, so Goto(sales,s,1) starts the sales context at its first priority. The sales and support contexts are placeholders here. You define them elsewhere in the file.

The i extension handles digits that match no extension in the context. The t extension handles a caller who says nothing. Without them, an invalid entry or silence would end the call, because the call ends when there is no i or t to go to.

This first version can loop forever. A caller who keeps pressing wrong keys hears the menu again and again, because i sends the call back with no limit. The section on looping shows how to add a retry counter.

BackGround, WaitExten, Read or Playback: which should you use?

Our side-by-side reference was compiled from the official Asterisk documentation, checked November 14, 2026. We did not run these configurations. Every row comes from a docs page linked in the table, and those pages were generated from Asterisk branch 23. Tutorials usually show one of these applications, so this table puts them side by side.

ApplicationSyntaxKey optionsStatus variableUse it for
BackGround()BackGround(filename1&[filename2[&...]],[options,[langoverride,[context]]])n, m, pBACKGROUNDSTATUSPrompts while waiting for a key
WaitExten()WaitExten([seconds,[options]])m(x), d(x)-Waiting for an extension
Read()Read(variable,filename&[filename2[&...]],[maxdigits,[options,[attempts,[timeout]]]])t, attempts, timeoutREADSTATUSStoring digits in a variable
Playback()Playback(filename&[filename2[&...]],[options])noanswerPLAYBACKSTATUSAnnouncements with no input

The docs list BackGround() and WaitExten() as available since 13.8.0, and Read() since 1.6.1.0.

Choose BackGround() then WaitExten() for a menu where a key jumps to an extension. Choose Read() then a READSTATUS check when you need digits in a variable, such as an account number or PIN. Choose Playback() for an announcement that needs no input.

Read() stores #-terminated digits in a variable instead of jumping to an extension. It sets READSTATUS to OK, ERROR, HANGUP, INTERRUPTED, SKIPPED or TIMEOUT, so a flow should test that value before it trusts the digits.

What is the difference between BackGround() and WaitExten()?

BackGround() plays prompts while it listens for the digits of an extension. WaitExten() waits for the caller to enter an extension. The two work as a pair. BackGround() plays the prompt, and WaitExten() keeps waiting for digits after the files finish.

BackGround() sets BACKGROUNDSTATUS to SUCCESS or FAILED. Its options change how a key press behaves. Option n does not answer the channel before the files play. Option m breaks only when a digit matches a one-digit extension in the destination context. Option p does not allow digits to interrupt playback.

INI config
exten => s,1,BackGround(main-menu,m)
 same => n,WaitExten(5)

In this block, the m option lets a matching single digit break the prompt early. WaitExten(5) then waits a five-second window for an extension. If one of the sound files does not exist, call processing ends, so check every file name before you publish a menu.

WaitExten() takes an optional number of seconds and options. The seconds may be fractional, so WaitExten(1.5) is valid. Option m(x) plays music on hold of class x while the caller waits. Option d(x) plays an indication tone, and dial tone is the default.

If you give WaitExten() no seconds argument, Asterisk uses the built-in response timeout. The next section explains that timeout.

How long does Asterisk wait for a keypress?

The TIMEOUT() function sets three limits, all in seconds, and it has been available since 1.2.0. You change each limit with Set(), which can set a dialplan function value.

TIMEOUT(response) is the time a caller has to start typing after the priorities run out. The default is a ten-second window. If nothing is typed, control goes to the t extension, or the call ends when there is no t.

TIMEOUT(digit) is the longest gap allowed between digits. The default is a five-second gap. After that gap, the entry counts as complete. An invalid entry goes to the i extension, or the call ends when there is no i.

TIMEOUT(absolute) is the maximum total time for a call. A value of 0 disables it. When the limit is reached, the T extension runs.

INI config
exten => s,1,Set(TIMEOUT(response)=10)
 same => n,Set(TIMEOUT(digit)=5)
 same => n,BackGround(main-menu)
 same => n,WaitExten()

The values in this block repeat the defaults, so you can see where to change them. The Set() lines run before BackGround(), so both the prompt and the wait use the new values. Change the numbers to suit your menu, and test the result on a real line before you rely on it.

What happens when a caller presses a wrong key or nothing?

Photo of phone, pushbutton phone, telephone booth
Photo by 652234 · Pixabay Content License · via Pixabay

Asterisk runs special extensions for menu problems. Each one is a name you define in the context, and it runs only when its case happens.

The i extension runs when digits dialed during BackGround() or WaitExten() match no extension in the current context. The t extension runs when the caller waits too long before responding, and the extension has no more priorities. The special extensions page lists both.

The e extension is a catch-all for a missing i, t or T extension. The EXCEPTION() function retrieves the details of the current dialplan exception, so you can see what went wrong.

The h extension runs in the current context when a call hangs up, so it is the place for clean-up. The s extension is the start point. The docs say it is not a catch-all, so do not rely on it to catch bad input.

This block gives a caller a message and a clean end when a menu has no handler for a problem:

INI config
exten => e,1,Playback(error-try-again)
 same => n,Hangup()

Hangup() ends the calling channel. A missing target for Goto() works in a similar way: Asterisk tries i, then h in the current context, and otherwise hangs up.

How do you stop the menu looping forever?

A menu loops when the i and t extensions send the caller back without a limit. A retry counter fixes that. Set() creates the counter, the bracket expression evaluates the arithmetic, and GotoIf() decides whether to stop.

INI config
[ivr-main]
exten => s,1,Answer()
 same => n,Set(RETRIES=0)
 same => n(menu),Set(RETRIES=$[${RETRIES} + 1])
 same => n,GotoIf($[${RETRIES} > 2]?too-many:)
 same => n,BackGround(main-menu,m)
 same => n,WaitExten()
 same => n(too-many),Playback(goodbye)
 same => n,Hangup()

exten => 1,1,Goto(sales,s,1)
exten => 2,1,Goto(support,s,1)
exten => i,1,Playback(invalid-option)
 same => n,Goto(ivr-main,s,menu)
exten => t,1,Playback(no-input)
 same => n,Goto(ivr-main,s,menu)

Set(RETRIES=0) runs once, before the menu label, so the count starts at zero and is not reset on each pass.

Set(RETRIES=$[${RETRIES} + 1]) adds one each time the menu label runs. The text inside the $[ ] bracket pair is evaluated as an expression, as the expressions page describes.

The GotoIf() line jumps to too-many when the count is above the limit. The false label is empty, so when the test fails, no jump is performed and the call moves to the next line, which is the BackGround() line. Change the limit in that test to set how many passes a caller gets.

A caller who stays silent goes to t, which also returns to the menu label, so silence counts as a pass in the same way as a wrong key.

The i and t lines jump to the menu label, not to s. That matters. Jumping to the label skips the Set() that resets the counter, so the count keeps growing. A plain name such as RETRIES stays on this channel. Prefixing a name with _ or __ lets child channels inherit it, which this example does not need.

How do you collect an account number or PIN with Read()?

Read() is the right choice when a caller must enter digits that your dialplan will check. It stores the digits in a variable, and it does not jump to an extension.

INI config
[account-entry]
exten => s,1,Answer()
 same => n(ask),Read(ACCOUNT,enter-account,,,3,10)
 same => n,GotoIf($[${READSTATUS} = OK]?valid:bad)
 same => n(valid),Playback(thank-you)
 same => n,Hangup()
 same => n(bad),Playback(try-again-later)
 same => n,Hangup()

The Read() line stores the digits in ACCOUNT and plays enter-account first. The two empty fields are maxdigits and the options. Leaving maxdigits empty keeps its documented default of 0, which waits for the # key. The attempts argument makes Read() retry when no data is entered. The timeout argument is the number of seconds to wait for a digit response, and it can be fractional.

If you know the length of the number, set maxdigits to that length. The docs accept a maximum of 255.

The GotoIf() line checks READSTATUS. When it is OK, the call goes to valid. Any other value, such as TIMEOUT or HANGUP, goes to bad. Skipping this check means the flow carries on even when the caller entered nothing.

This example checks only READSTATUS. It does not check the account number itself, because that check belongs to your back-end system.

When should you use Playback() rather than BackGround()?

Playback() plays files, and digits do not select an extension. Use it for a message the caller hears once, such as a closed notice. It sets PLAYBACKSTATUS to SUCCESS or FAILED.

Playback() answers the channel if no options are given. The noanswer option plays the file without answering the channel first.

INI config
exten => s,1,Playback(closed-notice,noanswer)
 same => n,Hangup()

What do the advanced examples look like?

Three configurations follow. Each uses only the pieces covered above, and you can add each one as its own set of contexts in extensions.conf.

Two-level menu with a way back. The main menu sends a key to a sub-menu. The sub-menu has its own key that returns the caller to the main menu.

INI config
[ivr-main]
exten => s,1,Answer()
 same => n(menu),BackGround(main-menu,m)
 same => n,WaitExten()

exten => 1,1,Goto(support-menu,s,1)
exten => i,1,Goto(ivr-main,s,menu)
exten => t,1,Goto(ivr-main,s,menu)

[support-menu]
exten => s,1,BackGround(support-menu,m)
 same => n,WaitExten()

exten => 1,1,Goto(billing,s,1)
exten => 9,1,Goto(ivr-main,s,menu)
exten => i,1,Playback(invalid-option)
 same => n,Goto(support-menu,s,1)
exten => t,1,Goto(ivr-main,s,menu)

The sub-menu lives in its own context, support-menu, so its keys do not clash with the main menu. The way-back key uses Goto(ivr-main,s,menu), which jumps to the labeled priority in the main menu. Neither menu has a retry limit in this version. Add the counter from the looping section if you need one.

Music on hold while waiting. This version lets the caller hear the whole prompt, then waits with music.

INI config
exten => s,1,Answer()
 same => n,BackGround(main-menu,p)
 same => n,WaitExten(10,m(default))

The p option stops digits from interrupting the prompt, so the caller hears all of it. In the WaitExten() line, the first argument, ten, is the timeout in seconds, and the m option plays music on hold of class default while the caller waits. Replace default with the music on hold class that your system uses.

PIN entry with retries. This flow asks for a PIN, retries a limited number of times, and then ends the call.

INI config
[pin-entry]
exten => s,1,Answer()
 same => n,Set(PINTRIES=0)
 same => n(ask),Set(PINTRIES=$[${PINTRIES} + 1])
 same => n,Read(PIN,enter-pin,4,,1,10)
 same => n,GotoIf($[${READSTATUS} = OK]?accepted:retry)
 same => n(retry),GotoIf($[${PINTRIES} > 2]?locked:ask)
 same => n(locked),Playback(too-many-attempts)
 same => n,Hangup()
 same => n(accepted),Playback(pin-received)
 same => n,Hangup()

The counter works like the one in the looping section. Read() asks once per pass here, because its attempts argument is 1, so this flow controls every retry itself. The first GotoIf() sends any OK result to accepted and every other result to retry. The second GotoIf() sends the caller back to ask or to locked, depending on the count.

This flow treats every value other than OK in the same way. A real flow may want separate branches for HANGUP and TIMEOUT, which Read() reports. The example stops at READSTATUS and does not compare the PIN with a stored value. That comparison belongs to your system.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

How do I play several prompts in one menu?
List the files in one BackGround() call, joined with ampersands, such as BackGround(welcome&main-menu). Once the files finish, WaitExten() keeps waiting for digits, as the BackGround() documentation describes.
How do I build a sub-menu and send callers back to the main menu?
Give the sub-menu its own context, then send callers back with Goto(ivr-main,s,menu), which jumps to the labeled priority. The two-level example in the advanced section shows the full pattern.

Sources

  1. Answer([delay,[options]]) answers the call; delay is in milliseconds with a minimum of 500 (docs.asterisk.org)
  2. In extensions.conf, the same => prefix avoids repeating the extension name; it was added i (docs.asterisk.org)
  3. BackGround() syntax is BackGround(filename1&[filename2[&...]],[options,[langoverride,[cont (docs.asterisk.org)
  4. WaitExten() syntax is WaitExten([seconds,[options]]); it waits a set number of seconds for (docs.asterisk.org)
  5. Read() syntax is Read(variable,filename&[filename2[&...]],[maxdigits,[options,[attempts,[t (docs.asterisk.org)
  6. Playback() syntax is Playback(filename&[filename2[&...]],[options]); it answers the channe (docs.asterisk.org)
  7. TIMEOUT(response) is the time a caller has to start typing after the priorities run out; t (docs.asterisk.org)
  8. Set(name=value) sets a channel variable or a dialplan function value, such as TIMEOUT(resp (docs.asterisk.org)
  9. The i extension runs when digits dialed during BackGround() or WaitExten() match no extens (docs.asterisk.org)
  10. EXCEPTION(field) retrieves the details of the current dialplan exception. (docs.asterisk.org)
  11. Hangup([causecode]) hangs up the calling channel. (docs.asterisk.org)
  12. Goto([[context,]extension,]priority) jumps to a priority, extension or context; if the tar (docs.asterisk.org)
  13. GotoIf(condition?[labeliftrue:[labeliffalse]]) jumps conditionally; if the chosen label is (docs.asterisk.org)
  14. Text inside a bracket pair prefixed by $, such as $[1 + 2], is evaluated as an expression. (docs.asterisk.org)