public static void main(String[] args) throws Exception { ManagerConnectionFactory factory = new ManagerConnectionFactory("172.168.10.205", "manager", "P@$$w0rd"); ManagerConnection managerConnection = factory.createManagerConnection(); managerConnection.login(); verifyMeetMeRoom("6300", "6000", managerConnection); managerConnection.logoff(); }
/** * @param actionID * @param channel * @return */ public boolean kHangUp(String actionID, String channel) { boolean back = false; String cause = "16"; String callParam = ("Action: kHangup" + CRLF + "ActionID: " + actionID + CRLF + "Channel: " + channel + CRLF + "Cause: " + cause + CRLF + CRLF); this.changeInfo(callParam); HangupAction hangupAct = new HangupAction(); hangupAct.setActionId(actionID); hangupAct.setChannel(channel); try { managerConnection.sendAction(hangupAct, sendCB); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } return back; }
// register a conference room for user. return true if success. public static boolean verifyMeetMeRoom( String roomNumber, String witness, ManagerConnection managerConnection) throws Exception { boolean validRoom = false; OriginateAction originateAction; ManagerResponse originateResponse; // le thanh hai System.out.println(""); originateAction = new OriginateAction(); originateAction.setChannel("SIP/" + witness); originateAction.setContext("conferences"); originateAction.setExten(roomNumber); originateAction.setPriority(new Integer(1)); originateAction.setTimeout(new Long(30000)); // //send the originate action and wait for a maximum of 30 seconds for // Asterisk // to send a reply originateResponse = managerConnection.sendAction(originateAction, 30000); // print out whether the originate succeeded or not System.out.println(originateResponse.getResponse()); if (originateResponse.getResponse().equals("Success")) validRoom = true; return validRoom; }
public boolean kSendSMS() { String channel = conteudo.getChannel(); String text = conteudo.getSmsText(); String destination = conteudo.getNumber(); String context = conteudo.getContext(); String tech = conteudo.getTech(); boolean back = false; String smsParam; boolean confirmation = conteudo.getConfirmation(); KSendSMSAction action; action = new KSendSMSAction(); if (!destination.equals("") && !channel.equals("") && !text.equals("")) { action.setDevice(channel); action.setDestination(destination); action.setMessage(text); action.setConfirmation(confirmation); smsParam = ("Action: " + action.getAction() + CRLF + "device: " + action.getDevice() + CRLF + "destination: " + action.getDestination() + CRLF + "Message: " + action.getMessage() + CRLF + "Confirmation: " + action.getConfirmation() + CRLF + CRLF); if (ManagerConnectionState.CONNECTED == this.managerConnection.getState()) { try { managerConnection.sendAction(action, sendCB); back = true; System.out.println("Sending SMS to " + destination); this.changeInfo(smsParam); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } } } else { System.out.println("Existem valores em branco!"); return back; } return back; }
/** * click call function example: call(809, 011862062881234) 809 is my extension number, * 011862062881234 is the number that I want to dial. this function will first call your * extension(parameter callFrom), then will call the target phone number(parameter callTo) after * you pickup the telephone. * * @param callFrom the Asterisk extension from * @param callTo the phone number to call */ public void call(String callFrom, String callTo) { OriginateAction originateAction; originateAction = new OriginateAction(); originateAction.setChannel(originationChannel + callFrom + originationChannelSuffix); originateAction.setContext(originationContext); originateAction.setExten(asteriskExternalCode + callTo); originateAction.setCallerId(callFrom); originateAction.setPriority(new Integer(1)); originateAction.setTimeout(new Long(TIMEOUT)); // 30 seconds timeout Debug.logInfo( "Calling from: " + originateAction.getChannel() + ", to: " + originateAction.getExten() + ", asterisk connection state : " + managerConnection.getState(), MODULE); // send the originate action and wait for a maximum of 30 seconds for Asterisk to send a reply try { // confirm the manager connection still connected, else try to reconnect if (managerConnection.getState() != ManagerConnectionState.CONNECTED) { managerConnection.login(); } managerConnection.sendAction(originateAction, TIMEOUT); } catch (IllegalArgumentException e) { Debug.logError(e, "Error outbound calling", MODULE); } catch (IllegalStateException e) { Debug.logError(e, "Error outbound calling", MODULE); } catch (IOException e) { Debug.logError(e, "Error outbound calling", MODULE); } catch (TimeoutException e) { Debug.logError(e, "Error outbound calling", MODULE); } catch (AuthenticationFailedException e) { Debug.logError(e, "Error outbound calling", MODULE); } }
public void kSendUuiMessage() { String device = conteudo.getUuiDevice(); String protocol = conteudo.getUuiProtocol(); String length = conteudo.getUuiLength(); String data = conteudo.getUuiData(); String uuiParam; KSendUUIAction action; action = new KSendUUIAction(); if (!device.equals("") && !data.equals("") && !protocol.equals("") && !length.equals("")) { action.setData(data); action.setDevice(device); action.setLength(length); action.setProtocol(protocol); uuiParam = ("Dial(Khomp/" + action.getDevice() + "/" + action.getProtocol() + "/" + action.getLength() + "/" + action.getData() + ")"); if (ManagerConnectionState.CONNECTED == this.managerConnection.getState()) { try { managerConnection.sendAction(action, sendCB); System.out.println("Sending UUI message to " + device); this.changeInfo(uuiParam); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } } } else { System.out.println("Existem valores em branco!"); } }
public void kSendPresentation() { String channel = conteudo.getChannel(); String presentation = conteudo.getPresentation(); String presentationParam; KGSMPresentationAction action; action = new KGSMPresentationAction(); if (!channel.equals("") && !presentation.equals("")) { action.setChannel(channel); action.setPresentation(presentation); presentationParam = ("Action: " + action.getAction() + CRLF + "channel: " + action.getChannel() + CRLF + "presentation: " + action.getPresentation() + CRLF + CRLF); if (ManagerConnectionState.CONNECTED == this.managerConnection.getState()) { try { managerConnection.sendAction(action, sendCB); System.out.println("Sending Presentation: " + presentation + " to channel: " + channel); this.changeInfo(presentationParam); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } } else { JOptionPane.showMessageDialog(telaPrincipal, "Necessário conectar primeiro!"); } } else { System.out.println("Existem valores em branco!"); } }
public void kSendUssdMessage() { String device = conteudo.getUSSDDevice(); String message = conteudo.getUSSDMessage(); String ussdParam; KSendUSSDAction action; action = new KSendUSSDAction(); if (!device.equals("") && !message.equals("")) { action.setDevice(device); action.setMessage(message); ussdParam = ("Action: " + action.getAction() + CRLF + "device: " + action.getDevice() + CRLF + "message: " + action.getMessage() + CRLF + CRLF); if (ManagerConnectionState.CONNECTED == this.managerConnection.getState()) { try { managerConnection.sendAction(action, sendCB); System.out.println("Sending USSD message to " + device); this.changeInfo(ussdParam); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } } } else { System.out.println("Existem valores em branco!"); } }
public void kSendSimCardSelection() { String channel = conteudo.getSimCardChannel(); int simCard = conteudo.getSimCardSelected(); String selectionParam; KSelectSIMcardAction action; action = new KSelectSIMcardAction(); if (!channel.equals("") && simCard > -1) { action.setChannel(channel); action.setSIMCard(simCard); selectionParam = ("Action: " + action.getAction() + CRLF + "channel: " + action.getChannel() + CRLF + "simcard: " + action.getSIMCard() + CRLF + CRLF); if (ManagerConnectionState.CONNECTED == this.managerConnection.getState()) { try { managerConnection.sendAction(action, sendCB); System.out.println("Changind SIM card to: SIMcard: " + simCard); this.changeInfo(selectionParam); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } } } else { System.out.println("Existem valores em branco!"); } }
/** Reloads asterisk config properties. */ public void reload() { // retrieve asterisk manager configuration here Properties configProperties = UtilProperties.getProperties("asterisk.properties"); // asterisk host address String host = (String) configProperties.get("asterisk.host"); // asterisk manager username String username = (String) configProperties.get("asterisk.username"); // asterisk manager password String password = (String) configProperties.get("asterisk.password"); // asterisk dial out prefix, calls between internal extensions are made by dialing the extension // number // whereas external calls need a prefix before the number you want dial, such as 9 asteriskExternalCode = (String) configProperties.get("asterisk.outbound.prev"); // the country code of asterisk server, can be specified to avoid dialing national numbers using // the country code asteriskCountryCode = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.countryCode", ""); // the area code of asterisk server, can be specified to avoid dialing local area numbers using // the area code asteriskAreaCode = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.areaCode", ""); // the current phone number of the asterisk server (the phone number of the phone line connected // to the asterisk server) asteriskPhoneNumber = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.phoneNumber", ""); // the prefix used to dial international phone numbers, usually "011" or "00" ... depends on the // origin country asteriskInternationalPrefix = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.outbound.foreign", ""); // the prefix used to dial numbers out of the local area, for example to dial other cities / // states asteriskAreaPrefix = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.outbound.area", ""); // if always add area code to dial, even though it was the same area code. alwaysDialAreaCode = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.alwaysDialAreaCode", "Y"); // if always add country code to dial, even though it was the same country code, it used in some // voip provider. alwaysDialCountryCode = UtilProperties.getPropertyValue( "asterisk.properties", "asterisk.alwaysDialCountryCode", "Y"); // Dialplan context to use for outgoing calls, i.e. the dialed number originationContext = UtilProperties.getPropertyValue( "asterisk.properties", "asterisk.originationContext", "from-internal"); // Channel Technology to use for outgoing calls for the user's channel i.e. SIP/ for chan_sip or // Local/ for chan_local originationChannel = UtilProperties.getPropertyValue( "asterisk.properties", "asterisk.originationChannel", "Local/"); // Channel Technology suffix (useful for ChanLocal - i.e. Local/825@from-internal originationChannelSuffix = UtilProperties.getPropertyValue( "asterisk.properties", "asterisk.originationChannelSuffix", ""); // Area codes that don't need the long-distance prefix String localAreaCodesString = UtilProperties.getPropertyValue("asterisk.properties", "asterisk.localAreaCodes", ""); localAreaCodes = UtilMisc.toListArray(localAreaCodesString.split(",")); // logs out an existing connection if (managerConnection != null) { if (managerConnection.getState().equals(ManagerConnectionState.CONNECTED)) { managerConnection.logoff(); } } ManagerConnectionFactory factory = new ManagerConnectionFactory(host, username, password); this.managerConnection = factory.createManagerConnection(); // connect to Asterisk and log in try { managerConnection.login(); managerConnection.addEventListener( new ManagerEventListener() { public void onManagerEvent(ManagerEvent event) { // add a listener to handle incoming and outgoing calls // e.getCallerId() is the calling phone number. such as 8605758672106 // e.getDestination() is the destination of call, include asterisk communication, such // as SIP/825-09caf850 if (event instanceof DialEvent) { DialEvent e = (DialEvent) event; String destinationNumber = retrieveNumber(e.getDestination()); Debug.logInfo("Call from:" + e.getCallerId() + ", to:" + destinationNumber, MODULE); calls.put(destinationNumber, e.getCallerId()); } } }); managerConnection.sendAction(new StatusAction()); } catch (IllegalStateException e) { Debug.logError(e, "Error reloading asterisk server manager connection", MODULE); } catch (IOException e) { Debug.logError(e, "Error reloading asterisk server manager connection", MODULE); } catch (AuthenticationFailedException e) { Debug.logError(e, "Error reloading asterisk server manager connection", MODULE); } catch (TimeoutException e) { Debug.logError(e, "Error reloading asterisk server manager connection", MODULE); } }
public String sendAction(ManagerAction action) throws Exception { ManagerResponse response = managerConnection.sendAction(action, 2000); return response.getResponse(); }
public void removeManagerEventListener(ManagerEventListener listener) { managerConnection.removeEventListener(listener); }
public void addManagerEventListener(ManagerEventListener listener) { managerConnection.addEventListener(listener); }
public void disconnect() { managerConnection.logoff(); }
public void connect() throws Exception { managerConnection.login(); }
public int makeCallAplicacao() { String number = conteudo.getNumber(); String channel = conteudo.getChannel(); String context = conteudo.getContext(); String tech = conteudo.getTech(); int priority = 1; int timeOut = 30000; int back = 0; int actID = this.actionID + 1; this.actionID = actID; OriginateAction originateAction; // SendActionCallback sendCB = this; originateAction = new OriginateAction(); if (!number.equals("") && !channel.equals("") && !context.equals("")) { originateAction.setActionId(String.valueOf(actID)); if (tech.equalsIgnoreCase("Khomp")) { originateAction.setChannel(tech + "/" + channel + "/" + number); } else { originateAction.setChannel(tech + "/" + number); } originateAction.setContext(context); originateAction.setExten(number); originateAction.setPriority(priority); originateAction.setTimeout(timeOut); back = 1; } else { System.out.println("Existem valores em branco!"); return back; } if (ManagerConnectionState.CONNECTED == this.managerConnection.getState()) { System.out.println("Calling to " + "Channel:" + channel + ", Extension:" + number); try { managerConnection.sendAction(originateAction, sendCB); } catch (IOException | IllegalArgumentException | IllegalStateException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } } else { back = -1; } String callParam = ("Action: Originate" + CRLF + "ActionID: " + originateAction.getActionId() + CRLF + "Channel: " + originateAction.getChannel() + CRLF + "Context: " + originateAction.getContext() + CRLF + "Exten: " + originateAction.getExten() + CRLF + "Priority: " + originateAction.getPriority() + CRLF + CRLF); this.changeInfo(callParam); // cria e popula objeto call Call call = new Call(); call.setActionID(Integer.toString(actID)); call.setChannel(channel); call.setStatus("Connected"); // conteudo.setTabela(call); // telaPrincipal.mostraTabelaChannels(); return back; }