Basic IVR with Asterisk
Tags: asterisk, ivr, beginners, voip
Published: 2021-05-28 03:14:19

## Weekly sharing

Hi everyone, I am Ekim, a fresh Bootcamp graduate and an IT helper (I don't dare to call myself a programmer yet). Every Friday, I will share some of the work that I've done over the last week in a bid to get feedbacks from you guys and record my journey to become a programmer. 

###Previously
<a href="https://dev.to/ekim34351855/asterisk-manager-with-javascript-4idc">Asterisk Manager with JavaScript</a> 
### Introduction 

Today, I would like to share how we could make a simple interactive voice response (IVR). I know you might have had some horrible experiences with that, but it would be a weird but fun experience when you hear your voice on the phone talking to you. 



### What is Interactive Voice Response (IVR) menu? 

- An IVR menu is a list of options that are presented to an **inbound caller** which prompts them to **make a selection**, thus qualifying the call and **directing** it to the most **appropriate contact**.



### Recording our own audio track through Asterisk for IVR 

- If you have read my previous posts, you would know that the codecs that telephony supports are limited. 

- Even if you have recorded a very good quality audio track, you still need to convert it into the format that our phones support. 

- To save time, I am not going to go through the `ffmpeg` part again. Instead, I am going to record all the IVR audio track through the `Record` dialplan application. 

- In pjsip.conf

  ```conf
    [transport-udp-nat]
    type = transport
    protocol = udp
    bind = 0.0.0.0
    
    [calling](!)				 
    type=endpoint				 
    context=interaction			 
    allow = !all, ulaw, alaw	 
    direct_media=no				 
    trust_id_outbound=yes
    rtp_symmetric=yes
    force_rport=yes
    rewrite_contact=yes
    device_state_busy_at=1
    dtmf_mode=rfc4733
    
    [auth-userpass](!)		 
    type = auth				 
    auth_type = userpass	 
    [aor-single-reg](!)		
    type = aor
    max_contacts = 1		 
    
    [7000](calling)			 
    auth=7000				 
    aors=7000				 
    callerid = 7000 <7000>	 
    
    [7000](auth-userpass)	 
    password = 7000
    username = 7000
    
    [7000](aor-single-reg)
    mailboxes = 7000@main
    
    [7100](calling)
    auth=7100
    aors=7100
    callerid = 7100 <7100>
    
    [7100](auth-userpass)
    password = 7100
    username = 7100
    
    [7100](aor-single-reg)
    mailboxes = 7100@main
```

- In extensions.conf

```conf
[interaction]
exten = *555,1,NoOp(recording)
same = n,Answer
same = n,Record(record/greeting.wav)
same = n,Playback(vm-received)
same = n,Playback(en/digits/1)
same = n,Record(record/press1.wav)
same = n,Playback(vm-received)
same = n,Playback(en/digits/2)
same = n,Record(record/press2.wav)
same = n,Playback(vm-received)
same = n,Playback(en/digits/3)
same = n,Playback(vm-last)
same = n,Record(record/wrong-key.wav)
same = n,Playback(vm-received)
same = n,Playback(en/digits/4)
same = n,Hangup

;exten = _7X00,1,NoOp(Call for ${EXTEN})	
;same = n,Dial(PJSIP/${EXTEN})		
;same = n,Hangup	

exten = 8888,1,NoOp(Call IVR)
same = n,Goto(ivr,s,1)

[ivr]
exten = s,1,NoOp(You are in IVR now)
same = n,Answer						; here is the start of the SIP protocol
same = n(play),Playback(record/greeting)
same = n,WaitExten(10)				; wait 10 seconds for the caller to press key

exten = 1,1,NoOp(Pressed 1)
same = n,Playback(record/press1)
same = n,Goto(s,play)				; go back to 'n(play),Playback(record/greeting)'

exten = 2,1,NoOp(Pressed 2)
same = n,Playback(record/press2)
same = n,Goto(s,play)

exten = i,1,NoOp(unknown key)		; handle invalid keys --> when keys other than 1 and 2 are pressed
same = n,Playback(record/wrong-key)
same = n,Goto(s,play)

exten = t,1,NoOp(Waited too long for exten)		; handle WaitExten timeout
same = n,Goto(s,play)
```




### Implementation

- First of all, let's record all the necessary navigation menu tracks. 
- I have set in the dialplan that when we call ```*555```, we will start recording each audio file one by one.
- When you hear the first 'beep' sound, it is for the recording of the greeting. 
  - For example, "thanks for calling, English, press 1; Deutsch, press 2". 
- When you finish each recording, remember to press ```#``` to save it. 
- If successful, you will hear the number of recording received. 
  - If you finished one recording, you will hear "received 1"
- When you hear "last", that means you are going to record the last audio. 
- Here is the sequence of the recording sections
   	1. greeting
    	2. the navigation after pressing 1
    	3. the navigation after pressing 2
    	4. the navigation when wrong key is pressed

- To go into the IVR, dial `8888` and it will start the IVR menu. 

  

### Conclusion

Now, you have just made yourself an IVR menu. If you follow tight, you would realize that the whole IVR is a loop. You could feel free to add the `Hangup` dialplan application wherever you like. One thing I would like to stress here is that this IVR structure is a very basic one as things are repetitive and could be refactorized in another way. A more advanced one might be introduced some time later (I hope ... ) with the use of database and AGI (Asterisk Gateway Interface). That's all for today. See you next time.