/** * 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); } }
/** 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); } }