Receive an inbound call

In this building block you see how to receive an inbound call.

Example

Prerequisites

Create an application

Install dependencies

Write the code

Add the following to InboundCall.java:

Route answerRoute = (req, res) -> {
    String from = req.queryParams("from").replace("", " ");
    TalkNcco message = new TalkNcco(String.format("Thank you for calling from %s", from));

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

    res.type("application/json");
    return new ObjectMapper().writer().writeValueAsString(nccos);
};

Spark.port(3000);
Spark.get("/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 InboundCall:

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

Prerequisites

Create an application

Install dependencies

Write the code

Add the following to index.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();

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 receive-an-inbound-call.py:

@app.route("/webhooks/answer")
def answer_call():
    for param_key, param_value in request.args.items():
        print("{}: {}".format(param_key, param_value))

    from_ = request.args['from']

    return jsonify([
        {
            "action": "talk",
            "text": "Thank you for calling from " + " ".join(from_)
        }
    ])

View full source 

Run your code

Save this file to your machine and run it:

$ python3 receive-an-inbound-call.py

Try it out

When you call your Nexmo Number you will hear a text-to-speech message.