Our first dialplan

Now that we know how to register phones to Asterisk and create a dialplan, we can create our first telephony system. If you’ve followed along, you should have an environment with two softphones set up and registered to Asterisk. We can confirm that the peers are active by using the sip show peers command:

We will now write a simple dialplan that will enable our phones to communicate with each other. Create the /etc/asterisk/extensions.conf file and populate it with the following lines:

[local]
exten => 101,1,Dial(SIP/alice)
 same => n, Hangup()

exten => 105,1,Dial(SIP/bob)
 same => n, Hangup()

Here is an explanation of the code above:

  • if the extension 101 is called from a telephone in the [local] context, Asterisk will execute the Dial application and call the peer alice in the SIP channel. When the conversation is over, the Hangup application will hang up the call.
  • if the extension 105 is called from a telephone in the [local] context, Asterisk will execute the Dial application and dial the peer bob in the SIP channel. When the conversation is over, the Hangup application will hang up the call

To apply the changes, we need to reload the dialplan. This is done using the dialplan reload command in the Asterisk CLI:

geek-university*CLI> dialplan reload
Dialplan reloaded.
== Parsing '/etc/asterisk/extensions.conf': Found
-- Registered extension context 'local'; registrar: pbx_config
-- Added extension '101' priority 1 to local
-- Added extension '101' priority 2 to local
-- Added extension '105' priority 1 to local
-- Added extension '105' priority 2 to local
-- Registered extension context '__func_periodic_hook_context__'; registrar: func_periodic_hook
-- merging incls/swits/igpats from old(__func_periodic_hook_context__) to new(__func_periodic_hook_context__) context, registrar = pbx_config
-- Added extension 'hook' priority 1 (CID match '') to __func_periodic_hook_context__
-- Added extension 'hook' priority 2 (CID match '') to __func_periodic_hook_context__
-- Added extension 'hook' priority 3 (CID match '') to __func_periodic_hook_context__
-- Added extension 'hook' priority 4 (CID match '') to __func_periodic_hook_context__
-- Added extension 'hook' priority 5 (CID match '') to __func_periodic_hook_context__
-- Added extension 'hook' priority 6 (CID match '') to __func_periodic_hook_context__
-- Added extension 'beep' priority 1 (CID match '') to __func_periodic_hook_context__
-- Added extension 'beep' priority 2 (CID match '') to __func_periodic_hook_context__
-- Time to scan old dialplan and merge leftovers back into the new: 0.000029 sec
-- Time to restore hints and swap in new dialplan: 0.000002 sec
-- Time to delete the old dialplan: 0.000005 sec
-- Total time merge_contexts_delete: 0.000036 sec
geek-university*CLI>

We can now make our first call. Now, when I will start Ekiga and dial the extension 101, my other peer (alice) should receive the call. This is the output displayed in the Asterisk console:

geek-university*CLI>
== Using SIP RTP CoS mark 5
-- Executing [101@local:1] Dial("SIP/bob-00000008", "SIP/alice") in new stack
== Using SIP RTP CoS mark 5
-- Called SIP/alice
-- SIP/alice-00000009 is ringing
-- SIP/alice-00000009 answered SIP/bob-00000008

You can probably understand what is going on in the output above – the peer bob is calling the peer alice. You can also see that alice has answered the call.

If you’ve followed along, congratulations! You’ve just made your first call in Asterisk!
Geek University 2022