DEV Community


Posted on

Asterisk AGI short sharing

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.



Sharing of how I made my own command


Today, I am going to start integrating my previous call flow with the use of AGI. To do that, we need the help of AGI (Asterisk Gateway Interface). The reason we might need to do that is that sometimes we want to record every call and insert the call logs into the database. So that, we could know what calls have been made or received. Moreover, the use of database could help us create an IVR , for it could contain all the IVR logic we have.


What is AGI ?

AGI provides an interface between the Asterisk dialplan and an external program that wants to manipulate a channel in the dialplan.


PostgreSQL installation & DB creation

# Install PostgreSQL
sudo apt install postgresql postgresql-contrib

# start the postgres service
sudo service postgresql start

# Change to user postgres as it can perform all tasks on the database
sudo su postgres

# login to the DB

# You should see something similar in your terminal

# Create database
# don't miss the colon
CREATE Database <your-database-name-here>;

# Create an account to manage the database
# don't miss the colon and the quotation marks
CREATE USER <your-username-here> WITH PASSWORD 'your-password-here' SUPERUSER;

# Leaving Database and postgres user
ctrl + d 
ctrl + d

# Try login from the current user
psql -U <user_name> -W -h <hostname> <db_name>
Enter fullscreen mode Exit fullscreen mode

Using AGI in Asterisk

In Asterisk, AGI can be treated as a dialplan application in extensions.conf. In the following, I will show you how you could use it as a way to insert data into the PostgreSQL database.

In modules.conf

# add the agi module
load =
Enter fullscreen mode Exit fullscreen mode

In pjsip.conf

type = transport
protocol = udp
bind =

allow = !all, ulaw, alaw     

type = auth              
auth_type = userpass     
type = aor
max_contacts = 1         

callerid = 7000 <7000>   

password = 7000
username = 7000


callerid = 7100 <7100>

password = 7100
username = 7100

Enter fullscreen mode Exit fullscreen mode

In extensions.conf

exten = _7X00,1,NoOp(${EXTEN})
; setting variables for data insertion
same = n,Set(_callInTime=${STRFTIME(,,%Y%b%d-%H%M%S)})
same = n,Set(_callerId=${CALLERID(num)})
same = n,Set(_extension=${EXTEN})
same = n,Set(_callDuration=${CDR(billsec)})
same = n,Answer
same = n,Dial(PJSIP/${EXTEN},30)
same = n,Goto(update_call_duration,s,1)

; call_log is the agi file, the rest are the arguments
exten = s,1,agi(call_log,${callerId},${callInTime},${extension},${callDuration})
same = n,Hangup
Enter fullscreen mode Exit fullscreen mode

In the extensions.conf, we use the agi dialplan application. Think of it as running a program like node abc.js or python3


So, where is our program ?

Actually, we need to go to the default agi-bin path to create the program.

But that would be too much for me today. So, please bear with me and stay tuned for the next week sharing and I will go deeper into the agi program.

Top comments (0)