Asterisk AGI short sharing 
Tags: agi, asterisk, database, beginners
Published: 2021-07-02 02:24:48

## 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. 

&nbsp;
&nbsp;
### Previously
<a href="https://dev.to/ekim34351855/sharing-of-how-i-made-my-own-command-5dfl">Sharing of how I made my own command</a>
&nbsp;
&nbsp;

### Introduction

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. 

&nbsp;
&nbsp;
### What is AGI ? 

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


&nbsp;&nbsp;
### PostgreSQL installation & DB creation

```bash
# 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
psql

# You should see something similar in your terminal
postgres=# 

# 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>
```



### 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. 

&nbsp;
&nbsp;
In modules.conf

```bash
# add the agi module
load = res_agi.so
```

&nbsp;
&nbsp;
In pjsip.conf

```bash
[transport-udp-nat]
type = transport
protocol = udp
bind = 0.0.0.0

[calling](!)				 
type=endpoint				 
context=interaction			 
allow = !all, ulaw, alaw	 
direct_media=no				 
trust_id_outbound=yes
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
device_state_busy_at=1
dtmf_mode=rfc4733

[auth-userpass](!)		 
type = auth				 
auth_type = userpass	 
[aor-single-reg](!)		
type = aor
max_contacts = 1		 

[7000](calling)			 
auth=7000				 
aors=7000				 
callerid = 7000 <7000>	 

[7000](auth-userpass)	 
password = 7000
username = 7000

[7000](aor-single-reg)

[7100](calling)
auth=7100
aors=7100
callerid = 7100 <7100>

[7100](auth-userpass)
password = 7100
username = 7100

[7100](aor-single-reg)
```

&nbsp;
&nbsp;
In extensions.conf

```bash
[interaction]
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)

[update_call_duration]
; call_log is the agi file, the rest are the arguments
exten = s,1,agi(call_log,${callerId},${callInTime},${extension},${callDuration})
same = n,Hangup
```

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

&nbsp;
&nbsp;
### 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.