Upgrade 3CX to v18 and get it hosted free!

MANAGERAPI-JAVA-AGI

Author image

// file name HelloEvents.java full flashed demo of Asterisk-JAVA package APIs
// by imran ( Pakistan ) you can use this to handle every thing in java that asterisk supports.
// it is a complex script but gives u a clear vision how you will use java api for any task.
// Please feed back >>>>>>>>> imran_accout@yahoo.com

import java.io.IOException;
import net.sf.asterisk.manager.AuthenticationFailedException;
import net.sf.asterisk.manager.ManagerConnection;
import net.sf.asterisk.manager.ManagerConnectionFactory;
import net.sf.asterisk.manager.ManagerEventHandler;
import net.sf.asterisk.manager.TimeoutException;
import net.sf.asterisk.manager.action.StatusAction;
import net.sf.asterisk.manager.event.ManagerEvent;
import net.sf.asterisk.manager.event.*;
import net.sf.asterisk.manager.action.OriginateAction;
import net.sf.asterisk.manager.response.ManagerResponse;

import net.sf.asterisk.fastagi.command.*;

public class HelloEvents implements ManagerEventHandler
{
private ManagerConnection managerConnection;

public HelloEvents() throws IOException
{
ManagerConnectionFactory factory = new ManagerConnectionFactory();

this.managerConnection = factory.getManagerConnection(“localhost”, “manager”, “pa55w0rd”);
}

public void run() throws IOException, AuthenticationFailedException,
TimeoutException, InterruptedException
{
// register for events
managerConnection.addEventHandler(this);
OriginateAction originateAction;
ManagerResponse originateResponse;
originateAction = new OriginateAction();

ChannelStatusCommand OBJ_CHANNELSTATUS = new ChannelStatusCommand();

String channelstatus = OBJ_CHANNELSTATUS.getChannel();

AnswerCommand OBJ_ANSWER = new AnswerCommand();

String DialStr =”SIP/100|40|tr”;
ExecCommand OBJ_EXEC = new ExecCommand(“DIAL”,DialStr);

GetVariableCommand OBJ_DIALSTATUS= new GetVariableCommand(“DIALSTATUS”);

String dialstatus = OBJ_DIALSTATUS.getVariable();

if(dialstatus.equals(“ANSWER”))
{
}

HangupCommand OBJ_HANGUP = new HangupCommand();

originateAction.setChannel(“SIP/John”);
originateAction.setContext(“default”);
originateAction.setExten(“1300”);
originateAction.setPriority(new Integer(1));
originateAction.setTimeout(new Integer(30000));

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

// send the originate action and wait for a maximum of 30 seconds for Asterisk
// to send a reply
originateResponse = managerConnection.sendAction(originateAction, 30000);

// print out whether the originate succeeded or not
System.out.println(originateResponse.getResponse());

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

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

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

public void handleEvent(ManagerEvent event)
{
//DialEvent DialObject = (DialEvent)event;
//ChannelEvent ChannelObject = (ChannelEvent)event;
DialEvent DialObject = null;
ChannelEvent ChannelObject = null;

Object obj = event.getSource();
// just print received events
if( obj == DialObject/*DialEvent*/ )
{
System.out.println(“

DialEvent

i’m excuted”);
DialObject = (DialEvent)event;

DialObject.getCallerId();
DialObject.getCallerIdName();
DialObject.getDestination();
DialObject.getDestUniqueId();
DialObject.getSrc();
DialObject.getSrcUniqueId();
}
else if( obj == ChannelObject/*ChannelEvent*/ )
{
System.out.println(“

(ChannelEvent)

i’m excuted”);
ChannelObject = (ChannelEvent)event;
ChannelObject.getCallerId();
ChannelObject.getCallerIdName();
ChannelObject.getChannel();
ChannelObject.getState();
}

System.out.println(event);
}

public static void main(String[] args) throws Exception
{
HelloEvents helloEvents;
helloEvents = new HelloEvents();
helloEvents.run();
}
}


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.
Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.