IVR loop control

As of the previous lesson, if a user started the IRV menu and timed out, they would then start the main menu over again. And then if they timeout out again, the process would start over again, and again, and again. We would then have an endless loop that would never end until the user ended the call. This would unnecessarily tie up a phone line.

To prevent the endless loop, we can control how many times a the timeout can cycle through the main menu. To do this we will create a variable that we can use as a counter to determine how many times the user has cycled through. We will use the Set() application to do this and set a variable named LOOP equal to zero.

We do not want to reset the counter variable every time the user is sent to extension “s” so we will add a name to the line of code that plays the main_menu.gsm audio file. This way we can direct the timeout to this line. I will name it menu.

Modify the “s” extension to include the Set() application and line name as seen below:

exten => s,1,NoOp(Main IVR)
same => n,Set(LOOP=0)
same => n,Answer
same => n(menu), Background(main_menu)
same => n,WaitExten(5)

Now we need to modify the timeout extension to increment the LOOP variable. We will use this to count how many times the menu has been played as a result of timeout. To increment the LOOP variable we will set it equal to the original value plus one. We can then check to see how many times the loop has played and based on our condition we can send it back to extension “s” and the line labeled “menu” or hangup if the condition is not met.

Below is the code to allow the LOOP to play twice. The third time it plays it will no longer be less than 3 and will just continue to the next line of code.

exten => t,1,NoOp(Timeout)
same => n,Set(LOOP=$[${LOOP}+1])
same => n,GotoIf($[${LOOP}<3]?s,menu)
same => n,Hangup