asterisk-1.6异步脚本

修改etc/asterisk/manager.conf

read = agi,system,call,log,verbose,agent,user,config,dtmf,reporting,cdr,dialplan

write = agi,system,call,agent,user,config,command,reporting,originate

修改

etc/asterisk/extensions.conf

[test]
exten =>_2.,1,AGI(agi:async)

脚本服务程序

import java.io.IOException; import org.asteriskjava.manager.AuthenticationFailedException; import org.asteriskjava.manager.ManagerConnection; import org.asteriskjava.manager.ManagerConnectionFactory; import org.asteriskjava.manager.ManagerEventListener; import org.asteriskjava.manager.TimeoutException; import org.asteriskjava.manager.action.*; import org.asteriskjava.manager.event.*; import org.asteriskjava.manager.response.ManagerResponse; public class HelloEvents implements ManagerEventListener { private ManagerConnection managerConnection; private ManagerConnection managerConnection1; public HelloEvents() throws IOException { ManagerConnectionFactory factory = new ManagerConnectionFactory( "ip", "uid", "pwd"); this.managerConnection = factory.createManagerConnection(); this.managerConnection1 = factory.createManagerConnection(); } public void run() throws IOException, AuthenticationFailedException, TimeoutException, InterruptedException { // register for events managerConnection.addEventListener(this); // connect to Asterisk and log in managerConnection.login(); managerConnection1.login(); // request channel state //managerConnection.sendAction(new StatusAction()); // wait 10 seconds for events to come in while(true){ Thread.sleep(10000); } // and finally log off and disconnect // managerConnection.logoff(); } int actionid=0; public void onManagerEvent(ManagerEvent event) { actionid++; // just print received events System.out.println("=============================="); System.out.println(event.getClass().getName()); if (event.getClass().equals(DtmfEvent.class)) { DtmfEvent dtmfEvent=(DtmfEvent)event; if (dtmfEvent.isEnd()) System.out.println(dtmfEvent); }else if (event.getClass().equals(PeerStatusEvent.class)) { System.out.println((PeerStatusEvent)event); }else if (event.getClass().equals(AgiExecEvent.class)) { System.out.println((AgiExecEvent)event); }else if (event.getClass().equals(AsyncAgiEvent.class)){ AsyncAgiEvent agiEvent=(AsyncAgiEvent)event; System.out.println(event); try { AgiAction agiAction=new AgiAction(); agiAction.setActionId("MyAction_"+actionid); agiAction.setCommandId("MyCommandId_"+actionid); agiAction.setChannel(agiEvent.getChannel()); if (agiEvent.getSubEvent().equals("Start")){ System.out.println("==============ANSWER==============="); agiAction.setCommand("ANSWER"); ManagerResponse response=managerConnection1.sendAction(agiAction); System.out.println(response); }else{ if (!agiEvent.getSubEvent().equals("End")){ //agiAction.setCommand("SAY NUMBER 1234 \"1*#\""); //agiAction.setCommand("STREAM FILE hello-world \"1*#\""); agiAction.setCommand("EXEC AGI \"agi://218.249.75.164/callin.agi\""); ManagerResponse response=managerConnection1.sendAction(agiAction); System.out.println(response); } } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 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); } public static void main(String[] args) throws Exception { HelloEvents helloEvents; helloEvents = new HelloEvents(); helloEvents.run(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值