Lately I’ve been involved in some work projects related to Asterisk.
I had the opportunity to play with the Asterisk Gateway Interface (AGI) to see what can be done with it.
In a few words, it allows you to execute scripts on an action (eg: a user calling an extension), that will interact with other systems (eg: retrieve information from a database and store other input data) and trigger other actions.
First of all, given that I’m more familiar with PHP than Perl, I looked around for libraries that can interact with my loved PHP.
You can get a lot of useful information at http://voip-info.org, being more specific at: http://www.voip-info.org/wiki/view/Asterisk+AGI
This will help you understand what can and cannot be done…
I used one of our company (Fonality Australia) trixbox Pro test systems for playing around with this.
What I was interested on, was specifically the FastAGI. Basically call remote agi scripts that communicate with your Asterisk system via tcp sockets (more info here).
This is interesting because it allows you to install software in your remote machine, without exposing your trixbox appliance to new software (even unsupported one).
First of all I’ve installed the phpagi library (that can be found on sourceforge at http://phpagi.sourceforge.net/) and had a look at the code.
The server side part (so the one that the trixbox system will call in the next steps) will execute as a deamon with the aid of xinetd on your linux box.
In my case xinetd wasn’t installed, and in a Debian flavoured linux distribution you can easily install it with “sudo apt-get install xinetd”, and of course you can get more info about xinetd at http://xinetd.org.
You can get more info on how all the the super server system is structured in the wikipedia inetd page here
Of course you have to make sure you have a working command line php server on your linux box (for Debian flavour: sudo apt-get install php5-cli) and test it running a command line script (php -f file.php).
As a first step of the installation of the library you need to unzip it somewhere in your linux box and that path will be from now on _PHP_LIBRARY_PATH
You need to list the server port inside the services list (/etc/services). So at the bottom add the line “fastagi 4573/tcp”.
And then to add a config entry inside /etc/xinetd.d/fastagi.
For testing I’ve entered as follows:
service fastagi
{
socket_type = stream
user = test
group = test
server = _PHP_LIBRARY_PATH/phpagi-fastagi.php
wait = no
protocol = tcp
disable = no
}
After restarting xinetd, our server side is ready to go and will accept requests from external IPs.
Just for being safe, I would ssh on a remote machine and see if we can telnet into the port of our FastAGI server and see if the server is listening (telnet IP_ADDRESS 4573).
All good? Let’s go.
Now we create a sample agi script. I will write the php file at _PHP_LIBRARY_PATH/sample.php
with just this as a content:
<?php
$fastagi->verbose('cool, the FastAGI server has been called!');
?>
Now we go inside our nice little trixbox menu interface and we add an agi script entry on the trixbox call menu with this syntax: agi://agi_server_ip_address/sample.php and save.
We can now ssh inside our trixbox box (or whatever asterisk based system you have) and access the asterisk cli interface with “asterisk -r” and enable the agi debugging with “agi debug”.
Now you can try to call your trixbox, and when you will reach the right menu steps you are calling the AGI script with… you will see the command sent from the php library to your Asterisk system!
Now you can have a play with the library and easily extend your phone system with new fancy features!
Using the instance of the AGI object $fastagi you can call any method of the phpagi library, listed and explained in here
You can do a lot of other fancy stuff with AGI, like communicating via soap with other systems, accessing data, saving data, recording messages, playing messages, read and play DTMF tones, even read new messages with text to speech libraries and the other way around!
I guess there is no limit to the possibility it can give to your phone system!
See you next!
Recent Comments