How to use GoTo and GotoIf Application on Asterisk
To Use GoTo And GotoIf Application On Asterisk
GoTo - this application allows you to jump to another extension, another context, or another priority. The GotoIf( ) Application - An empty string and the number 0 evaluate as false. Anything else evaluates as true. GotoIf is conditional goto command. Go to label1 if the condition is true or to the next step (or label2 if defined) if the condition is false.
Procedure
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-27 07:25:18 IST; 39min ago
Docs: man:systemd-sysv-generator(8)
Process: 1404 ExecStart=/etc/init.d/asterisk start (code=exited, status=0/SUCCESS)
Tasks: 74 (limit: 4537)
Memory: 134.4M
CPU: 28.749s
CGroup: /system.slice/asterisk.service
└─1427 /usr/sbin/asterisk
Jul 27 07:25:18 linuxhelp1 systemd[1]: Starting LSB: Asterisk PBX...
Jul 27 07:25:18 linuxhelp1 asterisk[1404]: * Starting Asterisk PBX: asterisk
Jul 27 07:25:18 linuxhelp1 asterisk[1404]: ...done.
Jul 27 07:25:18 linuxhelp1 systemd[1]: Started LSB: Asterisk PBX.
Jul 27 07:25:25 linuxhelp1 asterisk[1427]: radcli: rc_read_config: rc_read_config: can't open /etc/radiusc>
Jul 27 07:25:25 linuxhelp1 asterisk[1427]: radcli: rc_read_config: rc_read_config: can't open /etc/radiusc>
Step 3: Move to the Asterisk directory by using the below command
root@linuxhelp1:~# cd /etc/asterisk/
Step 4: Edit the extensions.conf file by using the below command
root@linuxhelp1:/etc/asterisk# vim extensions.conf
Add the following lines to the extensions.conf
[internal]
exten => _XXX,1,NoOp(Call started for ${EXTEN})
exten => _XXX,2,Goto(5)
exten => _XXX,3,Hangup()
exten => _XXX,4,Playback(hello)
exten => _XXX,5,Dial(PJSIP/167)
Step 5: Login to the Asterisk CLI mode by using the below command
root@linuxhelp1:/etc/asterisk# 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 = 1427)
Step 6: Reload the dialplan by using the below command
linuxhelp1*CLI> dialplan reload
Dialplan reloaded.
-- Including switch 'DUNDi/e164' in context 'ael-dundi-e164-switch'
-- Including switch 'Lua/' in context 'local'
-- Including switch 'Lua/' in context 'demo'
-- Including switch 'Lua/' in context 'default'
-- Including switch 'Lua/' in context 'public'
-- Time to scan old dialplan and merge leftovers back into the new: 0.007184 sec
-- Time to restore hints and swap in new dialplan: 0.000028 sec
-- Time to delete the old dialplan: 0.000499 sec
-- Total time merge_contexts_delete: 0.007711 sec
-- pbx_config successfully loaded 27 contexts (enable debug for details).
Output of the GoTo Application
-- Executing [168@internal:1] NoOp("PJSIP/167-00000012", "Call started for
168") in new stack
-- Executing [168@internal:2] Goto("PJSIP/167-00000012", "5") in new stack
-- Goto (internal,168,5)
-- Executing [168@internal:5] Dial("PJSIP/167-00000012", "PJSIP/167") in new stack
-- Called PJSIP/167
-- PJSIP/167-00000013 is ringing
== Spawn extension (internal, 168, 5) exited non-zero on 'PJSIP/167-00000012'
linuxhelp1*CLI>
Disconnected from Asterisk server
Asterisk cleanly ending (0).
Executing last minute cleanups
Step 7: Edit the extionsions.conf file to use Goto Application with alternative priority by using the below command.
root@linuxhelp1:/etc/asterisk# vim extensions.conf
Add the following lines
[internal]
exten => _XXX,1,NoOp(Call started for ${EXTEN})
exten => _XXX,2,Goto(4)
exten => _XXX,3,Hangup()
exten => _XXX,4,Playback(hello)
exten => _XXX,5,Dial(PJSIP/167)
Step 8: Login to the Asterisk CLI by using the below command.
root@linuxhelp1:/etc/asterisk# 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 = 1427)
Step 9: 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 'default'
-- Including switch 'Lua/' in context 'demo'
-- Including switch 'Lua/' in context 'local'
-- Including switch 'DUNDi/e164' in context 'ael-dundi-e164-switch'
-- Time to scan old dialplan and merge leftovers back into the new: 0.000330 sec
-- Time to restore hints and swap in new dialplan: 0.000005 sec
-- Time to delete the old dialplan: 0.000087 sec
-- Total time merge_contexts_delete: 0.000422 sec
-- pbx_config successfully loaded 27 contexts (enable debug for details).
Output of the GoTo application with alternative priority :
-- Executing [167@internal:1] NoOp("PJSIP/168-00000014", "Call started for 167") in new stack
-- Executing [167@internal:2] Goto("PJSIP/168-00000014", "4") in new stack
-- Goto (internal,167,4)
-- Executing [167@internal:4] Playback("PJSIP/168-00000014", "hello") in new stack
> 0x7f5330040900 -- Strict RTP learning after remote address set to: 192.168.6.101:4024
> 0x7f5330040900 -- Strict RTP learning after remote address set to: 192.168.6.101:4024
-- <PJSIP/168-00000014> Playing 'hello.ulaw' (language 'en')
> 0x7f5330040900 -- Strict RTP switching to RTP target address 192.168.6.101:4024 as source
-- Executing [167@internal:5] Dial("PJSIP/168-00000014", "PJSIP/167") in new stack
-- Called PJSIP/167
-- PJSIP/167-00000015 is ringing
> 0x7f5330040900 -- Strict RTP learning complete - Locking on source address 192.168.6.101:4024
== Everyone is busy/congested at this time (1:1/0/0)
-- Auto fallthrough, channel 'PJSIP/168-00000014' status is 'BUSY'
Step – 10: Edit the extensions.conf file to use the GotoIf Application by using the below command
root@linuxhelp1:/etc/asterisk# vim extensions.conf
Add the following line
[internal]
exten => _XXX,1,Set(repeat=5)
exten => _XXX,n(repeat),Playback(hello)
exten => _XXX,n,Set(repeat=$[ ${repeat} - 1])
exten => _XXX,n,GotoIf($[ ${repeat} > 0]?repeat:continue)
exten => _XXX,n(continue),Dial(PJSIP/${EXTEN})
Step 11: Login to the Asterisk CLI mode by using the below command
root@linuxhelp1:/etc/asterisk# 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 = 1427)
Step 12: Reload the dialplan by using the below command
linuxhelp1*CLI> dialplan reload
Dialplan reloaded.
-- Including switch 'DUNDi/e164' in context 'ael-dundi-e164-switch'
-- Including switch 'Lua/' in context 'local'
-- Including switch 'Lua/' in context 'demo'
-- Including switch 'Lua/' in context 'default'
-- Including switch 'Lua/' in context 'public'
-- Time to scan old dialplan and merge leftovers back into the new: 0.000336 sec
-- Time to restore hints and swap in new dialplan: 0.000005 sec
-- Time to delete the old dialplan: 0.000071 sec
-- Total time merge_contexts_delete: 0.000412 sec
-- pbx_config successfully loaded 27 contexts (enable debug for details).
Output of the GotoIf Application
-- Executing [167@internal:1] Set("PJSIP/169-00000016", "repeat=5") in new stack
-- Executing [167@internal:2] Playback("PJSIP/169-00000016", "hello") in new stack
> 0x7f5330040900 -- Strict RTP learning after remote address set to: 192.168.6.101:4000
-- <PJSIP/169-00000016> Playing 'hello.ulaw' (language 'en')
> 0x7f5330040900 -- Strict RTP switching to RTP target address 192.168.6.101:4000 as source
-- Executing [167@internal:3] Set("PJSIP/169-00000016", "repeat=4") in new stack
-- Executing [167@internal:4] GotoIf("PJSIP/169-00000016", "1?repeat:continue") in new stack
-- Goto (internal,167,2)
-- Executing [167@internal:2] Playback("PJSIP/169-00000016", "hello") in new stack
-- <PJSIP/169-00000016> Playing 'hello.ulaw' (language 'en')
-- Executing [167@internal:3] Set("PJSIP/169-00000016", "repeat=3") in new stack
-- Executing [167@internal:4] GotoIf("PJSIP/169-00000016", "1?repeat:continue") in new stack
-- Goto (internal,167,2)
-- Executing [167@internal:2] Playback("PJSIP/169-00000016", "hello") in new stack
-- <PJSIP/169-00000016> Playing 'hello.ulaw' (language 'en')
-- Executing [167@internal:3] Set("PJSIP/169-00000016", "repeat=2") in new stack
-- Executing [167@internal:4] GotoIf("PJSIP/169-00000016", "1?repeat:continue") in new stack
-- Goto (internal,167,2)
-- Executing [167@internal:2] Playback("PJSIP/169-00000016", "hello") in new stack
-- <PJSIP/169-00000016> Playing 'hello.ulaw' (language 'en')
-- Executing [167@internal:3] Set("PJSIP/169-00000016", "repeat=1") in new stack
-- Executing [167@internal:4] GotoIf("PJSIP/169-00000016", "1?repeat:continue") in new stack
-- Goto (internal,167,2)
-- Executing [167@internal:2] Playback("PJSIP/169-00000016", "hello") in new stack
-- <PJSIP/169-00000016> Playing 'hello.ulaw' (language 'en')
-- Executing [167@internal:3] Set("PJSIP/169-00000016", "repeat=0") in new stack
-- Executing [167@internal:4] GotoIf("PJSIP/169-00000016", "0?repeat:continue") in new stack
-- Goto (internal,167,5)
-- Executing [167@internal:5] Dial("PJSIP/169-00000016", "PJSIP/167") in new stack
-- Called PJSIP/167
-- PJSIP/167-00000017 is ringing
> 0x7f5330040900 -- Strict RTP learning complete - Locking on source address 192.168.6.101:4000
== Everyone is busy/congested at this time (1:1/0/0)
-- Auto fallthrough, channel 'PJSIP/169-00000016' status is 'BUSY'
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to use GoTo and GotoIf Application on Asterisk. Your feedback is much welcome.
Comments ( 0 )
No comments available