How to use AGI Script in Asterisk on Ubuntu 22.04
To Use AGI Script In Asterisk On Ubuntu 22.04
Introduction :
Asterisk Gateway Interface (AGI) provides an interface between the Asterisk dialplan and an external program that wants to manipulate a channel in the dialplan. In general, the interface is synchronous - actions taken on a channel from an AGI block and do not return until the action is completed.
Procedure :
Step 1: Check the OS version by using the below command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
Step 2: I have already installed Asterisk on my Ubuntu machine, Now I am going to check the status of the Asterisk by using the below command
root@linuxhelp:~# systemctl status asterisk
● asterisk.service - LSB: Asterisk PBX
Loaded: loaded (/etc/init.d/asterisk; generated)
Active: active (running) since Thu 2023-09-14 10:24:21 IST; 2 months 13 days ago
Docs: man:systemd-sysv-generator(8)
Process: 1369 ExecStart=/etc/init.d/asterisk start (code=exited, status=0/SUCCESS)
Tasks: 73 (limit: 4489)
Memory: 133.4M
CPU: 6min 24.098s
CGroup: /system.slice/asterisk.service
└─1391 /usr/sbin/asterisk
Sep 14 10:24:20 linuxhelp systemd[1]: Starting LSB: Asterisk PBX...
Sep 14 10:24:21 linuxhelp asterisk[1369]: * Starting Asterisk PBX: asterisk
Sep 14 10:24:21 linuxhelp asterisk[1369]: ...done.
Sep 14 10:24:21 linuxhelp systemd[1]: Started LSB: Asterisk PBX.
Sep 14 10:24:24 linuxhelp asterisk[1391]: radcli: rc_read_config: rc_read_config: can't open /etc/radiusclient-ng/radiusc>
Sep 14 10:24:24 linuxhelp asterisk[1391]: radcli: rc_read_config: rc_read_config: can't open /etc/radiusclient-ng/radiusc>
Step 3: Now change the directory to Asterisk default AGI directory by using the below command
root@linuxhelp:~# cd /var/lib/asterisk/agi-bin/
Step 4: List the directory by using the below command
root@linuxhelp:/var/lib/asterisk/agi-bin# ls
agi-test.agi eagi-sphinx-test eagi-test jukebox.agi
Step 5: Create a new file using vim editor by using the below command
root@linuxhelp:/var/lib/asterisk/agi-bin# vim testagi
Add the following lines in the file
#!/usr/bin/bash
echo "SET VARIABLE rep $1"
echo "SET VARIABLE exten $2"
Step 6: Now give the execute permission to the file by using the below command
root@linuxhelp:/var/lib/asterisk/agi-bin# chmod +x testagi
Step 7: Change the directory to the asterisk configuration directory by using the below command
root@linuxhelp:/var/lib/asterisk/agi-bin# cd /etc/asterisk/
Step 8: Now edit the extensions.conf file to change the dialplan by using the below command
root@linuxhelp:/etc/asterisk# vim extensions.conf
Add the following lines in the file
[internal]
exten => _XXX,1,AGI(testagi,3,${EXTEN})
exten => _XXX,n,NoOp(Dialing Extension is =============== ${exten} and the repeat is ========== ${rep})
exten => _XXX,n,Set(repeat=${rep})
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 9: Login to the Asterisk CLI mode by using the below command
root@linuxhelp:/etc/asterisk# asterisk -rvvvvvvvvvvvvvvvvvvvvvvv
Asterisk 20.4.0, 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.4.0 currently running on linuxhelp (pid = 1391)
Step 10: After the changes in the extensions.conf file, need to reload the dialplan by using the below command
linuxhelp*CLI> dialplan reload
Dialplan reloaded.
-- Including switch 'Lua/' in context 'local'
-- Including switch 'Lua/' in context 'demo'
-- Including switch 'Lua/' in context 'public'
-- 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.001192 sec
-- Time to restore hints and swap in new dialplan: 0.000009 sec
-- Time to delete the old dialplan: 0.000175 sec
-- Total time merge_contexts_delete: 0.001376 sec
-- pbx_config successfully loaded 27 contexts (enable debug for details).
Output :
-- Executing [167@internal:1] AGI("PJSIP/168-00000002", "testagi,3,167") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/testagi
-- <PJSIP/168-00000002>AGI Script testagi completed, returning 0
-- Executing [167@internal:2] NoOp("PJSIP/168-00000002", "Dialing Extension is =============== 167 and the repeat is ========== 3") in new stack
-- Executing [167@internal:3] Set("PJSIP/168-00000002", "repeat=3") in new stack
-- Executing [167@internal:4] Playback("PJSIP/168-00000002", "hello") in new stack
> 0x7ff108028740 -- Strict RTP learning after remote address set to: 192.168.6.107:4002
> 0x7ff108028740 -- Strict RTP learning after remote address set to: 192.168.6.107:4002
-- <PJSIP/168-00000002> Playing 'hello.ulaw' (language 'en')
> 0x7ff108028740 -- Strict RTP switching to RTP target address 192.168.6.107:4002 as source
-- Executing [167@internal:5] Set("PJSIP/168-00000002", "repeat=2") in new stack
-- Executing [167@internal:6] GotoIf("PJSIP/168-00000002", "1?repeat:continue") in new stack
-- Goto (internal,167,4)
-- Executing [167@internal:4] Playback("PJSIP/168-00000002", "hello") in new stack
-- <PJSIP/168-00000002> Playing 'hello.ulaw' (language 'en')
-- Executing [167@internal:5] Set("PJSIP/168-00000002", "repeat=1") in new stack
-- Executing [167@internal:6] GotoIf("PJSIP/168-00000002", "1?repeat:continue") in new stack
-- Goto (internal,167,4)
-- Executing [167@internal:4] Playback("PJSIP/168-00000002", "hello") in new stack
-- <PJSIP/168-00000002> Playing 'hello.ulaw' (language 'en')
-- Executing [167@internal:5] Set("PJSIP/168-00000002", "repeat=0") in new stack
-- Executing [167@internal:6] GotoIf("PJSIP/168-00000002", "0?repeat:continue") in new stack
-- Goto (internal,167,7)
-- Executing [167@internal:7] Dial("PJSIP/168-00000002", "PJSIP/167") in new stack
-- Called PJSIP/167
-- PJSIP/167-00000003 is ringing
== Everyone is busy/congested at this time (1:1/0/0)
-- Auto fallthrough, channel 'PJSIP/168-00000002' status is 'BUSY'
> 0x7ff108028740 -- Strict RTP learning complete - Locking on source address 192.168.6.107:4002
linuxhelp*CLI>
linuxhelp*CLI>
linuxhelp*CLI>
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to use AGI script on Asterisk on Ubuntu 22.04. Your feedback is much welcome.
# systemctl restart asterisk