In this tutorial, you will learn how to initiate a conference call to connect multiple people who phone into a number. The example code is on GitHub.
Previously, we’ve shown you how to Receive a Phone Call and Forward a Phone Call so if you’re not already familiar with those concepts, read those posts first.
Prerequisites
To work through this tutorial, you will need a Nexmo account. Sign up now if you don’t already have an account.
You will be using Gradle to manage your dependencies and run your application. Additionally, you’ll need to make sure you have a copy of the JDK installed. I will be using JDK 11 in this tutorial, but anything JDK 8 and higher should work.
Finally, you’ll need the Nexmo CLI installed. You’ll use it to purchase a phone number and configure your Nexmo account to point at your new application.
Create a Conference Call With Java
This tutorial will walk you through the following steps:
- Using Gradle to set up a new Java project.
- Using the Spark framework for controlling the call.
- Purchasing a number and configuring your Nexmo account to use that number with your application.
Using Gradle to Setup a New Java Project
You will use Gradle to manage your dependencies and to create and run your Java application.
The gradle init --type=java-application
command will create all of the folders you will need as well as a sample class where you will be writing your code.
From the command line, create a new Java project with the following commands, accepting any of the default prompts from Gradle:
1 2 3 4 |
mkdir conference-call cd conference-call gradle init --type=java-application |
Gradle will create the App
class in the src/main/java/conference/call
folder.
Inside of this class is a getGreeting
and main
method. You won’t need the getGreeting
method, so feel free to remove it.
Your App
class should look like this:
1 2 3 4 5 6 7 8 9 10 11 |
/* * This Java source file was generated by the Gradle 'init' task. */ package conference.call; public class App { public static void main(String[] args) { // Future Code Goes Here } } |
Using the Spark Framework for Controlling the Call
You will use the Spark framework to receive an HTTP call made by Nexmo when your number gets a call.
Adding the Dependencies
Add the following to the dependencies
block in your build.gradle
file:
1 2 3 4 5 6 |
// Spark Framework implementation "com.sparkjava:spark-core:2.8.0" // Nexmo Java Client implementation "com.nexmo:client:5.1.0" |
Your dependencies
block should look like this:
1 2 3 4 5 6 7 8 |
dependencies { implementation "com.sparkjava:spark-core:2.8.0" implementation "com.nexmo:client:5.1.0" // Use JUnit test framework testImplementation 'junit:junit:4.12' } |
Define the Answer Route
When a call is received, Nexmo will send a request to a pre-defined webhook URL. It expects to receive a Nexmo Call Control Object (NCCO) containing a list of actions to execute.
You will be using the talk
action to greet the incoming caller, and the conversation
action to connect incoming calls to a conference.
This is the resulting NCCO that your application will create:
1 2 3 4 5 6 7 8 9 10 11 |
[ { "text": "You will now be added to the conference call.", "action": "talk" }, { "name": "team-meeting", "action": "conversation" } ] |
You will use the TalkAction
and ConversationAction
classes from the Nexmo Java Server SDK for building the above NCCO.
Add the following code to the main
method:
1 2 3 4 5 6 7 |
TalkAction talk = TalkAction.builder("You will now be added to the conference call.") .build(); ConversationAction conversation = ConversationAction.builder("team-meeting") .build(); Ncco ncco = new Ncco(talk, conversation); |
Now that you have defined the NCCO, you will configure Spark to serve the NCCO. Your application will listen on port 3000
and the route will be /webhooks/answer
.
Add the following to the main
method:
1 2 3 4 5 6 |
port(3000); get("/webhooks/answer", (req, res) -> { res.type("application/json"); return ncco.toJson(); }); |
The main
method will now look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static void main(String[] args) { TalkAction talk = TalkAction.builder("You will now be added to the conference call.") .build(); ConversationAction conversation = ConversationAction.builder("team-meeting") .build(); Ncco ncco = new Ncco(talk, conversation); port(3000); get("/webhooks/answer", (req, res) -> { res.type("application/json"); return ncco.toJson(); }); } |
Purchasing a Number
You will need a Nexmo number to receive phone calls. If you do not have a number, you can use the Nexmo CLI to purchase one:
1 2 |
nexmo number:buy --country_code US |
Take note of the number that is assigned to you. You will need this number to link your application and for testing.
Exposing Your Application
To send an HTTP request to your application, Nexmo needs to know the URL that your application is running on.
Instead of configuring your local network or hosting your application on an external service, you can use ngrok to expose your application to the internet safely.
Download ngrok and run the following command:
1 2 |
ngrok http 3000 |
Take note of the forwarding address as you will need it when you configure your account. In the following picture, the forwarding address is http://99cad2de.ngrok.io
.
Configure Your Nexmo Account
If you do not have an application you can use the Nexmo CLI to create one using your ngrok forwarding address:
1 2 |
nexmo app:create "Conference Call Demo" http://your-ngrok-forwarding-address/webhooks/answer http://your-ngrok-forwarding-address/webhooks/events --keyfile private.key |
After running this command, you will be shown an application id. For example: notreal-1111-2222-3333-appid
. You will need this application id to link your phone number to the application.
You can use the Nexmo CLI to link your phone number and application:
1 2 |
nexmo link:app your-nexmo-phone-number your-application-id |
Test Your Application
Start your application with the gradle run
command inside of your conference-call
directory.
Call your Nexmo number and grab a friend, colleague, or another phone to do the same. Every new caller is entered into the conference call.
Conclusion
In a few lines of code, you have created an application that can create conference calls with multiple people.
Check out our documentation on Nexmo Developer where you can learn more about call flow or Nexmo Call Control Objects.
You can also take a look at the Conference Call code snippet for this example and more.