-
Notifications
You must be signed in to change notification settings - Fork 300
Examples
Brett Sutton edited this page Nov 7, 2017
·
2 revisions
The following shows how to do a synchronous dial and an async dial.
The reality is that you should ALWAYS use the async unless the dial is occurring in a background thread.
package org.asteriskjava.examples.activities;
import org.asteriskjava.pbx.ActivityCallback;
import org.asteriskjava.pbx.ActivityStatusEnum;
import org.asteriskjava.pbx.Call;
import org.asteriskjava.pbx.CallerID;
import org.asteriskjava.pbx.EndPoint;
import org.asteriskjava.pbx.PBX;
import org.asteriskjava.pbx.PBXException;
import org.asteriskjava.pbx.PBXFactory;
import org.asteriskjava.pbx.TechType;
import org.asteriskjava.pbx.Trunk;
import org.asteriskjava.pbx.activities.DialActivity;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;
public class Dial
{
static private Log logger = LogFactory.getLog(Dial.class);
static public void main(String[] args)
{
syncDial();
asyncDial();
}
/**
* Simple synchronous dial. The dial method won't return until the dial
* starts. Using this method will lockup your UI until the dial starts. For
* better control use the async Dial method below.
*/
static private void syncDial()
{
try
{
PBX pbx = PBXFactory.getActivePBX();
// The trunk MUST match the section header (e.g. [default]) that
// appears
// in your /etc/asterisk/sip.d file (assuming you are using a SIP
// trunk).
// The trunk is used to select which SIP trunk to dial through.
Trunk trunk = pbx.buildTrunk("default");
// We are going to dial from extension 100
EndPoint from = pbx.buildEndPoint(TechType.SIP, "100");
// The caller ID to show on extension 100.
CallerID fromCallerID = pbx.buildCallerID("100", "My Phone");
// The caller ID to display on the called parties phone
CallerID toCallerID = pbx.buildCallerID("83208100", "Asterisk Java is calling");
// The party we are going to call.
EndPoint to = pbx.buildEndPoint(TechType.SIP, trunk, "5551234");
// Trunk is currently ignored so set to null
// The call is dialed and only returns when the call comes up (it
// doesn't wait for the remote end to answer).
DialActivity dial = pbx.dial(from, fromCallerID, to, toCallerID);
Call call = dial.getNewCall();
Thread.sleep(20000);
logger.warn("Hanging up");
pbx.hangup(call);
}
catch (PBXException | InterruptedException e)
{
System.out.println(e);
}
}
static private void asyncDial()
{
PBX pbx = PBXFactory.getActivePBX();
// We are going to dial from extension 100
EndPoint from = pbx.buildEndPoint(TechType.SIP, "100");
// The caller ID to show on extension 100.
CallerID fromCallerID = pbx.buildCallerID("100", "My Phone");
// The caller ID to display on the called parties phone
CallerID toCallerID = pbx.buildCallerID("83208100", "Asterisk Java is calling");
// The party we are going to call.
EndPoint to = pbx.buildEndPoint(TechType.SIP, pbx.buildTrunk("default"), "5551234");
// Start the dial and return immediately.
// progress is provided via the ActivityCallback.
pbx.dial(from, fromCallerID, to, toCallerID, new ActivityCallback<DialActivity>()
{
@Override
public void progress(DialActivity activity, ActivityStatusEnum status, String message)
{
if (status == ActivityStatusEnum.SUCCESS)
{
System.out.println("Dial all good");
try
{
// Call is up
Call call = activity.getNewCall();
// So lets just hangup the call
logger.warn("Hanging up");
PBXFactory.getActivePBX().hangup(call.getOriginatingParty());
}
catch (PBXException e)
{
System.out.println(e);
}
}
if (status == ActivityStatusEnum.FAILURE)
System.out.println("Oops something bad happened when we dialed.");
}
});
}
}
package org.asteriskjava.examples.activities;
import org.asteriskjava.pbx.ActivityCallback;
import org.asteriskjava.pbx.ActivityStatusEnum;
import org.asteriskjava.pbx.Call;
import org.asteriskjava.pbx.CallerID;
import org.asteriskjava.pbx.Channel;
import org.asteriskjava.pbx.EndPoint;
import org.asteriskjava.pbx.PBX;
import org.asteriskjava.pbx.PBXFactory;
import org.asteriskjava.pbx.TechType;
import org.asteriskjava.pbx.activities.DialActivity;
import org.asteriskjava.pbx.activities.HoldActivity;
/**
* dial somebody and then put them on hold.
*
* @author bsutton
*/
public class Hold
{
static public void main(String[] args)
{
hold();
}
static private void hold()
{
PBX pbx = PBXFactory.getActivePBX();
// We are going to dial from extension 100
EndPoint from = pbx.buildEndPoint(TechType.SIP, "100");
// The caller ID to show on extension 100.
CallerID fromCallerID = pbx.buildCallerID("100", "My Phone");
// The caller ID to display on the called parties phone
CallerID toCallerID = pbx.buildCallerID("83208100", "Asterisk Java is calling");
// The party we are going to call.
EndPoint to = pbx.buildEndPoint("SIP/default/5551234");
// Start the dial and return immediately.
// progress is provided via the ActivityCallback.
pbx.dial(from, fromCallerID, to, toCallerID, new ActivityCallback<DialActivity>()
{
@Override
public void progress(DialActivity activity, ActivityStatusEnum status, String message)
{
if (status == ActivityStatusEnum.SUCCESS)
{
System.out.println("Dial all good so lets place them on hold");
PBX pbx = PBXFactory.getActivePBX();
// Call is up
Call call = activity.getNewCall();
// Place the remote party on hold.
HoldActivity hold = pbx.hold(call.getRemoteParty());
Channel heldChannel = hold.getChannel();
System.out.println("Held channel is " + heldChannel);
}
if (status == ActivityStatusEnum.FAILURE)
System.out.println("Oops something bad happened when we dialed.");
}
});
}
}
package org.asteriskjava.examples.activities;
import java.io.IOException;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.pbx.ActivityCallback;
import org.asteriskjava.pbx.ActivityStatusEnum;
import org.asteriskjava.pbx.Call;
import org.asteriskjava.pbx.Call.OperandChannel;
import org.asteriskjava.pbx.CallerID;
import org.asteriskjava.pbx.EndPoint;
import org.asteriskjava.pbx.PBX;
import org.asteriskjava.pbx.PBXFactory;
import org.asteriskjava.pbx.TechType;
import org.asteriskjava.pbx.Trunk;
import org.asteriskjava.pbx.activities.BlindTransferActivity;
import org.asteriskjava.pbx.activities.DialActivity;
import org.asteriskjava.pbx.internal.core.AsteriskPBX;
/**
* dial somebody and then blind transfer the call to a third party.
*
* @author bsutton
*/
public class BlindTransfer
{
static public void main(String[] args) throws IOException, AuthenticationFailedException, TimeoutException
{
/**
* Initialise the PBX Factory. You need to implement your own AsteriskSettings class.
*/
PBXFactory.init(new ExamplesAsteriskSettings());
/**
* Activities utilise an agi entry point in your dial plan.
* You can create your own entry point in dialplan or have
* asterisk-java add it automatically
*/
AsteriskPBX asteriskPbx = (AsteriskPBX) PBXFactory.getActivePBX();
asteriskPbx.createAgiEntryPoint();
// We are all configured lets try and do a blind transfer.
blindTransfer();
}
static private void blindTransfer()
{
PBX pbx = PBXFactory.getActivePBX();
// The trunk MUST match the section header (e.g. [default]) that appears
// in your /etc/asterisk/sip.d file (assuming you are using a SIP trunk).
// The trunk is used to select which SIP trunk to dial through.
Trunk trunk = pbx.buildTrunk("default");
// We are going to dial from extension 100
EndPoint from = pbx.buildEndPoint(TechType.SIP, "100");
// The caller ID to show on extension 100.
CallerID fromCallerID = pbx.buildCallerID("100", "My Phone");
// The caller ID to display on the called parties phone
CallerID toCallerID = pbx.buildCallerID("83208100", "Asterisk Java is calling");
// The party we are going to call.
EndPoint to = pbx.buildEndPoint(TechType.SIP, trunk, "5551234");
// Start the dial and return immediately.
// progress is provided via the ActivityCallback.
pbx.dial(from, fromCallerID, to, toCallerID, new ActivityCallback<DialActivity>()
{
@Override
public void progress(DialActivity activity, ActivityStatusEnum status, String message)
{
if (status == ActivityStatusEnum.SUCCESS)
{
System.out.println("Dial all good so lets do a blind transfer");
PBX pbx = PBXFactory.getActivePBX();
// Call is up
Call call = activity.getNewCall();
CallerID toCallerID = pbx.buildCallerID("101", "I'm calling you");
EndPoint transferTarget = pbx.buildEndPoint(TechType.SIP, "101");
pbx.blindTransfer(call, OperandChannel.REMOTE_PARTY, transferTarget, toCallerID, false, 30L,
new ActivityCallback<BlindTransferActivity>()
{
@Override
public void progress(BlindTransferActivity activity, ActivityStatusEnum status, String message)
{
// if success the blind transfer completed.
}
});
}
if (status == ActivityStatusEnum.FAILURE)
System.out.println("Oops something bad happened when we dialed.");
}
});
}
}