Skip to content

startcloud-dev/nexmo-java

 
 

Repository files navigation

Nexmo Client Library for Java

Build Status codecov Codacy Badge

You can use this Java client library to add Nexmo's API to your application. To use this, you'll need a Nexmo account. Sign up for free at nexmo.com.

Installation

To use the client library you'll need to have created a Nexmo account.

To install the Java client library using Gradle, add the following to build.gradle:

compile 'com.nexmo:nexmo-java:2.0'

Alternatively you can clone the repo and build the jar yourself:

git clone [email protected]:nexmo/nexmo-java.git
gradle build

Usage

Check the Javadoc for full documentation.

Examples

The following code initiates an outbound call which then reads the user a message:

import java.nio.file.Paths;

import com.nexmo.client.auth.JWTAuthMethod;
import com.nexmo.client.voice.NexmoClient;
import com.nexmo.client.voice.Call;

JWTAuthMethod auth = new JWTAuthMethod(application_id, Paths.get("application_key.pem"));
NexmoClient client = new NexmoClient(auth);
Call call = new Call(to, from,
                     "https://nexmo-community.github.io/ncco-examples/first_call_talk.json");
CallEvent event = client.getVoiceClient().createCall(call);

After the call is answered, you can get more information about it, including the amount it cost with:

CallRecord info = client.getVoiceClient().getCallDetails(event.getUuid());
System.out.println("This cost: " + info.getPrice() + " EUR");

You can modify an existing call in progress, for example by hanging up on the current call:

ModifyCallResponse modifyResponse = client.getVoiceClient().modifyCall(event.getUuid(), "hangup");
System.out.println(modifyResponse.getMessage());

While a call is in progress, you can send Dual-tone multi-frequency(DTMF) tones like so:

DtmfResponse dtmfResponse = client.getVoiceClient().sendDtmf(event.getUuid(), "332393");
System.out.println("Success! " + dtmfResponse.getMessage());

To stream an audio file to an active call, simply use the following method:

StreamResponse startStreamResponse = client.getVoiceClient().startStream(event.getUuid(), "https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3");
System.out.println("Success! " + startStreamResponse.getMessage());

If you'd like to stop streaming an audio file to an active call, you can do so with:

StreamResponse stopStreamResponse = client.getVoiceClient().stopStream(event.getUuid());
System.out.println("Alright. " + stopStreamResponse.getMessage());

To send a synthesized speech message to an active call, just use the following method:

TalkResponse startTalkResponse = client.getVoiceClient().startTalk(event.getUuid(), "Hello World");
System.out.println("Success! " + startTalkResponse.getMessage());

If you'd like to stop sending a synthesized speech message to an active call, you can do so with:

TalkResponse stopTalkResponse = client.getVoiceClient().stopTalk(event.getUuid());
System.out.println("Alright. " + stopTalkResponse.getMessage());

API Coverage

  • Account
    • Balance
    • Pricing
    • Settings
    • Top Up
    • Numbers
      • Search
      • Buy
      • Cancel
      • Update
  • Number Insight
    • Basic
    • Standard (Currently uses the deprecated API)
    • Advanced (Currently uses the deprecated API)
    • Webhook Notification
  • Verify
    • Verify
    • Check
    • Search
    • Control
  • Messaging
    • Send
    • Delivery Receipt (Callback can only be set in the Dashboard)
    • Inbound Messages
    • Search
      • Message
      • Messages
      • Rejections
    • US Short Codes
      • Two-Factor Authentication
      • Event Based Alerts
        • Sending Alerts
        • Campaign Subscription Management
  • Voice
    • Outbound Calls
    • Inbound Call
    • Text-To-Speech Call
    • Text-To-Speech Prompt

License

This library is released under the MIT License

About

Nexmo REST API Client Library for Java. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Java 100.0%