There’re a AGI Java example to use it. This example could be compiled under GNU/Linux or Windows.
// RechargeAgi.java Card Recharging by imran ( Pakistan )
import java.io.*;
import java.sql.*;
import java.util.*;
import net.sf.asterisk.fastagi.AGIChannel;
import net.sf.asterisk.fastagi.AGIException;
import net.sf.asterisk.fastagi.AGIRequest;
import net.sf.asterisk.fastagi.AbstractAGIScript;
/*
MySql Table
Cards
pkid | uid | pin | talktime | chargeddate
- /
class rechargeAGI extends AbstractAGIScript
{
public Statement mystatement = null;
public Connection myconnection = null;
public void dbconnection() throws Exception
{
try
{
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
String con = “jdbc:mysql://localhost/gitpbx?user=dd&password=dd”;
myconnection = DriverManager.getConnection(con);
mystatement = myconnection.createStatement();
}catch (Exception e )
{
System.out.println(“DB Connection failed”);
}
}
public void service(AGIRequest request, AGIChannel channel)throws AGIException
{
try{
dbconnection();
}catch ( Exception e )
{
System.out.println(“Connection failed”);
}
String myPIN=””;
String sql =””;
ResultSet rs;
String mymenuoption=””;
String CallerID = request.getParameter(“callerid”);
String CalledNUM = request.getParameter(“calledNUM”);
try
{
sql =”select uid,pin,talktime from Cards where uid ='”+CallerID+”‘”;
rs = mystatement.executeQuery(sql);
if( !rs.next() )
{
//stream message invalid uid
streamFile(channel,”Invalild-CallerID-Called”);
System.exit(0);
}
// play a menu 1 to 4 message here
for ( int i = 0; i<3;i++)
{
mymenuoption=””;
mymenuoption=request.getDnid();
if ( mymenuoption.equals(“”) || ( Integer.parseInt(mymenuoption) > 4 ) )
{
continue;
}
else
{
if( i < 3 )
{
streamFile(channel,”Invalid-option-selected”);
streamFile(channel,”Thanks-for-using”);
System.exit(0);
}
if ( mymenuoption.equals(“2”) || mymenuoption.equals(“3”) )
{
for ( int j=0; j<3; j++ )
{
// Please enter your pin code
myPIN=””;
myPIN =request.getDnid();
if ( myPIN.equals(“”) || myPIN.length() < 14 )
{
continue;
}
else
{
if( j < 3 )
{
streamFile(channel,”Invalid-PIN-code-entered”);
break;
}
sql =”select uid,pin,talktime from Cards where uid ='”+CallerID+”‘ and pin='”+myPIN+”‘”;
rs = mystatement.executeQuery(sql);
if( !rs.next() )
{
//stream message invalid uid
streamFile(channel,”no-record-have-found”);
}
else
{
if ( mymenuoption.equals(“2”) )
{
sql =”update Cards set talktime= talktime + ‘500’,chargeddate=sysdate() where uid ='”+CallerID+”‘ and pin='”+myPIN+”‘”;
mystatement.executeQuery(sql);
streamFile(channel,”Card-charged-successfully”);
}
if ( mymenuoption.equals(“3”) )
{
sql =”update Cards set talktime=talktime + ‘1000’,chargeddate=sysdate() where uid ='”+CallerID+”‘ and pin='”+myPIN+”‘”;
mystatement.executeQuery(sql);
streamFile(channel,”Card-charged-successfully”);
}
sql =”select uid,pin,talktime from Cards where uid ='”+CallerID+”‘”;
rs = mystatement.executeQuery(sql);
mymenuoption =”1″;
break;
}
}
}
}
// Say CallerID’s balance in dollars n cents
if ( mymenuoption.equals(“1”) )
{
int dollar=(Integer.parseInt(rs.getString(“talktime”))/100);
int cents =(Integer.parseInt(rs.getString(“talktime”))%100);
streamFile(channel,”You-have-balance”);
sayNumber(channel,String.valueOf(dollar));
streamFile(channel,”Dollars”);
sayNumber(channel,String.valueOf(cents));
streamFile(channel,”Cents”);
}
if ( mymenuoption.equals(“4”) )
{
streamFile(channel,”Thanks-for-using”);
System.exit(0);
}
}
}
}catch (Exception e )
{
streamFile(channel,”Application-is-temporarliy-not-functioning”);
System.exit(0);
}
}
}