Hamid Fadishei's Blog

June 27, 2010

AGI Gosub Command Support in Asterisk-Java

Filed under: Asterisk,Java — fadishei @ 4:36 am

Today, I had my first experience of contributing to an open-source project. The story is that a few weeks ago when I needed to execute an AGI Gosub command, I found out this command is not implemented in Asterisk-Java yet. Thus I decided to do some coding myself. I sent the result to the project’s JIRA by opening a new feature issue. Kindly, Stefan Reuter (funder of Asterisk-Java) appreciated the work and helped me to improve it. Finally, the new Gosub command added to the SVN and will be available in the 1.0.0 release.

The Gosub Command

This command has been added to Asterisk since 1.6 version. It lets you to call a subroutine in your dialplan from AGI script. You can also pass some optional arguments to the subroutine. Although an AGI developer may find this feature useful in several situations, personally I most benefit from gosub command in indirectly calling applications for whom no dedicated AGI command is available. An example is doing a “Queue” application as exec(“Queue”, …) may not give you all the expected behavior.

As an example of using this command, consider that you have this simple subroutine in your dialplan:

exten => 190,1,AGI(agi://localhost/gosub.agi)

exten => s,1,NoOp(arg1: ${ARG1})
exten => s,n,NoOp(arg2: ${ARG2})
exten => s,n,Playback(hello-world)
exten => s,n,Return()

Then you can call this subroutine like this:

public class TestGosub extends BaseAgiScript
        public void service(AgiRequest request, AgiChannel channel) throws AgiException
                gosub("sub-hello", "s", "1", "a", "b");

As it can be seen in the code, the first three arguments to the gosub method are the context, extension, and priority you want to call. The rest of parameters are optional arguments that will be available in the subroutine as variables ${ARG1}, ${ARG2}, etc.

Some Introductory Information

For people who are not familiar with Asterisk, I would like to add the following paragraphs.

Aboute Asterisk

Asterisk is an open-source software-based PBX system that lets you do telecommunications works on a Linux box. You can build auto-attendants, call centers, help desks, IVRs, VoIP providers etc with Asterisk.

About AGI

As a programmer, you can command at your will to an Asterisk box what to do. This feature is available through a standard language for talking between Asterisk and your program known as Asterisk Gateway Interface (AGI). This feature, along with another similar functionality known as Asterisk Manager Interface (AMI) makes a software developer capable of implementing almost any imaginable telecommunications application. An example application is one that reads out your new emails to you when you call it. Another application can be an IVR that dynamically adjusts its menu items based on user requirements.

About Asterisk-Java

Asterisk-Java is a library for doing AGI from Java programs. Personally, I think Asterisk-Java turns telecommunications application development into fun!



  1. Hi Hamid, I am ruli, from indonesia.
    Did you ever run the DefaultAgiServer (fastagi) in eclipse with successfully ?
    I have tried to run it with eclipse, but it always print some error log like this :

    Apr 23, 2012 8:41:59 PM org.asteriskjava.fastagi.DefaultAgiServer startup
    INFO: Listening on *:5050.
    Apr 23, 2012 8:42:06 PM org.asteriskjava.fastagi.DefaultAgiServer startup
    INFO: Received connection from /
    Apr 23, 2012 8:42:06 PM org.asteriskjava.fastagi.AbstractAgiServer getPool
    INFO: Thread pool started.
    Apr 23, 2012 8:42:06 PM org.asteriskjava.fastagi.internal.AgiConnectionHandler run
    SEVERE: No script configured for URL ‘agi://’ (script ‘telmo.agi’)

    I create two important file (fastagi-mapping.properties and fastagi.properties ) in the same directory of asterisk-java.jar.
    I configure the port to listen with 5050.

    please, may you can figure out what my problem is.

    thanks before.

    sory for my english 🙂

    Comment by Roe Lee — April 23, 2012 @ 2:11 pm | Reply

    • Roe,
      I use spring framework for my AGI projects. My mapping strategy is stored as spring xml bean definition not in a properies file. I guess you should store your mapping file in a folder which is on the classpath (pass -cp to jvm).

      Comment by fadishei — April 29, 2012 @ 4:02 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: