Connect an inbound call

In this building block you see how to connect an inbound call to a second number.

Example

Replace the following variables in the example code:

Key Description
YOUR_SECOND_NUMBER The number you wish the inbound caller to be connected to.

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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 dependencies

$ npm install express body-parser
Initialize your dependencies

Create a file named app.js and add the following code:

Copy to Clipboard
const app = require('express')()

View full source 

Write the code

Add the following to app.js:

Copy to Clipboard
const onInboundCall = (request, response) => {
  const ncco = [{
    action: 'connect',
    endpoint: [{
      type: 'phone',
      number: NEXMO_TO_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 app.js

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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 dependencies

Add the following to build.gradle:

compile 'com.nexmo:client:4.0.0'
compile 'com.sparkjava:spark-core:2.7.2'

Write the code

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

Copy to Clipboard
/*
 * Route to answer incoming calls with an NCCO response.
 */
Route answerRoute = (req, res) -> {
    ConnectAction connect = new ConnectAction.Builder()
            .endpoint(new PhoneEndpoint.Builder(RECIPIENT_NUMBER).build())
            .from(NEXMO_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.nexmo.quickstart.voice with the package containing ConnectInboundCall:

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

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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 dependencies

$ composer require slim/slim:^3.8

Write the code

Add the following to index.php:

Copy to Clipboard
$app = new \Slim\App;

$app->get('/webhooks/answer', function (Request $request, Response $response) {
    $ncco = [
        [
            'action' => 'connect',
            'endpoint' => [
                [
                    'type' => 'phone',
                    'number' => YOUR_SECOND_NUMBER
                ]
            ]
        ]
    ];
    return $response->withJson($ncco);
});
$app->run();

View full source 

Run your code

Save this file to your machine and run it:

$ php -t . -S localhost:3000

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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 dependencies

$ pip install 'flask>=1.0'
Initialize your dependencies

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

Copy to Clipboard
from flask import Flask, request, jsonify

app = Flask(__name__)

View full source 

Write the code

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

Copy to Clipboard
app = Flask(__name__)

@app.route("/webhooks/answer")
def answer_call():
    ncco = [
        {
            "action": "connect",
            "from": "NEXMO_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

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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 dependencies

$ gem install sinatra
Initialize your dependencies

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

Copy to Clipboard
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:

Copy to Clipboard
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 Nexmo Number you will automatically be connected to the number you specified in place of YOUR_SECOND_NUMBER.