Create an IVR in Asterisk using Java

Create an IVR in Asterisk using Java


In this post, we will explain how to install and run FreePBX (GPL), a Web-based GUI to control and manage Asterisk PBX, and control incoming phone calls using Java and the Asterisk FastAGI with a custom IVR. We will explain how to configure the system to run with its basic features. For further management, read FreePBX user manuals. First, let’s install FreePBX.

Installing FreeBPX

  1. Download the official release image from http://schmoozecom.com/distro-download.php
  2. Install the ISO on a PC or a Virtual Machine.
  3. After installing FreePBX, access the GUI using a WebBrowser. Access the IP address of the PC hosting the FreePBX.
  4. Set your main username and password.
  5. Select FreeBPX Administration to start managing your Asterisk PBX.
Screen Shot 2015-02-10 at 4.10.35 PM

Congratulations! You have now Asterisk PBX up and running.

In order to receive incoming calls you should configure your SIP trunk into Asterisk:

Connect Asterisk to SIP Trunk

  1. Go to Connectivity – Trunks. In our example, select Add SIP (chain_sip) Trunk.
  2. Configure your SIP parameters. It is usual of SIP Trunks to provide every parameter needed to configure it in Asterisk. After configuring the SIP Trunk, go to Connectivity-Inbound Routes to create rules for incoming calls. In the Inbound Routes add a Route specifying at least your DID Number from the SIP Trunk. Set a destination for the incoming calls from the DID number. For example, select Phonebook Directory to ask the caller for a phonebook number.

Screen Shot 2015-02-10 at 4.18.11 PM

Screen Shot 2015-02-10 at 4.18.11 PM

  1. Save and apply changes.
Screen Shot 2015-02-10 at 4.18.11 PM

Test your Asterisk calling from your phone to your SIP number. You should listen to a machine asking for a phonebook number.

Program de IVR

To control the incoming call using custom programming, you should use the Asterisk FastAGI. In our case, we will create an AGI script using Java and Asterisk-Java library:

  1. Download the Asterisk-Java library from http://www.asterisk-java.org/development/ from its SVN repository as follows:
svn co http://svn.reucon.net/repos/asterisk-java/trunk asterisk-java

  1. Access the asterisk-java folder where you downloaded the project and install the JAR file in your local Maven repository:
make install

  1. Get the JAR file named asterisk-java.jar built and copy it to your test folder where you will place your AGI script. Write a JAVA file called HelloIVRScript.java where you will write your IVR steps. For example, write the following example into the JAVA file:
import org.asteriskjava.fastagi.AgiChannel; import org.asteriskjava.fastagi.AgiException; import org.asteriskjava.fastagi.AgiRequest; import org.asteriskjava.fastagi.BaseAgiScript; public class HelloAgiScript extends BaseAgiScript { public void service(AgiRequest request, AgiChannel channel) throws AgiException { //Answer the channel… answer(); //Play speed-enternumber sound while waiting for the user //to enter 4 digits. We wait for a 20 seconds timeout //in case digits are not dialed. String numbers = getData("speed-enternumber", 20000L, 4); //Work with dialed digits //If the user pressed 2-0-0-1, ask for another number and store it //If the user pressed 1-0-0-1, let the user leave a voice message if(numbers.equal("2001")){ String finalNumbers = getData("vqplus-enter-number", 20000L, 2); //Store finalNumbers into your database or do something else //… }else if(numbers.equal("1001")){ //Record the voice for 10 seconds or until the user press 2 recordFile("voiceRecord","wav","2",10000); } //Terminate the phone call hangup(); }

  1. Note: You can use many default sounds, located in folder
/var/lib/asterisk/sounds

  1. or you can record files on your own to use it. Compile your Java file:
javac -cp asterisk-java.jar HelloIVRScript.java

  1. In the same folder, create a file named:
    fastagi-mapping.properties and place the following line inside the file:
Doing this, you specified that your IVR script is published into the URL: agi://localhost/hello.agi which you will use in the next steps. Edit the file: /etc/asterisk/extensions-custom.conf and add the following two lines:

  1. This means that when a phone call arrives at extension 1000 in your Asterisk, the hello.agi script will be invoked.
  2. Go to Admin – Custom Destinations and create a new Custom Destination with the following information:
Screen Shot 2015-02-10 at 4.18.11 PM
  1. Save and go to Connectivity – Inbound Routes as previously done.
  2. Select the Inbound Route you previously created and in the Set Destination section, set the following configuration:
Screen Shot 2015-02-10 at 5.00.49 PM


Save and apply changes.

  1. Test your IVR calling your SIP number again, and introducing numbers 2001 to enter two additional numbers or 1001 to leave a voice message. In case you leave a voice message, it will be stored in your “/var/lib/asterisk/sounds” folder in a file named voiceRecord.wav.

See more posts
Have a project or an idea?
Let's make it real!
Prefer a meeting?
Book a meeting or call us (512) 842 9784
Yes! Book me
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
PARTNERS
ECOSYSTEM
PRODUCTS