IVR is an Interactive Voice Response which takes input from a caller, perform an action based on that input, and return a result to the caller. Traditionally, IVR systems have been very complex and expensive to implement. The aim of this tutorial is to showcase simple way to get IVR in Asterisk system.
This is a continuation of Tutorials on Asterisk and Software based PBX. You should have a working Asterisk system before trying to setup IVR in Asterisk. If you’re new and would like to install Asterisk, you can use our tutorial based on CentOS below:

Working of IVR:
When an incoming call get to IVR context, the first prompt tells the caller what IVR expects from the caller, a method of receiving input from the caller, logic to verify that the caller’s response is valid input, logic to determine what the next step of the IVR should be, and finally, a storage mechanism for the responses, if applicable.

Configure IVR in Asterisk

The IVR in Asterisk design shown here is for a small campus with the following offices as sample:

  1. Reception office
  2. Finance office
  3. DVC office
  4. Security office
  5. ICT office
  6. School Office
  7. Accommodation Office

You can modify all configurations to suit your use case. SIP users added to sip.conf file are as below.

[101]
username=customercare
secret=pass
host=dynamic
type=friend
disallow= all
allow=ulaw
allow=h263
allow=gsm
allow=alaw
context=moi-ivr
callerid=customercare
videosupport=yes

[102]
username=finance
secret=pass
host=dynamic
type=friend
disallow= all
allow=ulaw
allow=g722
allow=gsm
allow=alaw
context=moi-ivr
callerid=finance

[103]
username=dvc
secret=pass
host=dynamic
type=friend
disallow= all
allow=ulaw
allow=g722
allow=gsm
allow=alaw
context=moi-ivr
callerid=dvc

[104]
username=security
secret=pass
host=dynamic
type=friend
disallow= all
allow=ulaw
allow=g722
allow=gsm
allow=alaw
context=moi-ivr
callerid=security

[105]
username=ICT
secret=pass
type=friend
disallow= all
allow=ulaw
allow=g722
allow=gsm
allow=alaw
context=moi-ivr
callerid=ICT

[106]
username=schooloffice
secret=pass
type=friend
disallow= all
allow=ulaw
allow=h263
allow=gsm
allow=alaw
context=moi-ivr
callerid=schooloffice

[107]
username=accomodation
secret=pass
type=friend
disallow= all
allow=ulaw
allow=h263
allow=gsm
allow=alaw
context=moi-ivr
callerid=accomodation

The next step is to have the context ready to rock. The context is given below:

[moi-incoming]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered IVR menu)
same => n,Answer()
same => n,Set(TIMEOUT(digit)=2) ; this sets the inter-digit timer
same => n,Wait(2) ; wait two second to establish audio
; During job hours, jump to moi-ivr context.
; During closed hours, jumpt to offices-closed.
same => n,GotoIfTime(9:00-16:00,mon-fri,*,*?moi-ivr,s,1:offices-closed)
same => n,GotoIfTime(9:00-12:00,sat,*,*?moi-ivr,s,1:offices-closed)
[offices-closed]
exten =>s,1,Answer()
same => n,wait(2)
same => Background(basic-pbx-ivr-main)
same => n,Hangup()
[moi-ivr]
exten =>s,1,Answer()
same => n,wait(2)
same => n,Set(TIMEOUT(digit)=10) ; ; this sets the inter-digit timer
same => n,Set(TIMEOUT(response)=10)
same => n,Background(basic-pbx-ivr-main)
same => n,WaitExten()
same => n,Background(silence/5)
; 1 to talk to reception ;101
; 2 to talk to finance office ; 102
; 3 to talk to dvc office ;103
; 4 to talk to security office ; 104
; 5 to talk to ICT office ;105
; 6 to talk to schools office ; 106
; 7 to talk to accommodation office ;107
exten => 1,1,Goto(ivr-customercare,s,1)
exten => 2,1,Goto(ivr-finance,s,1)
exten => 3,1,Goto(ivr-dvc,s,1)
exten => 4,1,Goto(ivr-security,s,1)
exten => 5,1,Goto(ivr-ict,s,1)
exten => 6,1,Goto(ivr-schools,s,1)
exten => 7,1,Goto(ivr-accomodation,s,1)
;; 'i' extension that is used for invalid extensions, i e, non-existing extensions dialled by clients.
exten => i,1,Playback(option-is-invalid)
same => n,Goto(moi-ivr)
;; Timeout extension
exten => t,1,Playback(still-there)
exten => t,Goto(moi-ivr,s,5)
;; define extensions for IVR menu
[ivr-reception]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customer care support center)
same => n,Answer()
same => n,Dial(SIP/101,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()
[ivr-finance]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customercare support center)
same => n,Answer()
same => n,Dial(SIP/102,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()
[ivr-dvc]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customercare support center)
same => n,Answer()
same => n,Dial(SIP/103,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()
[ivr-security]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customercare support center)
same => n,Answer()
same => n,Dial(SIP/104,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()
[ivr-ict]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customercare support center)
same => n,Answer()
same => n,Dial(SIP/105,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()
[ivr-schools]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customercare support center)
same => n,Answer()
same => n,Dial(SIP/106,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()
[ivr-accommodation]
exten =>s,1,Verbose(1, Caller ${CALLERID(all)} has entered Customercare support center)
same => n,Answer()
same => n,Dial(SIP/107,10,r)
same => n,Hangup()
same => n,Playback(unavail)
same => n,Hangup()

To make calls going to extension numbers 101-107 get to IVR in Asterisk context, we’ve to include IVR context in the main context for incoming calls. The file edited is
/etc/asterisk/extensions.conf. Then adding the following line of code,

include => moi-incoming

Now whenever a person dial 101 and make a call, its processing will be done on IVR context. Key point to note is that all extension numbers used on IVR must be registered for a call to be routed.
If you have a separate call routing logic on extensions.conf for PSTN incoming calls, just include IVR context in it.

If you Love what we do, support us by downloading this tutorial as pdf from the link below:

Related Articles are:

How to install Asterisk PBX 13.x on CentOS 7 step by step guide

SHARE
Previous articleTop things to do after Fresh Installation of Archlinux
Next articleBackbox Linux 4.6 Released Download
I'm a fan of Linux and open source enthusiast with bachelor's degree in Electrical and Communications Engineering.The Swiss Army Knife syndrome makes me an expert in the fields of Virtualization, Cloud Computing,VoIP,Linux Containers and Server Hardening. Currently i have the following certifications: 1. LPI 2. SUSE Certified Linux Administrator ( CLA) 3. SUSE Certified Linux Professional ( CLP) 4. CCNA And working on RHCSA, RHCE, RHCVA, and Openstack Cloud Certification. I have a strong desire to learn, develop, and share knowledge with others.