Skip to content

Instantly share code, notes, and snippets.

@adambutler
Last active December 4, 2017 09:02
Show Gist options
  • Save adambutler/3aa45be4116ac38eaca42f001e655ed8 to your computer and use it in GitHub Desktop.
Save adambutler/3aa45be4116ac38eaca42f001e655ed8 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-node-quickstart/blob/master/sms/dlr-express.js
// Lines: ALL LINES

const app = require('express')()
const bodyParser = require('body-parser')

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app
  .route('/webhooks/delivery-receipt')
  .get(handleDeliveryReceipt)
  .post(handleDeliveryReceipt)

function handleDeliveryReceipt(request, response) {
  const params = Object.assign(request.query, request.body)
  console.log(params)
  response.status(204).send()
}

app.listen(3000)

Run your server

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

$ node app.js

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