Search code examples
javaasteriskfreepbx

Originate call to sip trunk via asterisk manager api java


So I am a total newbie in asterisk and managing call lines in general but I managed to install Asterisk Now 13 distro, I have connected 2 sip phones with pjsip and configured a sip trunk which works when I dial an external number with the corresponding prefix. Now I have to programmaticly originate calls and connect them to local extensions which I have no idea how to achieve and I cant seem to find much information about it on the internet after hours of searching.

I managed to connect 2 local sip phones with the asterisk manager api and OriginateAction in the following way:

    originateAction = new OriginateAction();
            originateAction.setChannel(ConnectionType+"/"+extCaller);
            originateAction.setContext(context);
            originateAction.setCallerId(idCaller);
            originateAction.setExten(tDestination);
            originateAction.setPriority(priority);
            originateAction.setTimeout(timeoutCall);
 managerConnection.login();

        originateResponse = managerConnection.sendAction(originateAction, timeoutRequest);

I also tried this channel originate pjsip/201 extension number@from-ptsn and channel originate local/201@from-local extension number@trunkName .

The context of the PJSIP trunk is from-pstn,I tried using that in various ways without luck both in asterisk cli and the application.

How do I make it use the PJSIP trunk when originating the call and make a call out of the office?

EDIT: I originated an outgoing call using a number that completes with the trunk outgoing route requisites and the "from-internal" context like this:

channel originate Local/201@from-internal extension (prefix)numberToCall@from-internal

I still do not understand why this works and if it is the correct answer to my question.


Solution

  • So the answer is in the edit of the question. The only way to generate an outgoing call that I could find is to originate that call "internaly" (with the context "from-internal" which happens to be the same context that is used when originating internal calls) introducing a target number value that completes with the sip trunk's route pattern requirements.

    Example: I have a route configured for the sip trunk( trunk1 ) with a pattern(RegEx): [0]{1}/number/ that means that with a 0 infront of any nubmer it will be a valid value for that route and it will try to call using trunk1.

    In the case of AsteriskNow CentOS installation it happens to be with the context "from-internal". Since the asterisk configuration files are owned by the FreePBX it is recomended to use the FreePBX GUI instead of configuring the .conf files of asterisk manualy.

    That concludes to :

    channel originate Local/201@from-internal extension (0)[numberToCall]@from-internal
    

    Which will make the extension 201 ring first and when picked up it will try to use the sip trunk to dial that [numberToCall] because the route with the 0 is "called".

    In order to send that command to asterisk using asterisk-java I wrote the following code:

     ManagerConnectionFactory factory = new 
     ManagerConnectionFactory("serverIp", "username",
                        "passwd");
     ManagerConnection managerConnection=factory.createManagerConnection()
        OriginateAction originateAction=new OriginateAction();
    
         final String randomUUID=java.util.UUID.randomUUID().toString();
    
                System.out.println("ID random:_"+randomUUID);
    
                originateAction.setChannel([connectionType]+"/"+[callerExtension]);<-- SIP or PJSIP / 201(the phone that will ring first)
                originateAction.setContext("from-internal"); <-- Default FreePBX context
                originateAction.setCallerId([callerId]); // what will be showed on the phone screen (in most cases your phone)
                originateAction.setExten([targetExten]); //where to call.. the target extension... internal extension or the outgoing number.. the 0[nomberToCall] 
                originateAction.setPriority([priority]);// priority of the call
                originateAction.setTimeout(timeoutCall); // the time that a pickup event will be waited for
                originateAction.setVariable("UUID", randomUUID); // asigning a unique ID in order to be able to hangup the call.