Contact

Blogs

Take a look at the things that interest us.

Asterisk Dial Plan with Agi and PHP

Wesley Wesley 3 years

In this post, I would like to go in-depth with how to use the Asterisk Agi function in your dial plan. For this example, I will use a simple use case where user A calls user B and with PHP we'll check if this user has the right permissions to make the call.

Requirements

  • Asterisk 16 or 18
  • Basic Sip Account set up
  • PHP installed on server
  • Composer installed on server

What is the Asterisk Agi

The Asterisk Agi, more commonly known as the Asterisk Gateway Interface, is an interface for adding coding languages functionality to Asterisk.

Our Use Case

In our example we will use the following use case.

  • User A tries to call user B
  • With Agi we'll check if user A has the right permission to make the call
  • Call is done

Getting started

Before we dive deeper into the Asterisk Agi function, it is important to know that the coding language files for Asterisk are located in the following directory:

$ /var/lib/asterisk/agi-bin

Also, always make sure that your coding language files are executable. This can be done with the following command.

$ chmod 755 /var/lib/asterisk/agi-bin/*.php

Asterisk Agi Dial Plan

As we have mentioned before in our use case, in this example we will make a normal call between user A and user B. To do this, we'll need to get started with our extension.conf. First, let's move to our Asterisk directory and open the extension.conf file.

$ cd /etc/asterisk
$ vi /extension.conf

Here we'll need to add the following parameters.

[extensions]
exten => _1XX,1,Ringing
exten => _1XX,n,Wait()
exten => _1XX,n,Dial(PJSIP/${EXTEN},60,tT)
exten => _1XX,n,Answer()
exten => _1XX,n,Hangup

This allows us to make a basic call between user A and user B. The next step is adding the Agi function to our dial plan.

[extensions]
exten => _1XX,1,Ringing
exten => _1XX,n,Wait()
exten => _1XX,n,AGI(do-something.php, ${EXTEN}, ${CALLERID(num)})
exten => _1XX,n,GotoIf($["${response}" = ""]?hangup,1)
exten => _1XX,n,Dial(PJSIP/${EXTEN},60,tT)
exten => _1XX,n,Answer()
exten => _1XX,n,Hangup()
exten => hangup,1,NoOp(send hangup signal)
exten => hangup,n,Hangup()

This allows us to execute a PHP script and do a GoToIf on the actual response values.

Asterisk Agi PHP script

After creating our Asterisk dial plan it's time to create our PHP script. First, let's move to the Asterisk Agi directory, install the phpagi library through composer, create a new file called do-something.php, and open the file.

$ cd /var/lib/asterisk/agi-bin
$ composer require welltime/phpagi ^2.20
$ touch do-something.php
$ vi do-something.php

Here we will add the following code to the file and save it.

#!/usr/bin/env /bin/php
<?php

# composer
require __DIR__ . '/vendor/autoload.php';

# create new Agi
$agi = new AGI();

    // set parameters
    $to = $argv[1];
    $from = $argv[2];

    // check permission
    // you can call here whatever you want
    // from a database or a local file
    $permission = true;

    # set return variable
    $agi->set_variable("response", $permission);

The current PHP code doesn't do much at this time, but it gives you an idea of what you can do with the Asterisk Agi function. When you save the file you can start making your first test call from user A to user B.

As you can see, the call seems just like a normal call.

Now, let's update the response to false. If you make another test call, you'll see that the call is hanged up after the user makes it.

This was it for the Asterisk Agi, got any questions, need some help, let us know in the comments below.

Asterisk Dial Plan with Agi and PHP 2021-08-24 08:10:05

Awesome!!!!!!!!!! Thank you for so many details.

  • 2 years

and how to execute and present an output??

  • 1 year
4905

Have questions about our services?

Contact our Sales team to get answers.

Contact Us
gomibako@aska-ltd.jp