Make calls from Asterisk-Java

Hi,

Now I have a question: Is possible make a java aplication wich automaticaly make a call to a softpone??
I readed the tutorial of the API Asterisk-Java, and here don’t say how to do. Also I’m searching in google and I can’t find the answer…

Please, help!

Do a search for Asterisk AMI call origination

Thanks for the Reply.

I made the Java class HelloManager, but I execute this and the originateResponse.getResponse() returns error. Here I put the code:

import java.io.IOException;

import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.OriginateAction;
import org.asteriskjava.manager.response.ManagerResponse;

public class HelloManager
{
    private ManagerConnection managerConnection;

    public HelloManager() throws IOException
    {
        ManagerConnectionFactory factory = new ManagerConnectionFactory(
                "localhost", "manager", "jose");

        this.managerConnection = factory.createManagerConnection();
    }

    public void run() throws IOException, AuthenticationFailedException,
            TimeoutException
    {
        OriginateAction originateAction;
        ManagerResponse originateResponse;
        originateAction = new OriginateAction();
        originateAction.setChannel("SIP");
        originateAction.setContext("default");
        originateAction.setExten("3002");
        originateAction.setPriority(new Integer(1));
        originateAction.setTimeout(new Integer(30000));

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

        // print out whether the originate succeeded or not
        System.out.println("getResponse");
        System.out.println(originateResponse.getResponse());

        // and finally log off and disconnect
        managerConnection.logoff();
    }

    public static void main(String[] args) throws Exception
    {
        HelloManager helloManager;

        helloManager = new HelloManager();
        helloManager.run();
    }
}

What I want is make a call from this java aplication to a softphone in other computer in the same network, so I’m not sure if I must pass “localhost” to ManagerConnectionFactory. Neither sure if is correct this line:

originateAction.setChannel(“SIP”);

Is this esactly what I must put in setChannel??
Also I configured the manager.conf file of asterisk:

[general]
enabled = yes
port = 5038
bindaddr = 0.0.0.0

[manager]
secret=jose
permit=0.0.0.0/0.0.0.0
read=all
write=all

must I replace “0.0.0.0” in “bindaddr” and “permit” and put my IP with Asterisk installed?, the IP of the computer wich I want call? or I leave well?

Thanks.

Both are currently wildcards. The telephony party IP addresses are irrelevant.

so, what is wrong in my code :question: :confused:

The channel specification is incomplete. It needs to be compatible with the Dial application. Specifically you only have the technology, but you need a “/” followed by address information, at least a sip.conf section name or domain name, and probably also digits to dial.

Thank you! Problem solved!

Now I have a new problem… :frowning:

I modified the Java class for make a call to a softphone, and when answer, this make a call to another softphone. The code working correctly, but now I hired an IP telephony server for make calls to extern phones and mobiles. I have configured the files sip.conf and extensions.conf, and now is posible to call from my softphone to extern phones.
But what I really want is make a call from the Java class to an extern phone, and when this answer contact this with my softphone (as I did between softphones, but calling to an extern phone). This is the code I made, but this not work:

import java.io.IOException;

import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.OriginateAction;
import org.asteriskjava.manager.response.ManagerResponse;

public class HelloManager
{
    private ManagerConnection managerConnection;

    public HelloManager() throws IOException
    {
        ManagerConnectionFactory factory = new ManagerConnectionFactory(
                "sip2.adiptel.com", "manager", "jose");

        this.managerConnection = factory.createManagerConnection();
    }

    public void run() throws IOException, AuthenticationFailedException,
            TimeoutException
    {
        OriginateAction originateAction;
        ManagerResponse originateResponse;
        originateAction = new OriginateAction();
        originateAction.setChannel("SIP/****");
        //This is the extern phone number 
        originateAction.setContext("default");
        originateAction.setExten("*****");
        //This is the number wich gave me in the phone service
        originateAction.setPriority(new Integer(1));
        originateAction.setTimeout(new Integer(30000));

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

        // print out whether the originate succeeded or not
        System.out.println("getResponse");
        System.out.println(originateResponse.getResponse());

        // and finally log off and disconnect
        managerConnection.logoff();
    }

    public static void main(String[] args) throws Exception
    {
        HelloManager helloManager;

        helloManager = new HelloManager();
        helloManager.run();
    }
}

What is wrong?
Should I also configure manager.conf?

You didn’t provide the error messages!

However, if this comment is correct:

originateAction.setChannel("SIP/****"); //This is the extern phone number

you appear not to understand the structure of address part of SIP dialstrings.

The address part must contain either a sip.conf section name or a domain name/IP address, and may contain digits to dial. Unless you have sip.conf entries for every number you want to dial, or the comment is wrong, this is going to produce a channel unavailable response.

This is the Error:

Exception in thread “main” Java.net.ConnectException: Connection time out

And this is the configuration of my sip.conf:

;                 SIP.CONF
;=========================================
;
[general]
;
port=5060
disallow=all
allow=g726
allow=ulaw
allow=alaw
localnet=192.168.1.60/255.255.255.0
externip=sip2.adiptel.com
register => 531582867:449546180@sip2.adiptel.com/531582867
;
[ADIPTEL]
type=peer
fromuser=531582867
username=531582867
callerid=531582867
secret=449546180
context=default
realm=sip2.adiptel.com
fromdomain=sip2.adiptel.com
host=sip2.adiptel.com
port=5060
qualify=no
dtmfmode=auto
canreinvite=no
nat=yes
insecure=port,invite
;
[FIJO]
type=peer
fromuser=531582867
username=954789099
callerid=954789099
secret=449546180
context=default
realm=sip2.adiptel.com
fromdomain=sip2.adiptel.com
host=sip2.adiptel.com
port=5060
qualify=no
dtmfmode=auto
canreinvite=no
nat=yes
insecure=port,invite
;
[MOVIL]
type=peer
fromuser=531582867
username=619368982
callerid=619368982
secret=449546180
context=default
realm=sip2.adiptel.com
fromdomain=sip2.adiptel.com
host=sip2.adiptel.com
port=5060
qualify=no
dtmfmode=auto
canreinvite=no
nat=yes
insecure=port,invite
;
[3001]
type=peer
fromuser=531582867
username=3001
callerid=3001
secret=449546180
context=default
realm=sip2.adiptel.com
fromdomain=sip2.adiptel.com
host=sip2.adiptel.com
port=5060
qualify=no
dtmfmode=auto
canreinvite=no
nat=yes
insecure=port,invite
;
[3002]
type=peer
fromuser=531582867
username=3002
callerid=3002
secret=449546180
context=default
realm=sip2.adiptel.com
fromdomain=sip2.adiptel.com
host=sip2.adiptel.com
port=5060
qualify=no
dtmfmode=auto
canreinvite=no
nat=yes
insecure=port,invite
;

The number of the line which you refer is 954789099. And this is the configuration of the extensions.conf:

;             EXTENSIONS.CONF
;=========================================
;
[default]
;
exten => 531582867,1,Dial(SIP/531582867,40,t)  ; Para recibir sus llamadas entrantes en su extensión interna
; Tratamiento llamadas nacionales a fijos y móviles 
exten => 954789099,1,Dial(SIP/$EXTEN@ADIPTEL,60,t)
;
exten => 619368982,1,Dial(SIP/$EXTEN@ADIPTEL,60,t)
;
exten => s,1,Dial(SIP/954789099&SIP/531582867,60,t)
exten => 954789099,n,Hangup
exten => 531582867,n,Hangup
;
exten => 3001,1,Dial(SIP/3001,26)
exten => 3001,n,Hangup
;
exten => 3002,1,Dial(SIP/3002,26)
exten => 3002,n,Hangup
;

What is wrong with this code :question:

The error message indicates that the Java code didn’t get as far as establishing a TCP connection (typically a firewall set to drop).

There is no section [954789099] in your sip.conf.

Now the problem is solved, was caused by this line in the Java class:

         ManagerConnectionFactory factory = new ManagerConnectionFactory(
                "sip2.adiptel.com", "manager", "jose");

In stead of “sip2.adiptel.com” I had to put my IP :wink:

Thanks for the help!

did you make your java application on the same machine with asterisk server or separate?
I want make java application to make call in computer A and my asterisk server in computer B.
With switch and kabel utp to connect them both, but it isnt successful.
can you explain that? please help thank you

Hello,
I am having the same issue, even thought I took in consideration all your remarks, I still get the Error in originateResponse.getResponse().
The manager successfully logged to Asterisk, but nothing happens apart from the Error.
Here is my code :

Java (eclipse)

[code]package gomobile.com.telintel;

import java.io.IOException;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.OriginateAction;
import org.asteriskjava.manager.response.ManagerResponse;

public class HelloManager {

private ManagerConnection managerConnection;

public HelloManager() throws IOException {  
    ManagerConnectionFactory factory = new ManagerConnectionFactory("localhost", "manager", "pa55w0rd");

    this.managerConnection = factory.createManagerConnection();
}

public void call() throws IOException, AuthenticationFailedException, TimeoutException {
    OriginateAction originateAction;
    ManagerResponse originateResponse;

    originateAction = new OriginateAction();
    originateAction.setChannel("SIP/1000");
    originateAction.setContext("default");
    originateAction.setExten("1001");
    originateAction.setPriority(new Integer(1));
    originateAction.setTimeout(new Integer(30000));
    
    

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


    // print out whether the originate succeeded or not
    System.out.println(originateResponse.getResponse());
    
    
    
    // and finally log off and disconnect
    managerConnection.logoff();
}

public static void main(String[] args) throws Exception {
    HelloManager helloManager = new HelloManager();
    helloManager.call();
}

}[/code]

sip.conf

[general]
context=default
bindport=5060
disallow=all
allow=ulaw
allow=alaw

[1000]
type=friend
host=dynamic
secret=1234
context=default

[1001]
type=friend
host=dynamic
secret=1234
context=default

[1002]
type=friend
host=dynamic
secret=1234
context=default

extensions.conf

[general]
static=yes
writeprotect=no

[default]
exten => 1001,1,Answer()
exten => 1001,n,Dial(SIP/1001,20,tr)
exten => 1001,n,Hangup

exten => 1515,1,Verbose(2,Hello)


exten => 1002,1,Answer()
exten => 1002,n,Dial(SIP/1002,20,tr)
exten => 1002,n,Hangup

exten => 555,1,Answer()
exten => 555,n,Playback(tt-monkeys)
exten => 555,n,Hangup

exten => 666,1,Playback(thank-you-cooperation)
exten => 666,n,Hangup()

exten => 888,1,Agi(agi://localhost/hello.agi)

exten => 999,1,Agi(agi://localhost/hello2.agi)
exten => 999,n,Verbose(2,The channel name is ${MYVAR})
exten => 999,n,Playback(thank-you-cooperation)
exten => 999,n,Hangup()
exten => h,1,Verbose(2,Hang-> The channel name is ${MYVAR})
exten => t,1,Verbose(2,timeout t)
exten => T,1,Verbose(2,timeout T)
exten => i,1,Verbose(2,timeout T)

What I get in Eclipse console

Dec 12, 2014 8:05:23 PM org.asteriskjava.manager.internal.ManagerConnectionImpl connect INFO: Connecting to localhost:5038 Dec 12, 2014 8:05:23 PM org.asteriskjava.manager.internal.ManagerConnectionImpl setProtocolIdentifier INFO: Connected via Asterisk Call Manager/2.5.0 Dec 12, 2014 8:05:23 PM org.asteriskjava.manager.internal.ManagerConnectionImpl setProtocolIdentifier WARNING: Unsupported protocol version 'Asterisk Call Manager/2.5.0'. Use at your own risk. Dec 12, 2014 8:05:23 PM org.asteriskjava.manager.internal.ManagerConnectionImpl doLogin INFO: Successfully logged in Dec 12, 2014 8:05:23 PM org.asteriskjava.manager.internal.EventBuilderImpl buildEvent INFO: No event class registered for event type 'fullybooted', attributes: {status=Fully Booted, event=FullyBooted, privilege=system,all} Dec 12, 2014 8:05:25 PM org.asteriskjava.manager.internal.ManagerConnectionImpl doLogin INFO: Determined Asterisk version: Asterisk 1.0 Error Dec 12, 2014 8:05:25 PM org.asteriskjava.manager.internal.ManagerConnectionImpl disconnect INFO: Closing socket.

Please help :frowning:

[size=150][color=#FF40BF]how to change rename for this command asterisk -rvv
plz give me the detail solution[/color][/size]

Requests for detailed solutions should be made on the Biz and Jobs forum. Please indicate how much you are prepared to pay for the work.

Hi, I also followed the tutorial from this website and followed the instructions on this forum. However I just get an “Error” when I start the program. I try to add try catch block but nothing worked I only get “Error” with no other info. Also, the program succesfully connect to asterisk but when I send command it just print “Error”.

Here’s the code :

import java.io.IOException;

import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.OriginateAction;
import org.asteriskjava.manager.response.ManagerResponse;
//import org.asteriskjava.fastagi.BaseAgiScript;

public class HelloManager
{
    private ManagerConnection managerConnection;

    public HelloManager() throws IOException
    {
        ManagerConnectionFactory factory = new ManagerConnectionFactory(
                "192.168.80.7", "manager", "123");

        this.managerConnection = factory.createManagerConnection();
    }

    public void run() throws IOException, AuthenticationFailedException,
            TimeoutException
    {
        OriginateAction originateAction;
        ManagerResponse originateResponse;

	try {
        originateAction = new OriginateAction();
        originateAction.setChannel("SIP/phone1");
        originateAction.setContext("default");
        originateAction.setExten("3020");
        originateAction.setPriority(new Integer(1));
        originateAction.setTimeout(new Integer(30000));



        // 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



	try {
	originateResponse = managerConnection.sendAction(originateAction, 30000);
	System.out.println(originateResponse.getResponse());

	} catch (Exception e) {
    	e.printStackTrace();
    	System.out.println("The error is  " + e.getMessage());
	}
        //originateResponse = managerConnection.sendAction(originateAction, 30000);


        // print out whether the originate succeeded or not
        //System.out.println(originateResponse.getResponse());
	}catch(Exception e){
		e.printStackTrace();
		System.out.println("Error  is : " + e.getMessage());
	}finally{

        // and finally log off and disconnect
        managerConnection.logoff();
	}
    }

    public static void main(String[] args) throws Exception
    {
        HelloManager helloManager;

        helloManager = new HelloManager();
        helloManager.run();
    }
}

Of course phone1 is a sip.conf domain and the extension 3020 is available.

Found the solution ! The context wasn’t the right one (mine was “labo” )! Sorry about that.

If people get the “error” message I recommend to carrefully check every parameters of the originateAction!