Message delivery to a dahdi line using the Manager interface with asterisk-java

You want to deliver a message to a pots line, and you don’t trust callprogress in asterisk, so you implement a dialplan
that waits for a digit, then plays a message.
The dialplan, in /etc/asterisk/extensions_custom.conf is
[mimensaje]
exten => s,1(lbl_mensaje_0),Playback(nuevo_mensaje,noanswer)
exten => s,n,WaitForSilence(10,6,10)
exten => s,n,GotoIf($[“${WAITSTATUS}” = “TIMEOUT”]?lbl_mensaje_0:)
exten => s,n,Read(CONTEXT,nuevo_mensaje,1,ni,6,3)
exten => s,n,GotoIf($[“${READSTATUS}” != “OK”]?lbl_mensaje_1:)
exten => s,n,Playback(${elmensaje},noanswer)
exten => s,n,GotoIf($[“${PLAYBACKSTATUS}” = “SUCCESS”]?:lbl_mensaje_2)
exten => s,n(lbl_mensaje_2),Hangup()
exten => s,n(lbl_mensaje_1),Playback(self-destruct,noanswer)
exten => s,n,Goto(lbl_mensaje_2)
The java class that sends the message using that dialplan is:
import java.io.IOException;
import org.asteriskjava.manager.*;
import org.asteriskjava.manager.event.*;
import org.asteriskjava.manager.action. *;
import org.asteriskjava.manager.response.ManagerResponse;
public class HelloEvents1 implements ManagerEventListener
{
private ManagerConnection managerConnection;
String number=””;
String message=””;
int i=0;
public HelloEvents1() throws IOException
{
ManagerConnectionFactory factory = new
ManagerConnectionFactory(“localhost”,”adm”,”password”);
this.managerConnection = factory.createManagerConnection();
}
public OriginateAction setupOriginate(String n,String m) {
OriginateAction originateAction = new OriginateAction();
11/433
originateAction. setChannel(“DAHDI/1/”+n);
originateAction. setVariable(“elmensaje”, m);
originateAction. setContext(“mimensaje”);
originateAction. setExten(“10”);
originateAction. setActionId(n);
originateAction. setAsync(Boolean.TRUE);
originateAction. setTimeout(new Integer(30000));
return originateAction;
}
public void start() throws IOException, AuthenticationFailedException,
TimeoutException, InterruptedException
{
// register for events
managerConnection.addEventListener(this);
// connect to Asterisk and log in
managerConnection.login();
OriginateAction originateAction;
ManagerResponse originateResponse;
originateAction = new OriginateAction();
originateAction=setupOriginate(number,message);
originateResponse = managerConnection.sendAction(originateAction, 30000);
Thread.sleep(20000);
managerConnection.sendAction(new StatusAction()) ;
Thread.sleep(80000);
managerConnection.logoff();
}
public void onManagerEvent(ManagerEvent event)
{
// just print received events
// System.out.println(“——–“+event);
String e=””+event;
if (e.indexOf(“appdata='”+message+”,noanswer'”)>0){
System.out.println(“ISSY: Mensaje recibido<br>”);
System.exit(0);
}
if (e.indexOf(“CONTEXT”)>0||e.indexOf(“nuevo_mensaje”)>0){
i++;
System.out.println(“ISSY: Esperando a que digite 1 : “+i+” de 8 intentos<br>”);
}
if (i==8){
System.out.println(“ISSY: Mensaje no enviado, usuario no digito 1<br>”);
System.exit(1);
}
if (e.indexOf(“HANGUP”)>0){
System.out.println(“ISSY: Linea Colgada, mensaje no enviado<br>”) ;
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
if (args.length<2){
System.out.println(“Usage: Call <number> <message>”);
System.exit(1);
}
12/433
HelloEvents1 hello = new HelloEvents1();
hello.number=args[0];
hello.message=args[1];
hello.start();
}
}
Enjoy!
13/433

Leave a Reply

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