0% found this document useful (0 votes)
45 views4 pages

ARI and AGI

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

ARI and AGI

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

To run ARI (Asterisk REST Interface) and AGI (Asterisk Gateway Interface) scripts in parallel

on an Asterisk server using Java, you need to set up and coordinate both interfaces in the
Asterisk dialplan. Here’s a detailed guide on how to achieve this:

Prerequisites

1. Asterisk Installation: Ensure that Asterisk is installed and properly configured.


2. ARI and AGI Configuration: Ensure ARI is enabled and AGI scripts are executable.
3. Java Development Environment: Ensure you have Java installed and set up on your
server.

Steps

1. Enable ARI in Asterisk

Ensure ARI is enabled in the http.conf and ari.conf files in the /etc/asterisk/ directory.

http.conf:

ini
[general]
enabled=yes
bindaddr=0.0.0.0
bindport=8088

ari.conf:

ini

[general]
enabled=yes
pretty=yes

[asterisk]
type=user
read_only=no
password=your_password

2. Create Your ARI Application in Java

Create an ARI application using Java. You can use libraries like Unirest for HTTP requests.

ARI Script (AriApp.java):

java
Copy code
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import org.json.JSONObject;

public class AriApp {


private static final String ARI_URL = "http://localhost:8088/ari";
private static final String USERNAME = "asterisk";
private static final String PASSWORD = "your_password";
private static final String APP_NAME = "my_ari_app";

public static void main(String[] args) {


try {
HttpResponse<JsonNode> response = Unirest.get(ARI_URL + "/events")
.queryString("app", APP_NAME)
.basicAuth(USERNAME, PASSWORD)
.asJson();

response.getBody().getArray().forEach(item -> {
JSONObject event = (JSONObject) item;
handleEvent(event);
});
} catch (Exception e) {
e.printStackTrace();
}
}

private static void handleEvent(JSONObject event) {


System.out.println("Handling event: " + event.toString());
// Add your ARI handling logic here
}
}

3. Create Your AGI Script in Java

Create an AGI script using Java. You can use the Asterisk-java library to simplify this process.

AGI Script (AgiApp.java):

java
Copy code
import org.asteriskjava.fastagi.AgiChannel;
import org.asteriskjava.fastagi.AgiRequest;
import org.asteriskjava.fastagi.AgiScript;
import org.asteriskjava.fastagi.AgiServer;
import org.asteriskjava.fastagi.DefaultAgiServer;
import org.asteriskjava.fastagi.command.StreamFileCommand;

public class AgiApp implements AgiScript {


@Override
public void service(AgiRequest request, AgiChannel channel) throws Exception {
channel.answer();
channel.streamFile("hello-world");
channel.hangup();
}

public static void main(String[] args) {


AgiServer server = new DefaultAgiServer();
server.startup();
}
}

4. Configure Dialplan to Run ARI and AGI Scripts

Edit your extensions.conf to configure the dialplan to run both ARI and AGI scripts.

extensions.conf:

ini
Copy code
[default]
exten => _X.,1,NoOp(Starting AGI and ARI scripts)
same => n,AGI(agi://localhost)
same => n,Stasis(my_ari_app)
same => n,Hangup()

Running the Scripts

Compile and Run the Java Applications

Compile and run your Java applications.

Compile ARI Script:

sh
Copy code
javac -cp unirest-java-1.4.9.jar AriApp.java

Run ARI Script:

sh
Copy code
java -cp .:unirest-java-1.4.9.jar AriApp

Compile AGI Script:

sh
Copy code
javac -cp asterisk-java.jar AgiApp.java

Run AGI Script:

sh
Copy code
java -cp .:asterisk-java.jar AgiApp

Testing the Setup

Make a call to the extension defined in your extensions.conf to trigger the dialplan. Both the AGI
and ARI scripts should run as configured.

Conclusion

By setting up the Asterisk dialplan and Java applications as outlined, you can run ARI and AGI
scripts in parallel to handle different aspects of call processing simultaneously. This setup
leverages the power of Java to interact with Asterisk's REST and Gateway interfaces, allowing
for sophisticated call handling and integration capabilities.

You might also like