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();
    }
}


你可能感兴趣的:(脚本)