Skip to content

Instantly share code, notes, and snippets.

@mheap

mheap/README.md Secret

Forked from adambutler/README.md
Last active November 29, 2017 13:20
Show Gist options
  • Save mheap/43339b6ad882e583bb0be108b5983404 to your computer and use it in GitHub Desktop.
Save mheap/43339b6ad882e583bb0be108b5983404 to your computer and use it in GitHub Desktop.

Receive an SMS Delivery Receipt

Specification

A building block that shows how to handle an inbound call

  • You must specify one endpoint must be on the path /webhooks/delivery-receipt
  • Your endpoint must be accessible on GET and on POST requests
  • Your endpoint must listen on port 3000
  • Your response should return a 204 status code
  • Your endpoint should handle query string parameters on GET
  • Your endpoint should handle application/x-www-form-urlencoded parameters on POST
  • Your endpoint should handle JSON on POST

Note: Please test using all three settings in Dashboard > Settings: GET, POST & POST-JSON.

  • Code examples should include a GitHub link to the file
  • Code examples should include the line numbers used from the linked file
  • All code examples must be present in the master branch of the relevant quickstart repo
  • All code examples must be tested to work (manual testing is fine)

Instructions

  1. Fork this gist
  2. Edit the gist and the building block files to match the specification
  3. Return Gist as a comment in the original issue

Receive an SMS Delivery Receipt

You can subscribe to be notified of the delivery status of your outbound messages. This example shows you how to configure your webhook and implement the endpoint.

Implement a webhook endpoint

// GITHUB: https://github.com/nexmo-community/nexmo-php-quickstart/blob/master/sms/receive-delivery-receipt-slim/index.php
// Lines: ALL LINES

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

require 'vendor/autoload.php';

$app = new \Slim\App;

$handler = 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->get('/webhooks/delivery-receipt', $handler);
$app->post('/webhooks/delivery-receipt', $handler);

$app->run();

Run your server

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

$ php -t . -S localhost:3000

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

Configure your SMS endpoint with Nexmo

From Nexmo Dashboard go to Settings and enter your endpoint in the field labeled Webhook URL for Delivery Receipt.

Send an outbound SMS

Now when you send an outbound SMS with Nexmo you should see the status logged to your console.

$ nexmo sms YOUR_NUMBER 'A text message sent using the Nexmo SMS API'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment