Asterisk-1.6 asynchronous script

Source: Internet
Author: User

Modify 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

Modify

ETC/asterisk/extensions. conf

[Test]
Exten => _ 2., 1, Agi (AGI: async)

Script Service Program

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 blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TimeoutException e) {// TODO Auto-generated catch blocke.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();    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.