这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

Play DTMF into a call

This code snippet plays DTMF tones into the specified call.

Example

Replace the following variables in the example code:

Key Description
UUID The UUID of the call leg
DIGITS Digits representing the DTMF tones that will be played into the call.

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

Execute the following command at your terminal prompt to create the JWT for authentication:

export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)

Write the code

Add the following to play-dtmf-into-a-call.sh:

curl -X PUT https://api.nexmo.com/v1/calls/$UUID/dtmf \
  -H "Authorization: Bearer "$JWT\
  -H "Content-Type: application/json"\
  -d '{"digits": 1713}'

View full source

Run your code

Save this file to your machine and run it:

bash play-dtmf-into-a-call.sh

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

npm install @vonage/server-sdk

Create a file named play-dtmf-into-call.js and add the following code:

const Vonage = require('@vonage/server-sdk');

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
  applicationId: VONAGE_APPLICATION_ID,
  privateKey: VONAGE_PRIVATE_KEY
}, {debug: true});

View full source

Write the code

Add the following to play-dtmf-into-call.js:

const DIGITS = '1234567890';

vonage.calls.dtmf.send(UUID, { digits: DIGITS }, (err, res) => {
  if(err) { console.error(err); }
  else {
      console.log(res);
  }
});

View full source

Run your code

Save this file to your machine and run it:

node play-dtmf-into-call.js

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

Add the following to `build.gradle`:

compile 'com.vonage:client:6.2.0'

Create a class named SendDtmfToCall and add the following code to the main method:

VonageClient client = VonageClient.builder()
        .applicationId(VONAGE_APPLICATION_ID)
        .privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
        .build();

View full source

Write the code

Add the following to the main method of the SendDtmfToCall class:

final String ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/long-tts.json";
CallEvent call = client.getVoiceClient().createCall(new Call(
        TO_NUMBER,
        VONAGE_NUMBER,
        ANSWER_URL
));

Thread.sleep(20000);

final String UUID = call.getUuid();
final String DIGITS = "332393";
client.getVoiceClient().sendDtmf(UUID, DIGITS);

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

  apply plugin: 'application'
  mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.voice with the package containing SendDtmfToCall:

gradle run -Pmain=com.vonage.quickstart.voice.SendDtmfToCall

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

Install-Package Vonage

Create a file named PlayDtmfIntoCall.cs and add the following code:

using Vonage.Voice;
using Vonage.Request;
using Vonage;

View full source

Add the following to PlayDtmfIntoCall.cs:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);

View full source

Write the code

Add the following to PlayDtmfIntoCall.cs:

var command = new DtmfCommand() { Digits = DIGITS };

var response = client.VoiceClient.StartDtmf(UUID, command);

View full source

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

composer require vonage/client

Create a file named index.php and add the following code:

$keypair = new \Vonage\Client\Credentials\Keypair(
    file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
    VONAGE_APPLICATION_ID
);

$client = new \Vonage\Client($keypair);

View full source

Write the code

Add the following to index.php:

$client->voice()->playDTMF(UUID, '2468#');

View full source

Run your code

Save this file to your machine and run it:

php index.php

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

pip install vonage

Create a file named play-dtmf-into-call.py and add the following code:

client = vonage.Client(
    application_id=VONAGE_APPLICATION_ID,
    private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

Add the following to play-dtmf-into-call.py:

voice = vonage.Voice(client)

voice.send_dtmf(VONAGE_CALL_UUID, digits='1234')

View full source

Run your code

Save this file to your machine and run it:

python3 play-dtmf-into-call.py

Prerequisites

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID and private key that were used to create the call.

gem install vonage

Create a file named play-dtmf-into-call.rb and add the following code:


client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)

View full source

Write the code

Add the following to play-dtmf-into-call.rb:


DIGITS = '332393'
response = client.voice.dtmf.send(UUID, digits: DIGITS)

View full source

Run your code

Save this file to your machine and run it:

ruby play-dtmf-into-call.rb

Try it out

When you run the code a series of DTMF tones is played into the call identified with the specified UUID.