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

Install dependencies

Initialize your dependencies

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

Install dependencies

Write the code

Add the following to ConnectInboundCall.java:

Copy to Clipboard
Route answerRoute = (req, res) -> {
    ConnectNcco connect = new ConnectNcco(RECIPIENT_NUMBER);
    connect.setFrom(NEXMO_NUMBER);

    Ncco[] nccos = new Ncco[]{connect};

    res.type("application/json");

    // com.fasterxml.jackson.databind.ObjectMapper;
    return new ObjectMapper().writer().writeValueAsString(nccos);
};

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

Install dependencies

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

Install dependencies

Initialize your dependencies

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

Install dependencies

Initialize your dependencies

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.