How to get Call details through AMI

Greetings Asterisk community,

I have to create standalone tool to archive Calls with it’s own database,
I am looking to do it on real time, so I select using AMI to do this.

Following code give me some of needs , but still miss call duration , and real caller:

public class Ast implements ManagerEventListener
{
    private ManagerConnection managerConnection;

    public Ast() throws IOException
    {
        ManagerConnectionFactory factory = new ManagerConnectionFactory(
                "x.xx.xx", "xxxx", "xxx");

        this.managerConnection = factory.createManagerConnection();
        
        
        
    }

    public void run() throws IOException, AuthenticationFailedException,
    TimeoutException, InterruptedException
    {
    	

        // register for events
        managerConnection.addEventListener(this);

        // connect to Asterisk and log in
        managerConnection.login();

        // request channel state
        managerConnection.sendAction(new StatusAction());

        // wait 10 seconds for events to come in
        Thread.sleep(1000000000);

        // and finally log off and disconnect
        managerConnection.logoff();
    }

    public static void main(String[] args) throws Exception
    {
        Ast helloManager;

        helloManager = new Ast();
        helloManager.run();
    }

    public void onManagerEvent(ManagerEvent event)
    {
    	
		System.out.println(" MAnager E "+event);


    	if(event instanceof CdrEvent){
    		//System.out.println(event);
    		CdrEvent ev =  (CdrEvent)event;
    		System.out.println("caller ID "+ev);
    		
    		}
    	
        
    }
    

    
}

Result is :

org.asteriskjava.manager.event.HangupEvent[dateReceived=‘Wed Apr 19 12:51:32 GST 2017’,privilege=‘call,all’,server=null,calleridname=‘Ahmad Osman’,channel=‘SIP/111-00000012’,cause=‘16’,connectedlinename=‘Ahmad Albat’,sequencenumber=null,calleridnum=‘111’,causetxt=‘Normal Clearing’,callerid=‘111’,connectedlinenum=‘225’,uniqueid=‘1492591874.18’,timestamp=null,systemHashcode=906972885]

org.asteriskjava.manager.event.HangupEvent[dateReceived=‘Wed Apr 19 12:51:32 GST 2017’,privilege=‘call,all’,server=null,calleridname=‘Ahmad Albat’,channel=‘SIP/225-00000011’,cause=‘16’,connectedlinename=‘Ahmad Osman’,sequencenumber=null,calleridnum=‘225’,causetxt=‘Normal Clearing’,callerid=‘225’,connectedlinenum=‘111’,uniqueid=‘1492591874.17’,timestamp=null,systemHashcode=1289092090]

Give me advice to get the required data.

Best Regards

What information exactly would you like to grab from the call on realtime , verify which AMI event fits for you needs

https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+AMI+Events

and Related to you code I cant argument nothing as I dont know Java programming, I have done similar task using PHP.

Hi @ambiorixg12,

I am looking to get
src,dst, channel, trunk, billsec.

You can see it’s similar to some CDR Columns.

Current code give me tow lines contain real caller as src in first row, and as dst in second row

org.asteriskjava.manager.event.HangupEvent[dateReceived=‘Wed Apr 19 12:51:32 GST 2017’,privilege=‘call,all’,server=null,calleridname=‘Ahmad Osman’,channel=‘SIP/111-00000012’,cause=‘16’,connectedlinename=‘Ahmad Albat’,sequencenumber=null,calleridnum=‘111’,causetxt=‘Normal Clearing’,callerid=‘111’,connectedlinenum=‘225’,uniqueid=‘1492591874.18’,timestamp=null,systemHashcode=906972885]

org.asteriskjava.manager.event.HangupEvent[dateReceived=‘Wed Apr 19 12:51:32 GST 2017’,privilege=‘call,all’,server=null,calleridname=‘Ahmad Albat’,channel=‘SIP/225-00000011’,cause=‘16’,connectedlinename=‘Ahmad Osman’,sequencenumber=null,calleridnum=‘225’,causetxt=‘Normal Clearing’,callerid=‘225’,connectedlinenum=‘111’,uniqueid=‘1492591874.17’,timestamp=null,systemHashcode=1289092090]

Just a suggestion, but you don’t have to write anything for the CDRs to be stored in a database as soon as the call ends. You could just set it up using Realtime and ODBC. Create the table and modify the Asterisk config files to write to the database rather than the log file. Like ambiorixg12, I also have AMI programs in PHP that monitor calls during execution, but I just let the system write my CDRs for me.

1 Like