Receive an Inbound Call

Receiving inbound calls with Nexmo is easy. You just need a server that provides a endpoint that Nexmo will call when your number is called. You configure this endpoint by associating your Nexmo number with an application.

Prerequisites

Implement a webhook

const app = require('express')()

const onInboundCall = (request, response) => {
  const from = request.query.from
  const fromSplitIntoCharacters = from.split('').join(' ')

  const ncco = [{
    action: 'talk',
    text: `Thank you for calling from ${fromSplitIntoCharacters}`
  }]

  response.json(ncco)
}

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

app.listen(3000)

Run your server

Save this file to your machine and run it using the node command:

$ node app.js

Implement a webhook

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;
$app->get('/webhooks/answer', function (Request $request, Response $response) {
    $params = $request->getQueryParams();
    $fromSplitIntoCharacters = implode(" ", str_split($params['from']));

    $ncco = [
        [
            'action' => 'talk',
            'text' => 'Thank you for calling from '.$fromSplitIntoCharacters
        ]
    ];

    return $response->withJson($ncco);
});

$app->run();

Run your server

Save this file to your machine and run it:

$ php -t . -S localhost:3000

Implement a webhook

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

helpers do
  def parsed_body
    JSON.parse(request.body.read)
  end
end

before do
  content_type :json
end

route :get, :post, '/webhooks/answer' do
  from = params['from'] || parsed_body['from']
  from_split_into_characters = from.split('').join(' ')

  [{
    action: 'talk',
    text: "Thank you for calling from #{from_split_into_characters}"
  }].to_json
end

set :port, 3000

Run your server

Save this file to your machine and run it:

$ ruby app.rb

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

Associate an application to your webhook

To link your number to the endpoint you've just created we'll need an Application.

$ nexmo app:create demo <YOUR_HOSTNAME>/webhooks/answer <YOUR_HOSTNAME>/webhooks/event
$ nexmo link:app <NEXMO_NUMBER> <NEXMO_APPLICATION_ID>

Call your number

When you call your Nexmo number you should now get a TTS response back.