Track NCCO progress

In this code snippet you see how to track how far through an NCCO a caller gets using the notify action

Example

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 "Track NCCO Progress Example" http://demo.ngrok.io/webhooks/answer http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

composer require nexmo/client
Initialize your dependencies

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

Copy to Clipboard
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;

View full source

Write the code

Add the following to index.php:

Copy to Clipboard
$app->get('/webhooks/answer', function (Request $request, Response $response) {
    $params = $request->getQueryParams();
    $uri = $request->getUri();
    $ncco = [
        [
            'action' => 'talk',
            'text' => 'Thanks for calling the notification line'
        ],
        [
            'action' => 'notify',
            'payload' => ['foo' => 'bar'],
            'eventUrl' => [
                $uri->getScheme().'://'.$uri->getHost().':'.$uri->getPort().'/webhooks/notification'
            ]
        ],
        [
            'action' => 'talk',
            'text' => 'You will never hear me as the notification URL will return an NCCO'
        ]
    ];

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

// The notification can be sent via GET or POST, depending on your eventMethod
$app->map(['GET', 'POST'], '/webhooks/notification', function(Request $request, Response $response) {
    $params = $request->getParsedBody();

    // Fall back to query parameters if needed
    if (!count($params)){
        $params = $request->getQueryParams();
    }

    error_log(print_r($params, true));

    return $response->withJson([
        [
            'action' => 'talk',
            'text' => 'Your notification has been received, loud and clear'
        ]
    ]);
});

$app->map(['GET', 'POST'], '/webhooks/event', function (Request $request, Response $response) {
    $params = $request->getParsedBody();

    // Fall back to query parameters if needed
    if (!count($params)){
        $params = $request->getQueryParams();
    }

    error_log(print_r($params, true));
    return $response->withStatus(204);
});

$app->run();

View full source

Run your code

Save this file to your machine and run it:

php index.php

Try it out

When you call your Nexmo Number you will hear a text-to-speech message and receive a request to your notification URL