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:16
Show Gist options
  • Save mheap/f6dbefdbfcdf80c7f601eccb4fc62f62 to your computer and use it in GitHub Desktop.
Save mheap/f6dbefdbfcdf80c7f601eccb4fc62f62 to your computer and use it in GitHub Desktop.

Handle inbound SMS

Specification

A building block that shows how to handle an inbound call

  • You must specify one endpoint must be on the path /webhooks/inbound-sms
  • 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

Handle inbound SMS

Handling inbound calls with Nexmo is easy. You just need to create a webhook endpoint and configure your number or account to point to the endpoint.

Prerequisites

Implement a webhook endpoint

// GITHUB: https://github.com/nexmo-community/nexmo-php-quickstart/blob/master/sms/receive-with-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/inbound-sms', $handler);
$app->post('/webhooks/inbound-sms', $handler);

$app->run();

Run your server

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

$ php -t . -S 127.0.0.1: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 Inbound Message.

Send your Nexmo number an SMS

Now when you send your Nexmo number an SMS you should see it logged in your console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment