Make calls through Asterisk CLI
Tags: asterisk, voip, beginners, codec
Published: 2021-05-07 03:18:29

## 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/record-and-hear-my-voice-through-asterisk-11om">Record and Hear your voice through Asterisk</a>


### Introduction

As promised, I will go through a more user-friendly process of recording a voice. Also, I would like to show you how to originate a call (make a call) from Asterisk. 



### Voice Navigation

- If you are careful enough, you would have noticed that the `Record` dialplan application includes a "beep" sound when you dial 41XXXX. 
- So now, let's make our voice navigation based on that.
- Record a track that signals people to start recording their voices after hearing a "beep" sound and press `#` when they finish recording. 



### Phone restriction

- If you dial 41XXXX to record your navigation audio, just like the previous sharing, you wouldn't need to worry about the audio restrictions. However, if you record your navigation audio through your smartphone application or other devices, the recording you got is highly likely incompatible with the telephony codecs. 



- Often, for **hardphones**, there are restrictions for the audio file.
  - Codec 	&rarr; 	.gsm (convention and saves space) | .wav (better quality but occupying more space)
  - Channels 	&rarr; 	mono
  - Sample rate 	&rarr; 	8000hz (at max)
  - Bits per sample 	&rarr; 	16bits



- So, we need to convert the file into what we need using `ffmpeg`. 

```bash
ffmpeg -i <file> -ac 1 -ar 8000 -c:a pcm_s16le "<new file>.wav"
```



### Audio track location

- After that, let's move the converted file to the location where Asterisk stores sounds. 

```bash 
cd /var/lib/asterisk/sounds/
```

- I would like to create a folder for better organization, so I create a folder called "playback"

```bash
mkdir playback
```



### Dialplan modifications

- Going back to our extensions.conf, 

```conf
[recordAndPlayback]
exten = _41XXXX,1,Answer
; Playback is the application; playback is the folder in /var/lib/asterisk/sounds/ ; xxxxx is the converted audio name where .wav is excluded.
same = n,Playback(playback/xxxxx)	
; record your voice and save it in the name of XXXX, the any 4 digits you dialed and in .gsm format
same = n,Record(record/${EXTEN:2}.gsm)		
same = n,Wait(1)
same = n,Hangup()

exten = _42XXXX,1,Answer
same = n,Wait(1)
; play the audio file you recorded from /var/lib/asterisk/sounds/record
same = n,Playback(record/${EXTEN:2})		
same = n,Wait(1)
same = n,Hangup()
```



- As always, after doing changes in the dialplan, reload it in the CLI. 

```bash
sudo asterisk -rvvvvv
dialplan reload
```



- Now, it is more user-friendly. 



### Calling through CLI 

- Before we originate a call through CLI, we need to do some settings in pjsip.conf, extensions.conf and modules.conf.

- In pjsip.conf,

```conf
[transport-udp-nat]
type = transport
protocol = udp
bind = 0.0.0.0


[endpoint-useragent](!)
type=endpoint
context=calling			;Context is now "calling"
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

[1112](endpoint-useragent)		;created 1112 endpoint
auth=1112
aors=1112
callerid = 1112 <1112>

[1112](auth-userpass)
password = 1112
username = 1112

[1113](endpoint-useragent)		;created 1113 endpoint
auth=1113
aors=1113
callerid = 1113 <1113>

[1113](auth-userpass)
password = 1113
username = 1113
```



- In extensions.conf

``` js
[calling]
exten = _111X,1,Dial(PJSIP/${EXTEN})
 same = n,Hangup()
```



- In modules.conf

```conf
;Application
load = app_bridgewait.so
load = app_dial.so
load = app_playback.so
load = app_stack.so
load = app_verbose.so
load = app_voicemail.so
load = app_directory.so
load = app_confbridge.so
load = app_queue.so
load = app_record.so
load = app_originate.so  	; added 
load = res_clioriginate.so	; added 
```



- Also, don't forget to set up those endpoints on your two different softphones. For me, I use MicroSIP and Linphone. 



- And remember, restart after adding new modules in modules.conf, and reload the dialplan and pjsip.

```bash
sudo service asterisk restart
sudo asterisk -rvvvvv
dialplan reload		; not necessary after restart
core reload		; not necessary after restart
channel originate LOCAL/1112@calling application Dial(PJSIP/1113)		# this is how you originate a call via CLI
```
- TLDR: This is how you originate a call from CLI`channel originate LOCAL/1112@calling application Dial(PJSIP/1113)`.


#### Conclusion

- I hope you know more about the codec for telephony system and know how to make calls from CLI. That's it for today. If you have problems regarding `ffmpeg` installation, which I omitted in this sharing, please comment below and see if I could help. In the meantime, stay healthy and stay tuned for more content !!!