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

Connect an inbound call

In this code snippet you see how to connect an inbound call to another person by making an outbound call.

Example

Replace the following variables in the example code:

Key Description
VONAGE_NUMBER Your Vonage Number. E.g. 447700900000
YOUR_SECOND_NUMBER The number you are connecting to. E.g. 447700900002

Prerequisites

A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Vonage needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Connect Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

npm install express

Create a file named connect-an-inbound-call.js and add the following code:

const app = require('express')()

View full source

Write the code

Add the following to connect-an-inbound-call.js:

const onInboundCall = (request, response) => {
  const ncco = [{
    action: 'connect',
    from: VONAGE_NUMBER,
    endpoint: [{
      type: 'phone',
      number: YOUR_SECOND_NUMBER
    }]
  }]

  response.json(ncco)
}

app.get('/webhooks/answer', onInboundCall)

app.listen(3000)

View full source

Run your code

Save this file to your machine and run it:

node connect-an-inbound-call.js

Prerequisites

A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Vonage needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Connect Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

Add the following to `build.gradle`:

compile 'com.vonage:client:5.5.0'
compile 'com.sparkjava:spark-core:2.7.2'

Write the code

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


/*
 * Route to answer incoming calls with an NCCO response.
 */
Route answerRoute = (req, res) -> {
    ConnectAction connect = ConnectAction.builder()
            .endpoint(PhoneEndpoint.builder(RECIPIENT_NUMBER).build())
            .from(VONAGE_NUMBER)
            .build();

    res.type("application/json");

    return new Ncco(connect).toJson();
};

Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/answer", answerRoute);

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 ConnectInboundCall:

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

Prerequisites

A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Vonage needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Connect Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

Install-Package Vonage

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

using Vonage.Voice.Nccos;
using Vonage.Voice.Nccos.Endpoints;

View full source

Write the code

Add the following to ConnectInboundCallController.cs:

[Route("webhooks/answer")]
public string Answer()
{
    var YOUR_SECOND_NUMBER = Environment.GetEnvironmentVariable("YOUR_SECOND_NUMBER") ?? "YOUR_SECOND_NUMBER";
    var VONAGE_NUMBER = Environment.GetEnvironmentVariable("VONAGE_NUMBER") ?? "VONAGE_NUMBER";

    var talkAction = new TalkAction()
    {
        Text = "Thank you for calling",
        VoiceName = "Kimberly"
    };

    var secondNumberEndpoint = new PhoneEndpoint() { Number=YOUR_SECOND_NUMBER};            
    var connectAction = new ConnectAction() { From=VONAGE_NUMBER, Endpoint= new[] { secondNumberEndpoint } };
    
    var ncco = new Ncco(talkAction,connectAction);
    return ncco.ToString();
}

View full source

Prerequisites

A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Vonage needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Connect Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

composer require slim/slim:^3.8 vonage/client

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

use Dotenv\Dotenv;
use Laminas\Diactoros\Response\JsonResponse;
use \Psr\Http\Message\ResponseInterface as Response;
use \Psr\Http\Message\ServerRequestInterface as Request;

View full source

Add the following to index.php:

require 'vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

define('YOUR_SECOND_NUMBER', getenv('YOUR_SECOND_NUMBER'));
define('VONAGE_NUMBER', getenv('VONAGE_NUMBER'));

$app = new \Slim\App();

View full source

Write the code

Add the following to index.php:

$app->get('/webhooks/answer', function (Request $request, Response $response) {
    $numberToConnect = new \Vonage\Voice\Endpoint\Phone(YOUR_SECOND_NUMBER);

    $action = new \Vonage\Voice\NCCO\Action\Connect($numberToConnect);
    $action->setFrom(VONAGE_NUMBER);

    $ncco = new \Vonage\Voice\NCCO\NCCO();
    $ncco->addAction($action);

    return new JsonResponse($ncco->toArray());
});

$app->run();

View full source

Run your code

Save this file to your machine and run it:

php -S localhost:3000 -t .

Prerequisites

A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Vonage needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Connect Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

pip install 'flask>=1.0'

Create a file named connect-an-inbound-call.py and add the following code:

#!/usr/bin/env python3
from flask import Flask, jsonify

app = Flask(__name__)

View full source

Write the code

Add the following to connect-an-inbound-call.py:

@app.route("/webhooks/answer")
def answer_call():
    ncco = [
        {
            "action": "connect",
            "from": "VONAGE_NUMBER",
            "endpoint": [{
                "type": 'phone',
                "number": "YOUR_SECOND_NUMBER"
            }]
        }
    ]
    return jsonify(ncco)

if __name__ == '__main__':
    app.run(port=3000)

View full source

Run your code

Save this file to your machine and run it:

python3 connect-an-inbound-call.py

Prerequisites

A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.

Install the CLI

npm install -g nexmo-cli

Create an application

Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID in the example below. It will also create private.key in the current directory which you will need in the Initialize your dependencies step

Vonage needs to connect to your local machine to access your answer_url. We recommend using ngrok to do this. Make sure to change demo.ngrok.io in the examples below to your own ngrok URL.

nexmo app:create "Connect Inbound Call Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key

gem install sinatra sinatra-contrib

Create a file named connect_an_inbound_call.rb and add the following code:

require 'sinatra'
require 'sinatra/multi_route'
require 'json'

before do
  content_type :json
end

View full source

Write the code

Add the following to connect_an_inbound_call.rb:

route :get, :post, '/webhooks/answer' do
  [{
    action: 'connect',
    endpoint: [{
      type: 'phone',
      number: YOUR_SECOND_NUMBER
    }]
  }].to_json
end

set :port, 3000

View full source

Run your code

Save this file to your machine and run it:

ruby connect_an_inbound_call.rb

Try it out

You'll need to expose your server to the open internet. During development you can use a tool like Ngrok to do that.

When you call your Vonage Number you will automatically be connected to the number you specified in place of YOUR_SECOND_NUMBER.