Skip to content

Instantly share code, notes, and snippets.

@adambutler
Created November 14, 2017 16:58
Show Gist options
  • Save adambutler/2164da265280cc0be3970ddeb4c75576 to your computer and use it in GitHub Desktop.
Save adambutler/2164da265280cc0be3970ddeb4c75576 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-node-quickstart/blob/master/sms/receive-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/inbound-sms')
  .get(handleInboundSms)
  .post(handleInboundSms)

function handleInboundSms(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 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