How to Create Asterisk Dialplan Context

To create Asterisk Dialplan Context

Introduction:

Asterisk Dialplans are responsible for routing calls, so they are often called the system's heart. The Dialplan is written using a scripting language, so it is incredibly powerful. Asterisk is far more than just a phone system capable of accepting and connecting calls.

Installation Steps:

Step 1: Check the OS version by using the below command

root@linuxhelp1:~# lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:    22.04
Codename:    jammy

Step 2 : Check the status of the Asterisk by using the below command

root@linuxhelp1:~# systemctl status asterisk
● asterisk.service - LSB: Asterisk PBX
     Loaded: loaded (/etc/init.d/asterisk; generated)
     Active: active (running) since Thu 2023-07-20 10:32:35 IST; 9h ago
       Docs: man:systemd-sysv-generator(8)
      Tasks: 74 (limit: 4537)
     Memory: 139.0M
        CPU: 5min 27.132s
     CGroup: /system.slice/asterisk.service
             └─17969 /usr/sbin/asterisk

Jul 20 10:32:35 linuxhelp1 systemd[1]: Starting LSB: Asterisk PBX...
Jul 20 10:32:35 linuxhelp1 asterisk[17956]:  * Starting Asterisk PBX: asterisk
Jul 20 10:32:35 linuxhelp1 asterisk[17956]:    ...done.
Jul 20 10:32:35 linuxhelp1 systemd[1]: Started LSB: Asterisk PBX.
Jul 20 10:32:36 linuxhelp1 asterisk[17969]: radcli: rc_read_config: rc_read_config: can't open /etc/radiusc>
Jul 20 10:32:36 linuxhelp1 asterisk[17969]: radcli: rc_read_config: rc_read_config: can't open /etc/radiusc>

Step 3 : Login to the Asterisk CLI mode by using the below command

root@linuxhelp1:~# asterisk -rvvvvvvvvvvvvvvvvvvvvvvvvv
Asterisk 20.3.1, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.3.1 currently running on linuxhelp1 (pid = 17969)

Step 4 : Show the endpoints by using the below command

linuxhelp1*CLI> pjsip show endpoints

 Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>
    I/OAuth:  <AuthId/UserName...........................................................>
        Aor:  <Aor............................................>  <MaxContact>
      Contact:  <Aor/ContactUri..........................> <Hash....> <Status> <RTT(ms)..>
  Transport:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress..................>
   Identify:  <Identify/Endpoint.........................................................>
        Match:  <criteria.........................>
    Channel:  <ChannelId......................................>  <State.....>  <Time.....>
        Exten: <DialedExten...........>  CLCID: <ConnectedLineCID.......>
==========================================================================================

 Endpoint:  167/167                                              Not in use    0 of inf
     InAuth:  167/167
        Aor:  167                                                1
      Contact:  167/sip:167@192.168.6.101:51508;transport= c4d06cbc16 Avail         1.257
  Transport:  tcp_transport             tcp      3     96  0.0.0.0:5060

 Endpoint:  168/168                                              Not in use    0 of inf
     InAuth:  168/168
        Aor:  168                                                1
      Contact:  168/sip:168@192.168.6.101:51507;transport= 04e351bc0d Avail         0.881
  Transport:  tcp_transport             tcp      3     96  0.0.0.0:5060

 Endpoint:  169/169                                              Not in use    0 of inf
     InAuth:  169/169
        Aor:  169                                                1
      Contact:  169/sip:169@192.168.6.101:51796;transport= bf4652f8f3 Avail         0.974
  Transport:  tcp_transport             tcp      3     96  0.0.0.0:5060


Objects found: 3

Step 5 : Exit from the Asterisk CLI mode by using the below command

Disconnected from Asterisk server
Asterisk cleanly ending (0).
Executing last minute cleanups

Step 6: Edit the extensions.conf file to modify the dialplan

root@linuxhelp1:~# vim /etc/asterisk/extensions.conf

Step 7: Again login to the Asterisk CLI mode

root@linuxhelp1:~# asterisk -rvvvvvvvvvvvvvvvvvvv
Asterisk 20.3.1, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.3.1 currently running on linuxhelp1 (pid = 17969)

Step 8 : Reload the Dialplan by using the below command

linuxhelp1*CLI> dialplan reload
Dialplan reloaded.
    -- Including switch 'Lua/' in context 'public'
    -- Including switch 'Lua/' in context 'demo'
    -- Including switch 'Lua/' in context 'local'
    -- Including switch 'Lua/' in context 'default'
    -- Including switch 'DUNDi/e164' in context 'ael-dundi-e164-switch'
    -- Time to scan old dialplan and merge leftovers back into the new: 0.001776 sec
    -- Time to restore hints and swap in new dialplan: 0.000063 sec
    -- Time to delete the old dialplan: 0.000103 sec
    -- Total time merge_contexts_delete: 0.001942 sec
    -- pbx_config successfully loaded 27 contexts (enable debug for details).


    -- Executing [167@internal:1] NoOp("PJSIP/169-00000057", "Call started for 167") in new stack
    -- Executing [167@internal:2] Dial("PJSIP/169-00000057", "PJSIP/167,10") in new stack
    -- Called PJSIP/167
    -- PJSIP/167-00000058 is ringing
    -- Nobody picked up in 10000 ms
    -- Executing [167@internal:3] Playback("PJSIP/169-00000057", "") in new stack
[Jul 20 20:24:31] WARNING[27422][C-00000026]: app_playback.c:464 playback_exec: Playback requires an argument (filename)
  == Spawn extension (internal, 167, 3) exited non-zero on 'PJSIP/169-00000057'

Step 9: Exit from the Asterisk CLI mode by using the below command

Disconnected from Asterisk server
Asterisk cleanly ending (0).
Executing last minute cleanups

Step 10: Move to the directory to find the sounds for playback by using the below command

root@linuxhelp1:~# cd /var/lib/asterisk/sounds/en
root@linuxhelp1:/var/lib/asterisk/sounds/en#
root@linuxhelp1:/var/lib/asterisk/sounds/en#
root@linuxhelp1:/var/lib/asterisk/sounds/en# ls
1-for-am-2-for-pm.alaw                       north.siren14
1-for-am-2-for-pm.g722                       north.siren7
1-for-am-2-for-pm.g729                       north.sln16
1-for-am-2-for-pm.gsm                        north.ulaw
1-for-am-2-for-pm.siren14                    north.wav
1-for-am-2-for-pm.siren7                     not-auth-pstn.alaw
1-for-am-2-for-pm.sln16                      not-auth-pstn.g722
1-for-am-2-for-pm.ulaw                       not-auth-pstn.g729
1-for-am-2-for-pm.wav                        not-auth-pstn.gsm
1-yes-2-no.alaw                              not-auth-pstn.siren14
1-yes-2-no.g722                              not-auth-pstn.siren7
1-yes-2-no.g729                              not-auth-pstn.sln16
1-yes-2-no.gsm                               not-auth-pstn.ulaw
1-yes-2-no.siren14                           not-auth-pstn.wav
1-yes-2-no.siren7                            not-enough-credit.alaw
1-yes-2-no.sln16                             not-enough-credit.g722
1-yes-2-no.ulaw                              not-enough-credit.g729
1-yes-2-no.wav                               not-enough-credit.gsm
abandon-all-hope.alaw                        not-enough-credit.siren14
abandon-all-hope.g722                        not-enough-credit.siren7
abandon-all-hope.g729                        not-enough-credit.sln16
abandon-all-hope.gsm                         not-enough-credit.ulaw
abandon-all-hope.siren14                     not-enough-credit.wav
abandon-all-hope.siren7                      nothing-recorded.alaw
abandon-all-hope.sln16                       nothing-recorded.g722

Step 11: Add any of the above sound to the extensions.conf file to add the playback sound to the channel by using the below command

root@linuxhelp1:/var/lib/asterisk/sounds/en# vim /etc/asterisk/extensions.conf

Step 12 : Login to the Asterisk CLI mode and reload the Dialplan by using the below command

root@linuxhelp1:/var/lib/asterisk/sounds/en# asterisk -rvvvvvvvvvvvvvvvvvvv
Asterisk 20.3.1, Copyright (C) 1999 - 2022, Sangoma Technologies Corporation and others.
Created by Mark Spencer <markster@digium.com>
Asterisk comes with ABSOLUTELY NO WARRANTY; type 'core show warranty' for details.
This is free software, with components licensed under the GNU General Public
License version 2 and other licenses; you are welcome to redistribute it under
certain conditions. Type 'core show license' for details.
=========================================================================
Connected to Asterisk 20.3.1 currently running on linuxhelp1 (pid = 17969)

linuxhelp1*CLI> dialplan reload
Dialplan reloaded.
    -- Including switch 'DUNDi/e164' in context 'ael-dundi-e164-switch'
    -- Including switch 'Lua/' in context 'default'
    -- Including switch 'Lua/' in context 'local'
    -- Including switch 'Lua/' in context 'demo'
    -- Including switch 'Lua/' in context 'public'
    -- Time to scan old dialplan and merge leftovers back into the new: 0.005825 sec
    -- Time to restore hints and swap in new dialplan: 0.000022 sec
    -- Time to delete the old dialplan: 0.000652 sec
    -- Total time merge_contexts_delete: 0.006499 sec
    -- pbx_config successfully loaded 27 contexts (enable debug for details).

Messages while the call from one extension to another

    -- Executing [167@internal:1] NoOp("PJSIP/169-00000059", "Call started for 167") in new stack
    -- Executing [167@internal:2] Dial("PJSIP/169-00000059", "PJSIP/167,10") in new stack
    -- Called PJSIP/167
    -- PJSIP/167-0000005a is ringing
    -- Nobody picked up in 10000 ms
    -- Executing [167@internal:3] Playback("PJSIP/169-00000059", "you-must-first-dial") in new stack
       > 0x7fb13c069630 -- Strict RTP learning after remote address set to: 192.168.6.101:4068
    -- <PJSIP/169-00000059> Playing 'you-must-first-dial.ulaw' (language 'en')
       > 0x7fb13c069630 -- Strict RTP switching to RTP target address 192.168.6.101:4068 as source
    -- Executing [167@internal:4] Hangup("PJSIP/169-00000059", "") in new stack
  == Spawn extension (internal, 167, 4) exited non-zero on 'PJSIP/169-00000059'

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to create Asterisk Dialplan context. Your feedback is much welcome.

FAQ
Q
What is the application to call from one end point to another?
A
Dial application
Q
What is the application to hangup the call in asterisk?
A
Hangup application
Q
How to load dialplan in Asterisk?
A
If you change the dialplan, you can use the Asterisk CLI command "dialplan reload" to load the new dialplan without disrupting service in your PBX.
Q
What is the Asterisk dialplan for internal calls?
A
The Asterisk dialplan is responsible for routing calls, so it is often referred to as the heart of an Asterisk system.
Q
Where is Asterisk dialplan located?
A
/etc/asterisk/extensions.conf